当前位置: 首页>>代码示例>>Java>>正文


Java SharedPreferences类代码示例

本文整理汇总了Java中android.content.SharedPreferences的典型用法代码示例。如果您正苦于以下问题:Java SharedPreferences类的具体用法?Java SharedPreferences怎么用?Java SharedPreferences使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


SharedPreferences类属于android.content包,在下文中一共展示了SharedPreferences类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: prefs

import android.content.SharedPreferences; //导入依赖的package包/类
private static SharedPreferences prefs(Context context) {
    return PreferenceManager.getDefaultSharedPreferences(context);
}
 
开发者ID:jakehilborn,项目名称:speedr,代码行数:4,代码来源:Prefs.java

示例2: tab_5

import android.content.SharedPreferences; //导入依赖的package包/类
public static String tab_5 (Activity activity) {
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(activity);
    String s;

    final String tab_string = sharedPref.getString("tab_4", "");

    try {
        if (tab_string.isEmpty()) {
            s = activity.getString(R.string.context_tab);
        } else {
            s = tab_string;
        }
    } catch (Exception e) {
        Log.e("KidsPortal", "Unable to get String", e);
        s = activity.getString(R.string.context_tab);
    }
    return s;
}
 
开发者ID:JaeNuguid,项目名称:Kids-Portal-Android,代码行数:19,代码来源:helper_browser.java

示例3: onCreatePreferences

import android.content.SharedPreferences; //导入依赖的package包/类
@Override
public void onCreatePreferences(Bundle bundle, String s) {
    // Add 'general' preferences, defined in the XML file
    addPreferencesFromResource(R.xml.pref_general);

    SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();
    PreferenceScreen prefScreen = getPreferenceScreen();
    int count = prefScreen.getPreferenceCount();
    for (int i = 0; i < count; i++) {
        Preference p = prefScreen.getPreference(i);
        if (!(p instanceof CheckBoxPreference)) {
            String value = sharedPreferences.getString(p.getKey(), "");
            setPreferenceSummary(p, value);
        }
    }
}
 
开发者ID:fjoglar,项目名称:android-dev-challenge,代码行数:17,代码来源:SettingsFragment.java

示例4: getInformationFromFB

import android.content.SharedPreferences; //导入依赖的package包/类
private void getInformationFromFB(String UID) {
    dbr_users.child(UID).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            email = (String) dataSnapshot.child("mail").getValue();
            name = (String) dataSnapshot.child("name").getValue();
            username = (String) dataSnapshot.child("username").getValue();
            password = (String) dataSnapshot.child("password").getValue();
            telefonnummer = (String) dataSnapshot.child("telefonnummer").getValue();

            SharedPreferences.Editor editor = getSharedPreferences("CHAPP_PREFS", Context.MODE_PRIVATE).edit();
            editor.putString("email", email);
            editor.putString("name", name);
            editor.putString("username", username);
            editor.putString("telefonnummer", telefonnummer);
            editor.putString("password", password);
            editor.commit();
        };

        @Override
        public void onCancelled(DatabaseError databaseError) {
            // ...
        }
    });
}
 
开发者ID:burliEnterprises,项目名称:chapp-messenger,代码行数:26,代码来源:LoginActivity.java

示例5: getLocationCoordinates

import android.content.SharedPreferences; //导入依赖的package包/类
/**
 * Returns the location coordinates associated with the location. Note that there is a
 * possibility that these coordinates may not be set, which results in (0,0) being returned.
 * Interestingly, (0,0) is in the middle of the ocean off the west coast of Africa.
 *
 * @param context used to access SharedPreferences
 * @return an array containing the two coordinate values for the user's preferred location
 */
public static double[] getLocationCoordinates(Context context) {
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);

    double[] preferredCoordinates = new double[2];

    /*
     * This is a hack we have to resort to since you can't store doubles in SharedPreferences.
     *
     * Double.doubleToLongBits returns an integer corresponding to the bits of the given
     * IEEE 754 double precision value.
     *
     * Double.longBitsToDouble does the opposite, converting a long (that represents a double)
     * into the double itself.
     */
    preferredCoordinates[0] = Double
             .longBitsToDouble(sp.getLong(PREF_COORD_LAT, Double.doubleToRawLongBits(0.0)));
    preferredCoordinates[1] = Double
            .longBitsToDouble(sp.getLong(PREF_COORD_LONG, Double.doubleToRawLongBits(0.0)));

    return preferredCoordinates;
}
 
开发者ID:fjoglar,项目名称:android-dev-challenge,代码行数:30,代码来源:SunshinePreferences.java

示例6: getAllHostPackages

import android.content.SharedPreferences; //导入依赖的package包/类
public static Set<String> getAllHostPackages(Context ctx)
{
	SharedPreferences prefs = ctx.getSharedPreferences("KP2A.PluginAccess.hosts", Context.MODE_PRIVATE);
	Set<String> result = new HashSet<String>();
	for (String host: prefs.getAll().keySet())
	{
		try
		{
			PackageInfo info = ctx.getPackageManager().getPackageInfo(host, PackageManager.GET_META_DATA);
			//if we get here, the package is still there
			result.add(host);
		}
		catch (PackageManager.NameNotFoundException e)
		{
			//host gone. ignore.
		}
	}
	return result;
	
}
 
