gpt4 book ai didi

java - 我如何选择并杀死多个应用程序

转载 作者:行者123 更新时间:2023-11-29 22:14:12 24 4
gpt4 key购买 nike

我是android的新手,我的论文就像android中的“电池医生救星”。我的第一个问题是选择多个应用程序并立即将其终止。老实说,我已经遇到了第一个问题,但我的问题是我一次只能终止一个应用程序。这是我的代码,感谢您对我的问题的即时回复:)。

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;
}

}
}

最佳答案

创建 onButton 单击您的终止按钮。从 ListView 中收集所有选定的按钮及其位置索引,然后使用这些 ID 获取服务名称并杀死它们。猜猜这很容易。虽然我不想成为一个明显的船长 :)

我不认为你完全理解里面的东西是如何工作的所以我不得不调整你的代码相当不错 我删除了 BroadcastReceiver 不要浪费你可以玩的时间稍后再说。

将以下行添加到您的 AndroidManifest:

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

和代码:

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;
}

}
}

关于java - 我如何选择并杀死多个应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8851829/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com