Android SharePreference等 操作基础类

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用

[Java]代码    

/**
 * 保存、得到SharedPreferences
 * @author leo
 *
 */
public class AppUtil {
	public static void saveString(Context paramContext, String strName, String strValue) {
		paramContext.getSharedPreferences(Constants.Share_Pre, 0).edit().putString(strName, strValue).commit();
	}

	public static void saveInt(Context paramContext, String strName, int strValue) {
		paramContext.getSharedPreferences(Constants.Share_Pre, 0).edit().putInt(strName, strValue).commit();
	}
	public static void saveBoolean(Context paramContext, String strName, Boolean strValue) {
		paramContext.getSharedPreferences(Constants.Share_Pre, 0).edit().putBoolean(strName, strValue).commit();
	}

	public static String getString(Context context, String strName, String strDefault) {
		return context.getSharedPreferences(Constants.Share_Pre, 0).getString(strName, strDefault);
	}
	
	public static void clearFile(Context context) {
		context.getSharedPreferences(Constants.Share_Pre, 0).edit().clear().commit();
	}

	public static Boolean delString(Context context, String strName) {
		return context.getSharedPreferences(Constants.Share_Pre, 0).edit().remove(strName).commit();
	}

	

	public static Intent sendSMS(String content) {
		Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"));
		intent.putExtra("sms_body", content);
		return intent;
	}
	//得到手机串码
	public static String getPhoneCode(Context c){
			TelephonyManager telephonemanage=(TelephonyManager)c.getSystemService(Context.TELEPHONY_SERVICE);		
			return telephonemanage.getDeviceId();
		}
	
	public static boolean getBoolean(Context paramContext, String strName, Boolean strValue) {
		return paramContext.getSharedPreferences(Constants.Share_Pre, 0).getBoolean(strName, strValue);
	}
	
	public static int getInt(Context context, String strName, int strDefault) {
		try {
			return context.getSharedPreferences(Constants.Share_Pre, 0).getInt(strName, strDefault);
		} catch (Exception e) {
			return 0;
		}
		
	}
	
	public static boolean isAppInstalled(Context context,String packagename) {
		final PackageManager packageManager = context.getPackageManager();
		List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
		List<String> pName = new ArrayList<String>();
		if (pinfo != null) {
			for (int i = 0; i < pinfo.size(); i++) {
				String pn = pinfo.get(i).packageName;
				pName.add(pn);
			}
		}
		return pName.contains(packagename);
	}
	
	/**
	 * Get the version number of the current program
	 * 
	 * @param context
	 * @return
	 */

	public static int getCurVersion(Context context,String packagename) {
		int curversion = 0;
		try {
			// ---get the package info---
			PackageManager pm = context.getPackageManager();
			PackageInfo pi = pm.getPackageInfo(packagename, 0);
			curversion = pi.versionCode;
			if (curversion<= 0) {
				return 0;
			}
		} catch (Exception e) {
			if (Constants.DebugMode) {
				Log.e("VersionInfo", "Exception", e);
			}

		}
		return curversion;
	}
	
	/**
	 * apps安装
	 * @param context
	 * @param filePath
	 */
	public static void install(Context context, String filePath) {
		Intent i = new Intent(Intent.ACTION_VIEW);
		i.setDataAndType(Uri.parse("file://" + filePath), "application/vnd.android.package-archive");
		i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(i);
	}
	
}

标签: 代码

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:Android EditText禁止复制粘贴

下一篇:LogUtils 对Logcat的一个封装工具类