安卓应用运行、卸载、清除数据、强制停止

IntentFilter appFilter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);//安装广播
appFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);//卸载的广播
appFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);//升级的广播
appFilter.addDataScheme(“package”);

  • 1、卸载应用

        Uri packageURI = Uri.parse("package:" + packageName);
        Intent intent = new Intent(Intent.ACTION_DELETE, packageURI);
        this.startActivity(intent);
    
  • 2.运行

    PackageManager pm = getPackageManager();
    //可以设置启动的方式,这里无设置
    Intent intent = pm.getLaunchIntentForPackage(resolveInfo.activityInfo.packageName);
    startActivity(intent);
    
  • 3.强制停止

    mAm = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    
    ResolveInfo resolveInfo = (ResolveInfo) apksListAdapter.getItem(mSelectedPosition);
    mAm.forceStopPackage(resolveInfo.activityInfo.packageName);
    
  • 4.清除数据

     String packageName = resolveInfo.activityInfo.packageName;
     boolean res = mAm.clearApplicationUserData(packageName,        mClearDataObserver);
      mPackageManager.deleteApplicationCacheFiles(packageName, null);
    mPackageManager.clearPackagePreferredActivities(packageName);
    
    class ClearUserDataObserver extends IPackageDataObserver.Stub {
        public void onRemoveCompleted(final String packageName, final boolean succeeded) {

            if(succeeded){
                  
                Message message = new Message();
                message.what = 0x110;
                clearDataHandler.sendMessage(message);
            }
            //清除数据完毕后可以更新相关的UI
        }
    }

你可能感兴趣的:(Android进阶)