定义
SharedPreferences preferences = getSharedPreferences("mySelectedApps", MODE_WORLD_READABLE);
使用
Editor editor = preferences.edit();
editor.putString(nameAndPackageString, labelString);//把包名和应用名存储
editor.commit();
editor.remove(nameAndPackageString); //移除
editor.commit();
editor.clear(); //清空
editor.commit();
获取已定义的SharedPreferences
SharedPreferences prefs = myContext.getSharedPreferences("mySelectedApps", Context.MODE_WORLD_READABLE);
使用
private Map<String, String> selectMap = new HashMap<String, String>();
selectMap = (Map<String, String>)prefs.getAll();//通过getAll()方法获取对应的HashMap
法一:
Iterator iter = selectMap.entrySet().iterator();
while (iter.hasNext()){
Map.Entry entry = (Map.Entry) iter.next();
item.nameAndPackage = (String) entry.getKey();
item.label = (String) entry.getValue();
}
法二:
for (Object o : tempMap.keySet()) {
System.out.println("key=" + o + " value=" + tempMap.get(o));
}
private List<ResolveInfo> mAllApps; //保存所有Apps的数据
private void bindAllApps() {
mPackageManager = getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mAllApps = mPackageManager.queryIntentActivities(mainIntent, 0);
Collections.sort(mAllApps, new ResolveInfo.DisplayNameComparator(mPackageManager));
}
获取应用程序相关信息
ResolveInfo res = mAllApps.get(i);
item.name = res.activityInfo.name; //Activity的名称
item.packageName= res.activityInfo.packageName; //包名
item.appIcon = res.loadIcon(mPackageManager); //图标
item.label = (String) res.loadLabel(mPackageManager);//应用名
由包名获取应用的图标
ApplicationInfo applicationInfo = null;
try {
applicationInfo = pm.getApplicationInfo(item.packageName, PackageManager.GET_META_DATA);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
if(applicationInfo != null)
item.appIcon = pm.getApplicationIcon(applicationInfo);
ps:一个app里面有可能有几个Main属性的Activity,就像拨号与联系人,它们图标和名称都不一样,但其实是属于一个app,他们包名一样,如果我们用读取main的方法来读取所有的app,那么就可以读取出多个。这多个其实是一个app,它们的AndroidManifest.xml不同,作为了不同的入口。
普通调用另一个Activity
Intent intent = new Intent(MainActivity.this, Game.class);
startActivity(intent);
包名+Activity名的方式调用
ComponentName componentName = new ComponentName(i.packageName, i.name);
Intent ii = new Intent();
ii.setComponent(componentName);
startActivity(ii);
单由包名调用
Intent ii = getPackageManager().getLaunchIntentForPackage(i.packageName);
startActivity(ii);
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
filter.addDataScheme("package");
registerReceiver(myAppReceiver, filter);
定义广播
public class AppInstallReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
PackageManager manager = context.getPackageManager();
String PkgName = intent.getData().getSchemeSpecificPart();;
if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
Toast.makeText(context, "安装成功"+PkgName, Toast.LENGTH_LONG).show();
}
else if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) {
Toast.makeText(context, "卸载成功"+ PkgName, Toast.LENGTH_LONG).show();
}
else if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
Toast.makeText(context, "替换成功"+PkgName, Toast.LENGTH_LONG).show();
}
}
}
注销广播在onDestroy()中
unregisterReceiver(myAppReceiver);
public void onReceive( Context context, Intent intent ) {ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo activeNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);NetworkInfo mWifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);Log.v("检测网络", " ");//判断是否连上有线网络if (activeNetInfo != null && activeNetInfo.isConnected()){if(firsWebtFlage != 0 && intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION))Toast.makeText(context, "连上有线网络" + firsWebtFlage, Toast.LENGTH_LONG).show();elsefirsWebtFlage++;webButton.setSelected(true);}elsewebButton.setSelected(false);//判断是否连上WiFiif( mWifi != null && mWifi.isConnected()){if(firstWifiFlage != 0 && intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION))Toast.makeText(context, "连上WiFi" + firsWebtFlage, Toast.LENGTH_LONG).show();elsefirstWifiFlage++;wifiButton.setSelected(true);}elsewifiButton.setSelected(false);}
<resources><array name="difficulty"><item>@string/easy_label</item><item>@string/medium_label</item><item>@string/hard_label</item></array></resources>