Java android kill apk和后台服务

时间:2017-08-09 09:37:37

标签: java android

我想杀死我的apk和后台服务我尝试这样做:

 finishAffinity();
                   ActivityManager am = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
                    am.killBackgroundProcesses("pl.eltegps.inwentaryzja.offline");

                int pid = Process.myPid();
                Process.killProcess(pid);
                System.exit(1);

但它不起作用。当我检查我如何最小化apk时,我看到我的apk

2 个答案:

答案 0 :(得分:0)

List<ApplicationInfo> packages;
    PackageManager pm;
    pm = getPackageManager();
    //get a list of installed apps.
    packages = pm.getInstalledApplications(0);

    ActivityManager mActivityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);

   for (ApplicationInfo packageInfo : packages) {
        if((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM)==1)continue;
        if(packageInfo.packageName.equals("mypackage")) continue;
        mActivityManager.killBackgroundProcesses(packageInfo.packageName);
   } 

答案 1 :(得分:0)

我做了这个并且有效:

Intent i = new Intent(LFRFIDActivity.this, SplashScreen.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    i.putExtra("isExitAction", true);
                    startActivity(i);

并在启动活动中执行此操作:

 @Override
    protected void onResume() {
        super.onResume();
        try {
            Intent i = getIntent();
            Boolean isExit = i.getBooleanExtra("isExitAction",false);
            if(isExit){
                this.finish();
            }
        }
        catch (Exception e){}
    }