我是android的新手,我的论文就像android的“ Battery Doctor
Saver”一样。我的第一个问题是选择多个应用程序并立即将其杀死。老实说,我已经遇到了第一大问题,但是我的问题是我一次只能杀死一个应用程序。因此,这是我的代码,谢谢您对我的问题的立即答复:)。
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,然后单击您的kill按钮。从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;
}
}
}
问题内容: 我有以下设置: 我正在创建黄瓜步骤,我需要能够启动和关闭该应用程序。 问题答案: 您可以使用和频道杀死无限循环! 很不错的Go交换,不是吗? 现在,这有什么奇怪的地方?这是一个零尺寸的频道。我们只能用空结构填充它(即:)。可能是a 或其他,因为我们不使用频道的内容。重要的是,我们使用通道通知goroutine中的无限循环,该该停止了。 该语句用于捕获来自渠道的内容。这是一条阻塞语句(除
我使用startActivity(Intent)在我的Android应用程序中创建一个新活动。 有可能扼杀其中一项活动吗?如果有,如何扼杀? 我想要这样 Activity4->Activity1->关闭应用程序
本文向大家介绍请问,如何杀死一个进程?相关面试题,主要包含被问及请问,如何杀死一个进程?时的应答技巧和注意事项,需要的朋友参考一下 考察点:进程 Kill pid
问题内容: 我在前台启动了我的程序(守护程序),然后用杀死了它,但剩下一个僵尸,无法用杀死它。如何杀死僵尸进程? 如果僵尸是一个死进程(已被杀死),我如何将其从输出中删除? 问题答案: 僵尸已经死了,所以您无法杀死它。要清理僵尸,必须等待其父级等待,因此杀死父级应该可以消除僵尸。(父对象死后,僵尸将被pid 1继承,而pid 1将等待该僵尸并清除其在进程表中的条目。)如果守护程序正在生成成为僵尸的
问题内容: 问题答案: 调用stop的替代方法是使用中断向线程发出信号,告知你希望它完成其工作。(这假设你要停止的线程行为良好,如果它在抛出异常后立即通过吃掉它们而忽略了InterruptedException,并且不检查中断状态,那么你将返回使用stop()。) 下面是一些代码,我写的一个答案,一个线程的问题在这里,它的线程中断,将如何工作的例子: 要注意的一些事情: 中断原因并立即抛出,否则你
我有一个通过FCM数据消息触发来电的应用程序(优先级:高,没有通知有效载荷)。当app处于前台或后台时,app接收呼叫。对于上述情况,在锁定屏幕中接收通知(来电)。 但由于某种原因,当关闭应用程序或从多任务托盘中刷卡时,电话就不再接到了。我认为服务被扼杀了。我能做什么? 工作流当收到FCM通知时,一个后台服务启动,然后该服务将自身更改为具有“来电”通知的前台服务。我也在使用全屏意图。下层是我用来服