Android小功能集
jopen
11年前
避免我们的应用出现在最近任务栏(注:不是任务管理器)中出现
<!-- 添加属性 --> android:excludeFromRecents="true"
获取手机上的安装的所有Home类别应用
// private List<String> getHomes() { // List<String> packages = new ArrayList<String>(); // PackageManager packageManager = mservice.getPackageManager(); // Intent intent = new Intent(Intent.ACTION_MAIN); // intent.addCategory(Intent.CATEGORY_HOME); // List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent, // PackageManager.MATCH_DEFAULT_ONLY); // for(ResolveInfo info : resolveInfo){ // packages.add(info.activityInfo.packageName); // } // return packages; // }
监听Home键,有需要的时候可以用用
HomeKeyEventBroadCastReceiver receiver = new HomeKeyEventBroadCastReceiver(); registerReceiver(receiver, new IntentFilter( Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
public class HomeKeyEventBroadCastReceiver extends BroadcastReceiver{ static final String TAG = "HomeKeyEventBroadCastReceiver"; static final String SYSTEM_REASON = "reason"; static final String SYSTEM_HOME_KEY = "homekey";//home key static final String SYSTEM_RECENT_APPS = "recentapps";//long home key @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) { String reason = intent.getStringExtra(SYSTEM_REASON); if (reason != null) { if (reason.equals(SYSTEM_HOME_KEY)) { // home key处理点 String pk = getCurrentPk(context); Log.i(TAG, "pk:"+pk); Log.i(TAG, "HOME KEY"); } else if (reason.equals(SYSTEM_RECENT_APPS)) { // long home key处理点 Log.i(TAG, "LONG HOME KEY"); } } } } private String getCurrentPk(Context context){ ActivityManager mActivityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> rti = mActivityManager.getRunningTasks(1); return rti.get(0).topActivity.getPackageName(); } }
在新版本中创建一个Receiver接收手机应用的安装和卸载(可以监听到旧版本的卸载)
public class PkInstallReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED" )) { String packageName = intent.getDataString().substring(8); System. out.println( "安装:" +packageName + "包名的程序" ); } //接收卸载广播 if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED" )) { String packageName = intent.getDataString().substring(8); System. out.println( "卸载:" + packageName + "包名的程序" ); Intent newIntent = new Intent(); newIntent.setClassName(packageName,packageName+ ".AutoStartProTestActivity" ); newIntent.setAction( "android.intent.action.MAIN"); newIntent.addCategory( "android.intent.category.LAUNCHER" ); newIntent.setFlags(Intent. FLAG_ACTIVITY_NEW_TASK); context.startActivity(newIntent); } } } AndroidManifest.xml进行配置 //receiver <receiver android:name="com.justsy.lpi.receiver.PkInstallReceiver" android:label=" @string/app_name"> <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED" /> <action android:name="android.intent.action.PACKAGE_REMOVED" /> <data android:scheme="package" /> </intent-filter> </receiver> //permission <uses-permission android:name= "android.permission.RESTART_PACKAGES" /> <uses-permission android:name= "android.permission.RECEIVE_BOOT_COMPLETED" />