杀死后台流程问题

时间:2014-04-27 16:52:26

标签: android kill-process

我正在使用此代码来杀死所有后台进程在按钮上单击,但问题有时它不起作用,即不会杀死任何应用程序,所以还有其他方法吗?日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"/> 

1 个答案:

答案 0 :(得分:0)

你写了

finally {
     finish();
}
  

应该完成活动。