本文介绍了如何选择和杀灭多种应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android的一个新手,我的观点是一样的电池医生节电器的机器人。我的电话号码1的问题是选择多个应用程序,并杀死它一次。老实说,我已经有了我的第一问题,但我的问题是,我只能杀一次一个应用程序。所以,这里是我的code感谢您对我的问题立即作出反应:)

 包plm.thesis.taskkiller;


公共类TaskKillerActivity扩展ListActivity {

    私有静态最后字符串变量=TaskKillerActivity;
    TaskListAdapter适配器;
    RefreshReceiver接收器;

    类RefreshReceiver扩展的BroadcastReceiver {
        私有静态最后字符串变量=RefreshReceiver;

        @覆盖
        公共无效的onReceive(上下文的背景下,意图意图){
            Log.d(TAG,提神);
            clearRunningProcessesList();
            loadRunningProcesses();

        }

    }

    公共无效clearRunningProcessesList(){
        adapter.clearTasks();
    }

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        适配器=新TaskListAdapter(this.getApplicationContext());
        setListAdapter(适配器);
        loadRunningProcesses();
        接收器=新RefreshReceiver();
        getApplicationContext()。registerReceiver(接收器,
                新的IntentFilter(plm.thesis.taskkiller.REFRESH_RECEIVER));
    }

    @覆盖
    保护无效的onDestroy(){
        super.onDestroy();
        。getApplicationContext()unregisterReceiver(接收器);
    }

    公共无效loadRunningProcesses(){

        ActivityManager activityManager =(ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
        名单< RunningAppProcessInfo> appinfolist = activityManager
                .getRunningAppProcesses();

        Log.d(TAG,AppInfoList尺寸:+ appinfolist.size());

        对于(RunningAppProcessInfo runningAppProcessInfo:appinfolist){
            TaskObject runningtask =新TaskObject();
            runningtask.pid = runningAppProcessInfo.pid;
            runningtask.processName = runningAppProcessInfo.processName;
            adapter.addTask(runningtask);
        }
    }

    类TaskObject {
        INT PID;
        字符串processName;
    }

    类TaskListAdapter扩展了BaseAdapter {

        私有静态最后字符串变量=TaskListAdapter;

        ArrayList的< TaskKillerActivity.TaskObject>清单;
        上下文语境;

        公共TaskListAdapter(上下文的背景下){
            Log.d(TAG,创造了新的任务列表适配器);
            this.context =背景;
            如果(表== NULL){
                名单=新的ArrayList< TaskKillerActivity.TaskObject>();
            }
        }

        公共无效addTask(TaskObject taskObject){
            list.add(taskObject);
        }

        公共无效clearTasks(){
            list.clear();
            Log.d(TAG,列表的大小:+则为list.size());
            this.notifyDataSetChanged();
        }

        公众诠释getCount将(){
            返回则为list.size();
        }

        公共TaskObject的getItem(INT位置){
            返回list.get(位置);
        }

        众长getItemId(INT位置){
            返回的位置;
        }

        公共查看getView(INT位置,查看convertView,ViewGroup中父){
            LayoutInflater充气= LayoutInflater.from(上下文);
            查看查看= inflater.inflate(R.layout.taskitem,NULL);
            TextView的textPid =(TextView中)view.findViewById(R.id.taskPid);
            textPid.setText(Integer.toString(的getItem(位置).pid));
            TextView的textName =(TextView中)view.findViewById(R.id.taskName);
            textName.setText(的getItem(位置).processName);
            按钮buttonKill =(按钮)view.findViewById(R.id.taskKill);
            buttonKill.setTag(的getItem(位置).processName);
            复选框chckKill =(复选框)view.findViewById(R.id.appSelected);
            chckKill.setTag(的getItem(位置).pid);

            view.findViewById(R.id.taskKill).setOnClickListener(
                    新OnClickListener(){
                        公共无效的onClick(视图v){
                            ActivityManager activityManager =(ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
                            Log.d(TAG,杀人的过程:+ v.getTag());
                            activityManager.kil​​lBackgroundProcesses((字符串)V
                                    .getTag());
                            context.sendBroadcast(新意图(
                                    plm.thesis.taskkiller.REFRESH_RECEIVER));

                        }
                    });

            返回查看;
        }

    }
}
 

解决方案

创建onButton点击你杀按钮。从收集的ListView的所有选择的按钮及其位置索引然后得到使用这些ID的服务的名称,并杀死他们。猜猜它很容易,因为它是。我并不想成为一个队长,虽然很明显:)

我不认为你完全了解的东西的作品在那里,所以我不得不以调整您的code相当升技我删除了的BroadcastReceiver不要浪费时间,你可以玩它以后。

添加到您的Andr​​oidManifest以下行:

 <使用-权限的Andr​​oid:名称=android.permission.KILL_BACKGROUND_PROCESSES/>
 

和code:

 进口android.app.Activity;
进口android.app.ActivityManager;
进口android.content.Context;

进口android.os.Bundle;
进口android.util.Log;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget *。

进口的java.util.ArrayList;
进口的java.util.List;

公共类TaskKillerActivity延伸活动{

    私有静态最后字符串变量=TaskKillerActivity;
    TaskListAdapter适配器;
    ListView的LV;
 // RefreshReceiver接收器;
//
//类RefreshReceiver扩展的BroadcastReceiver {
//私有静态最后字符串变量=RefreshReceiver;
//
// @覆盖
//公共无效的onReceive(上下文的背景下,意图意图){
// Log.d(TAG,提神);
// clearRunningProcessesList();
// loadRunningProcesses();
//
//}
//
//}

    公共无效clearRunningProcessesList(){
        adapter.clearTasks();
    }

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        最后ActivityManager activityManager =(ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
       //适配器=新TaskListAdapter(本);
        的LinearLayout LL =新的LinearLayout(本);
       LinearLayout.LayoutParams LP1 =新LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        ll.setLayoutParams(LP1);
        ll.setOrientation(1);
         LV =新的ListView(本);
        适配器=新TaskListAdapter(本);
        loadRunningProcesses();

        lv.setAdapter(适配器);


        按钮btnKill =新的按钮(这一点);
        btnKill.setText(杀任务);

        btnKill.setOnClickListener(新View.OnClickListener(){

            公共无效的onClick(视图v){
                //listItem.setBackgroundColor(Color.BLACK);
                对于(TaskObject到:getTasksToKill())
                {
                   activityManager.kil​​lBackgroundProcesses(to.getProcessName());

                }
           adapter.clearTasks();
           loadRunningProcesses();
           lv.setAdapter(适配器);
                // TODO自动生成方法存根

            }
        });
        ll.addView(btnKill);
        ll.addView(LV);
        的setContentView(Ⅱ);


    }

    私人列表< TaskObject> getTasksToKill()
    {
        名单< TaskObject> TOL =新的ArrayList< TaskObject>();

        的for(int i = 0; I< adapter.getCount();我++)
        {
            // RelativeLayout的RL =(RelativeLayout的)lv.getChildAt(ⅰ);

            TaskObject为= adapter.getItem(我);
            //复选框CB =(复选框)rl.getChildAt(0);
            如果(to.isToKill())
            {
                tol.add(到);
            }
        }

        返回TOL;
    }


    @覆盖
    保护无效的onDestroy(){
        super.onDestroy();
       // getApplicationContext()unregisterReceiver(接收器)。
    }

    公共无效loadRunningProcesses(){

        ActivityManager activityManager =(ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
        名单< ActivityManager.RunningAppProcessInfo> appinfolist = activityManager
                .getRunningAppProcesses();

        Log.d(TAG,AppInfoList尺寸:+ appinfolist.size());

        对于(ActivityManager.RunningAppProcessInfo runningAppProcessInfo:appinfolist){
            TaskObject runningtask =新TaskObject();
            runningtask.setPid(runningAppProcessInfo.pid);
            runningtask.setProcessName(runningAppProcessInfo.processName);
            adapter.addTask(runningtask);
        }
    }

    类TaskObject {
        私人诠释PID;
        私人字符串processName;
        私人布尔toKill;

        公众诠释GETPID(){
            返回PID;
        }

        公共无效setPid(INT PID){
            this.pid = PID;
        }

        公共字符串getProcessName(){
            返回processName;
        }

        公共无效setProcessName(字符串processName){
            this.processName = processName;
        }

        公共布尔isToKill(){
            返回toKill;
        }

        公共无效setToKill(布尔toKill){
            this.toKill = toKill;
        }
    }

    类TaskListAdapter扩展了BaseAdapter {

        私有静态最后字符串变量=TaskListAdapter;

        ArrayList的< TaskObject>清单;
        上下文语境;

        公共TaskListAdapter(上下文的背景下){
            Log.d(TAG,创造了新的任务列表适配器);
            this.context =背景;
            如果(表== NULL){
                名单=新的ArrayList< TaskKillerActivity.TaskObject>();
            }
        }

        公共无效addTask(TaskObject taskObject){
            list.add(taskObject);
        }

        公共无效clearTasks(){
            list.clear();
            Log.d(TAG,列表的大小:+则为list.size());
            this.notifyDataSetChanged();
        }

        公众诠释getCount将(){
            返回则为list.size();
        }

        公共TaskObject的getItem(INT位置){
            返回list.get(位置);
        }

        众长getItemId(INT位置){
            返回的位置;
        }

        公共查看getView(最终诠释的立场,观点convertView,ViewGroup中父){
           // LayoutInflater充气= LayoutInflater.from(上下文);
           //视图中查看= inflater.inflate(R.layout.taskitem,NULL);
            RelativeLayout的RL =新RelativeLayout的(上下文);
            TextView的textPid =新的TextView(上下文);
            textPid.setId(222222);
            textPid.setText(Integer.toString(的getItem(位置).getPid()));

            TextView的textName =新的TextView(上下文);
            textName.setId(333333);
            textName.setText(的getItem(位置).getProcessName());

            复选框chckKill =新的复选框(上下文);
            chckKill.setOnClickListener(新View.OnClickListener(){

      公共无效的onClick(视图v){
                //是chkIos检查?
        如果(((复选框)ⅴ).isChecked()){
            的getItem(位置).setToKill(真正的);
        }

      }
    });


            chckKill.setTag(的getItem(位置).getPid());
            chckKill.setId(11111111);

            RelativeLayout.LayoutParams LP2 =新RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);

             rl.addView(chckKill,LP2);


            RelativeLayout.LayoutParams LP =新RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
            lp.addRule(RelativeLayout.RIGHT_OF,chckKill.getId());
            lp.setMargins(25,0,0,0);
            rl.addView(textName,LP);

            RelativeLayout.LayoutParams LP1 =新RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
            lp1.addRule(RelativeLayout.RIGHT_OF,chckKill.getId());
            lp1.setMargins(25,20,0,0);
            rl.addView(textPid,LP1);


            返回RL;
        }

    }
}
 

I'm a newbie in android and my thesis is just like the "Battery Doctor Saver" in android. My number 1 problem is to select multiple application and kill it at once. Honestly I already have my no.1 problem but my problem is I can only kill one application at a time. So here is my code thank you for your immediate response to my question :).

package plm.thesis.taskkiller;


public class TaskKillerActivity extends ListActivity {

    private static final String TAG = "TaskKillerActivity";
    TaskListAdapter adapter;
    RefreshReceiver receiver;

    class RefreshReceiver extends BroadcastReceiver {
        private static final String TAG = "RefreshReceiver";

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(TAG, "refreshing");
            clearRunningProcessesList();
            loadRunningProcesses();

        }

    }

    public void clearRunningProcessesList() {
        adapter.clearTasks();
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        adapter = new TaskListAdapter(this.getApplicationContext());
        setListAdapter(adapter);
        loadRunningProcesses();
        receiver = new RefreshReceiver();
        getApplicationContext().registerReceiver(receiver,
                new IntentFilter("plm.thesis.taskkiller.REFRESH_RECEIVER"));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getApplicationContext().unregisterReceiver(receiver);
    }

    public void loadRunningProcesses() {

        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningAppProcessInfo> appinfolist = activityManager
                .getRunningAppProcesses();

        Log.d(TAG, "AppInfoList Size: " + appinfolist.size());

        for (RunningAppProcessInfo runningAppProcessInfo : appinfolist) {
            TaskObject runningtask = new TaskObject();
            runningtask.pid = runningAppProcessInfo.pid;
            runningtask.processName = runningAppProcessInfo.processName;
            adapter.addTask(runningtask);
        }
    }

    class TaskObject {
        int pid;
        String processName;
    }

    class TaskListAdapter extends BaseAdapter {

        private static final String TAG = "TaskListAdapter";

        ArrayList<TaskKillerActivity.TaskObject> list;
        Context context;

        public TaskListAdapter(Context context) {
            Log.d(TAG, "created new task list adapter");
            this.context = context;
            if (list == null) {
                list = new ArrayList<TaskKillerActivity.TaskObject>();
            }
        }

        public void addTask(TaskObject taskObject) {
            list.add(taskObject);
        }

        public void clearTasks() {
            list.clear();
            Log.d(TAG, "list size:" + list.size());
            this.notifyDataSetChanged();
        }

        public int getCount() {
            return list.size();
        }

        public TaskObject getItem(int position) {
            return list.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = LayoutInflater.from(context);
            View view = inflater.inflate(R.layout.taskitem, null);
            TextView textPid = (TextView) view.findViewById(R.id.taskPid);
            textPid.setText(Integer.toString(getItem(position).pid));
            TextView textName = (TextView) view.findViewById(R.id.taskName);
            textName.setText(getItem(position).processName);
            Button buttonKill = (Button) view.findViewById(R.id.taskKill);
            buttonKill.setTag(getItem(position).processName);
            CheckBox chckKill = (CheckBox) view.findViewById(R.id.appSelected);
            chckKill.setTag(getItem(position).pid);

            view.findViewById(R.id.taskKill).setOnClickListener(
                    new OnClickListener() {
                        public void onClick(View v) {
                            ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
                            Log.d(TAG, "killing process: " + v.getTag());
                            activityManager.killBackgroundProcesses((String) v
                                    .getTag());
                            context.sendBroadcast(new Intent(
                                    "plm.thesis.taskkiller.REFRESH_RECEIVER"));

                        }
                    });

            return view;
        }

    }
}
解决方案

