简体   繁体   English

杀死后台进程问题

[英]Killing Background processes issue

I am using this code to kill all the background processes On the button click, but the problem is sometimes it does not work ie does not kill any application so is there any other way of doing it? 我正在使用此代码来杀死所有后台进程。单击按钮后,问题是有时它不起作用,即不杀死任何应用程序,所以还有其他方法可以这样做吗? Thnx 日Thnx

package com.example.api;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.os.Process;

import java.util.List;

public class MainActivity extends Activity {

    Button theButton;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        theButton = (Button) findViewById(R.id.button);
        theButton.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v)
            {
                try {

                List<ApplicationInfo> packages;
                PackageManager pm;
                pm = getPackageManager();
                packages = pm.getInstalledApplications(150);
                ActivityManager mActivityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);

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

                }
                } finally {
               // finish();
                }

            }
        });

    }

}

<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/> 

And you wrote 你写了

finally {
     finish();
}

That is supposed to finish the activity. 那应该完成了活动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM