Android并没有提供提供任务管理器程序,无从得知后台有哪些在运行。本范例是通过ActivityManager.getRunningTasks方法来取得正在运行中的工作程序,并使用ListView来显示。
具体程序如下:
package com.nmobile.allove.wfp;
import java.util.ArrayList;
import java.util.List;
import android.app.ActivityManager;
import android.app.ListActivity;
import android.app.Service;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class GetRunningTaskInfo extends ListActivity implements OnClickListener{
private Button mButton;
ArrayList<String> appTaskList;
ActivityManager mActivityManager;
private int intGetTaskCounter=10;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.Init();
mButton.setOnClickListener(this);
}
public void Init()
{
mButton=(Button)this.findViewById(R.id.Button01);
mActivityManager=(ActivityManager) this.getSystemService(Service.ACTIVITY_SERVICE);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.Button01:
appTaskList=new ArrayList<String>();
List<ActivityManager.RunningTaskInfo> list=mActivityManager.getRunningTasks(intGetTaskCounter);
int i=1;
for(ActivityManager.RunningTaskInfo task:list)
{
appTaskList.add(""+(i++)+": "+task.baseActivity.getClassName()+"(ID="+task.id+")");
}
if(appTaskList.size()==0)
{
Toast.makeText(this, "appTaskList.size()==0", Toast.LENGTH_SHORT).show();
}
else
{
this.setListAdapter(new MyAdapter());
}
break;
}
}
class Holder
{
TextView mTextView;
}
class MyAdapter extends BaseAdapter
{
LayoutInflater inflater;
Holder mHolder;
@Override
public int getCount() {
// TODO Auto-generated method stub
return appTaskList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
inflater=LayoutInflater.from(GetRunningTaskInfo.this);
if(convertView==null)
{
convertView=inflater.inflate(R.layout.item, null);
mHolder=new Holder();
mHolder.mTextView=(TextView)convertView.findViewById(R.id.item_TextView01);
convertView.setTag(mHolder);
}
else
{
mHolder=(Holder) convertView.getTag();
}
mHolder.mTextView.setText(appTaskList.get(position));
return convertView;
}
}
}
记得加上访问权限:
android.permission.GET_TASKS
标签:管理器,Overridepublic,int,类似,Window,convertView,appTaskList,import,android From: https://blog.51cto.com/u_15907753/5926219