开发者ID:PhilippC,项目名称:keepass2android,代码行数:21,代码来源:AccessManager.java

示例7: clearExpired

import android.content.SharedPreferences; //导入依赖的package包/类
@Override
public boolean clearExpired(Date date) {
    boolean clearedAny = false;
    SharedPreferences.Editor prefsWriter = cookiePrefs.edit();

    for (ConcurrentHashMap.Entry<String, Cookie> entry : cookies.entrySet()) {
        String name = entry.getKey();
        Cookie cookie = entry.getValue();
        if (cookie.isExpired(date)) {
            // Clear cookies from local store
            cookies.remove(name);

            // Clear cookies from persistent store
            prefsWriter.remove(COOKIE_NAME_PREFIX + name);

            // We've cleared at least one
            clearedAny = true;
        }
    }

    // Update names in persistent store
    if (clearedAny) {
        prefsWriter.putString(COOKIE_NAME_STORE, TextUtils.join(",", cookies.keySet()));
    }
    prefsWriter.commit();

    return clearedAny;
}
 
开发者ID:yiwent,项目名称:Mobike,代码行数:29,代码来源:PersistentCookieStore.java

示例8: onPrepareOptionsMenu

import android.content.SharedPreferences; //导入依赖的package包/类
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    Menu nv = navigationView.getMenu();
    MenuItem mloginstatus = nv.findItem(R.id.nav_loginstatus);
    SharedPreferences sharedPref = getSharedPreferences("sharedPref", Context.MODE_PRIVATE);
    loggedIn = sharedPref.getBoolean("isLoggedIn", false);
    mServer = sharedPref.getString("server", null);

    if (loggedIn) {
        mloginstatus.setTitle(R.string.nav_logout);
        Log.i(TAG, "User is logged in!");
    } else {
        mloginstatus.setTitle(R.string.nav_login);
        Log.i(TAG, "User is not logged in!");
    }
    if (mServer != null) {
        setVersion(mServer);
    } else {
        setVersion("http://hampager.de:8080");
        //if mServer == null
        // setVersion("http://dapnet.db0sda.ampr.org:8080")
    }

    return true;
}
 
开发者ID:DecentralizedAmateurPagingNetwork,项目名称:DAPNETApp,代码行数:27,代码来源:MainActivity.java

示例9: setPersonGroupName

import android.content.SharedPreferences; //导入依赖的package包/类
public static void setPersonGroupName(String personGroupIdToAdd, String personGroupName, Context context) {
    SharedPreferences personGroupIdNameMap =
            context.getSharedPreferences("PersonGroupIdNameMap", Context.MODE_PRIVATE);

    SharedPreferences.Editor personGroupIdNameMapEditor = personGroupIdNameMap.edit();
    personGroupIdNameMapEditor.putString(personGroupIdToAdd, personGroupName);
    personGroupIdNameMapEditor.commit();

    Set<String> personGroupIds = getAllPersonGroupIds(context);
    Set<String> newPersonGroupIds = new HashSet<>();
    for (String personGroupId: personGroupIds) {
        newPersonGroupIds.add(personGroupId);
    }
    newPersonGroupIds.add(personGroupIdToAdd);
    SharedPreferences personGroupIdSet =
            context.getSharedPreferences("PersonGroupIdSet", Context.MODE_PRIVATE);
    SharedPreferences.Editor personGroupIdSetEditor = personGroupIdSet.edit();
    personGroupIdSetEditor.putStringSet("PersonGroupIdSet", newPersonGroupIds);
    personGroupIdSetEditor.commit();
}
 
开发者ID:interritus1996,项目名称:memento-app,代码行数:21,代码来源:StorageHelper.java

示例10: postInfo

import android.content.SharedPreferences; //导入依赖的package包/类
private void postInfo() {
    if (mAuthTask != null) {
        return;
    }
    showProgress(true);
    String payPath = "";
    RadioButton zhifubao = (RadioButton)findViewById(R.id.zhifubao);
    RadioButton wechat = (RadioButton)findViewById(R.id.wechat);
    if (zhifubao.isChecked()){
        payPath = "zhifubao";
    } else if (wechat.isChecked()) {
        payPath="wechat";
    }
    SharedPreferences sp = getSharedPreferences("now_account", Context.MODE_PRIVATE);
    stuNum=sp.getString("now_stu_num",null);
    Bundle bundle = getIntent().getExtras();
    mAuthTask = new PostTask(bundle.getString("money"),bundle.getString("name"),
            bundle.getString("phone"),bundle.getString("num"),bundle.getString("packsort"),
            bundle.getString("pickupplace"),bundle.getString("delieverplace"),
            bundle.getString("pickuptime"),bundle.getString("delievertime"),
            payPath,bundle.getString("remark"),stuNum,bundle.getString("securitymoney"));
    mAuthTask.execute((Void) null);

}
 