Create onButton click on your kill button. Gather all selected buttons from your ListView with their position index then get the service names using these IDs and kill them. Guess it's easy as it is. I don't want to be a captain obvious though :)

I don't think you entirely understand how the stuff works in there so I had to ajustyour code quite abit I removed the BroadcastReceiver not to lose time you can play with it later.

Add to your AndroidManifest following line:

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

and the code:

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import java.util.ArrayList;
import java.util.List;

public class TaskKillerActivity extends Activity {

    private static final String TAG = "TaskKillerActivity";
    TaskListAdapter adapter;
    ListView lv;
 //   RefreshReceiver receiver;
//
//    class RefreshReceiver extends BroadcastReceiver {
//        private static final String TAG = "RefreshReceiver";
//
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            Log.d(TAG, "refreshing");
//            clearRunningProcessesList();
//            loadRunningProcesses();
//
//        }
//
//    }

    public void clearRunningProcessesList() {
        adapter.clearTasks();
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
       // adapter = new TaskListAdapter(this);
        LinearLayout ll = new LinearLayout(this);
       LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        ll.setLayoutParams(lp1);
        ll.setOrientation(1);
         lv = new ListView(this);
        adapter = new TaskListAdapter(this);
        loadRunningProcesses();

        lv.setAdapter(adapter);


        Button btnKill = new Button(this);
        btnKill.setText("kill task");

        btnKill.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                //listItem.setBackgroundColor(Color.BLACK);
                for(TaskObject to :getTasksToKill())
                {
                   activityManager.killBackgroundProcesses(to.getProcessName());

                }
           adapter.clearTasks();
           loadRunningProcesses();
           lv.setAdapter(adapter);
                // TODO Auto-generated method stub

            }
        });
        ll.addView(btnKill);
        ll.addView(lv);
        setContentView(ll);


    }

    private List<TaskObject> getTasksToKill()
    {
        List<TaskObject> tol = new ArrayList<TaskObject>();

        for(int i =0;i<adapter.getCount();i++)
        {
            //RelativeLayout rl = (RelativeLayout)lv.getChildAt(i);

            TaskObject to = adapter.getItem(i);
            //CheckBox  cb = (CheckBox)rl.getChildAt(0);
            if(to.isToKill())
            {
                tol.add(to);
            }
        }

        return tol;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
       // getApplicationContext().unregisterReceiver(receiver);
    }

    public void loadRunningProcesses() {

        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appinfolist = activityManager
                .getRunningAppProcesses();

        Log.d(TAG, "AppInfoList Size: " + appinfolist.size());

        for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo : appinfolist) {
            TaskObject runningtask = new TaskObject();
            runningtask.setPid(runningAppProcessInfo.pid);
            runningtask.setProcessName(runningAppProcessInfo.processName);
            adapter.addTask(runningtask);
        }
    }

    class TaskObject {
        private int pid;
        private String processName;
        private boolean toKill;

        public int getPid() {
            return pid;
        }

        public void setPid(int pid) {
            this.pid = pid;
        }

        public String getProcessName() {
            return processName;
        }

        public void setProcessName(String processName) {
            this.processName = processName;
        }

        public boolean isToKill() {
            return toKill;
        }

        public void setToKill(boolean toKill) {
            this.toKill = toKill;
        }
    }

    class TaskListAdapter extends BaseAdapter {

        private static final String TAG = "TaskListAdapter";

        ArrayList<TaskObject> list;
        Context context;

        public TaskListAdapter(Context context) {
            Log.d(TAG, "created new task list adapter");
            this.context = context;
            if (list == null) {
                list = new ArrayList<TaskKillerActivity.TaskObject>();
            }
        }

        public void addTask(TaskObject taskObject) {
            list.add(taskObject);
        }

        public void clearTasks() {
            list.clear();
            Log.d(TAG, "list size:" + list.size());
            this.notifyDataSetChanged();
        }

        public int getCount() {
            return list.size();
        }

        public TaskObject getItem(int position) {
            return list.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {
           // LayoutInflater inflater = LayoutInflater.from(context);
           // View view = inflater.inflate(R.layout.taskitem, null);
            RelativeLayout rl = new RelativeLayout(context);
            TextView textPid = new TextView(context);
            textPid.setId(222222);
            textPid.setText(Integer.toString(getItem(position).getPid()));

            TextView textName = new TextView(context);
            textName.setId(333333);
            textName.setText(getItem(position).getProcessName());

            CheckBox chckKill = new CheckBox(context);
            chckKill.setOnClickListener(new View.OnClickListener() {

      public void onClick(View v) {
                //is chkIos checked?
        if (((CheckBox) v).isChecked()) {
            getItem(position).setToKill(true);
        }

      }
    });


            chckKill.setTag(getItem(position).getPid());
            chckKill.setId(11111111);

            RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);

             rl.addView(chckKill,lp2);


            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
            lp.addRule(RelativeLayout.RIGHT_OF, chckKill.getId());
            lp.setMargins(25,0,0,0);
            rl.addView(textName,lp);

            RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
            lp1.addRule(RelativeLayout.RIGHT_OF, chckKill.getId());
            lp1.setMargins(25,20,0,0);
            rl.addView(textPid,lp1);


            return rl;
        }

    }
}

这篇关于如何选择和杀灭多种应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 20:43