开发者ID:Luodian,项目名称:Shared-Route,代码行数:25,代码来源:PayBillActivity.java

示例11: onCreate

import android.content.SharedPreferences; //导入依赖的package包/类
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Define the settings file
    getPreferenceManager().setSharedPreferencesName(MainActivity.PREFERENCES);
    getPreferenceManager().setSharedPreferencesMode(Context.MODE_PRIVATE);

    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences);

    // Set the summary for the diary reminder time
    SharedPreferences sharedPreferences = getActivity()
            .getSharedPreferences(MainActivity.PREFERENCES, Context.MODE_PRIVATE);
    setDiaryReminderTimeSummary(sharedPreferences);

    // Prepare alarm
    Intent intent = new Intent(getActivity(), AlarmHandler.class);
    intent.setAction(AlarmHandler.DIARY_ALERT);
    mAlarmIntent = PendingIntent.getBroadcast(getActivity(), 0, intent, 0);
    mAlarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);

    mReceiver = new ComponentName(getActivity(), BootHandler.class);
    mPackageManager = getActivity().getPackageManager();
}
 
开发者ID:jgevans,项目名称:TherapyGuide,代码行数:26,代码来源:SettingsFragment.java

示例12: Save

import android.content.SharedPreferences; //导入依赖的package包/类
/**
 * Saves the pedometer state to shared preferences.
 */
@SimpleFunction(description = "Saves the pedometer state to the phone. Permits " +
  "permits accumulation of steps and distance between invocations of an App that uses " +
  "the pedometer. Different Apps will have their own saved state.")
public void Save() {
  // Store preferences
  SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
  SharedPreferences.Editor editor = settings.edit();
  editor.putFloat("Pedometer.stridelength", strideLength);
  editor.putFloat("Pedometer.distance", totalDistance);
  editor.putInt("Pedometer.prevStepCount", numStepsRaw);
  if (pedometerPaused) {
    editor.putLong("Pedometer.clockTime", prevStopClockTime);
  } else {
    editor.putLong("Pedometer.clockTime", prevStopClockTime +
        (System.currentTimeMillis() - startTime));
  }
  editor.putLong("Pedometer.closeTime", System.currentTimeMillis());
  editor.commit();
  Log.d(TAG, "Pedometer state saved.");
}
 
开发者ID:mit-cml,项目名称:appinventor-extensions,代码行数:24,代码来源:Pedometer.java

示例13: recreateAgents

import android.content.SharedPreferences; //导入依赖的package包/类
private void recreateAgents(SharedPreferences settings) {
    Message request = Message.obtain();
    request.what = Environment.RECREATE_AGENTS;
    request.replyTo = replyMessenger;

    Bundle bundle = new Bundle();

    HashSet<String> agentNamesFromActivity = (HashSet<String>)
            settings.getStringSet(ACTIVITY_NAME, new HashSet<String>());
    bundle.putSerializable(AGENT_NAMES, agentNamesFromActivity);

    request.setData(bundle);

    try {
        requestMessenger.send(request);
    } catch (RemoteException ex) {
        ex.printStackTrace();
    }
}
 
开发者ID:kflauri2312lffds,项目名称:Android_watch_magpie,代码行数:20,代码来源:MagpieActivity.java

示例14: appLaunched

import android.content.SharedPreferences; //导入依赖的package包/类
public void appLaunched(Context context) {
    SharedPreferences preferences = context.getSharedPreferences("MyBPreferences", Context.MODE_PRIVATE);
    if(preferences.getBoolean("disabled", false)) {
        return;
    }

    SharedPreferences.Editor editor = preferences.edit();

    long launchCount = preferences.getLong("launchCount", 0) + 1;
    editor.putLong("launchCount", launchCount);

    long firstLaunchDate = preferences.getLong("firstLaunchDate", 0);
    if(firstLaunchDate == 0) {
        firstLaunchDate = System.currentTimeMillis();
        editor.putLong("firstLaunchDate", firstLaunchDate);
    }

    if(launchCount >= LAUNCHES_UNTIL_PROMPT) {
        if(System.currentTimeMillis() >= firstLaunchDate + (DAYS_UNTIL_PROMPT * 24 * 60 * 60 * 1000)) {
            showRateDialog(context, editor);
        }
    }

    editor.commit();
}
 
开发者ID:kerick-jeff,项目名称:MyBP,代码行数:26,代码来源:RateApp.java

示例15: startPushService

import android.content.SharedPreferences; //导入依赖的package包/类
public static void startPushService() {
    SharedPreferences preferences = applicationContext.getSharedPreferences("Notifications", MODE_PRIVATE);

    if (preferences.getBoolean("pushService", true)) {
        applicationContext.startService(new Intent(applicationContext, NotificationsService.class));
    } else {
        stopPushService();
    }
}
 
开发者ID:MLNO,项目名称:airgram,代码行数:10,代码来源:ApplicationLoader.java


注:本文中的android.content.SharedPreferences类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。