首发地址:http://www.eoeandroid.com/thread-201709-1-1.html
看到几个不错的源码:
植物大战僵尸,又见植物大战僵尸!(分享)
http://www.eoeandroid.com/thread-211063-1-1.html
Android 3D 乡村赛车 完整代码
http://www.eoeandroid.com/thread-211471-1-1.html
发现自己之前练习写过一个Demo,RUN了下感觉还是挺实用的,所以就发上来给大家看看。
权限(这个不要忘记哦!)
<uses-permission android:name="android.permission.GET_TASKS"/> <uses-permission android:name="android.permission.RESTART_PACKAGES"/>
主界面
package irdc.ex10_10; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.app.ActivityManager; import android.app.ActivityManager.RunningServiceInfo; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class EX10_10 extends Activity { private Button Button01; private Button Button02; private ActivityManager mActivityManager; private ArrayList<String> arylistTask; private ArrayList<String> arylistTaskPackageName; private ArrayList<String> arylistService; private ArrayList<String> arylistServicePackageName; private int intGetTaskCounter = 30; public List<ActivityManager.RunningTaskInfo> mRunningTasks; public List<RunningServiceInfo> mRunningTaskServices; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button01 = (Button) findViewById(R.id.Button01); Button02 = (Button) findViewById(R.id.Button02); /* 取得ActivityManager */ mActivityManager = (ActivityManager) EX10_10.this .getSystemService(ACTIVITY_SERVICE); Button01.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub arylistTask = new ArrayList<String>(); arylistTaskPackageName = new ArrayList<String>(); /* 用ActivityManager取得正在运行的硬用程序 */ mRunningTasks = mActivityManager.getRunningTasks(intGetTaskCounter); int i = 1; /* 以循环及baseActivity方式取得名称与ID并放入ArrayList */ for (ActivityManager.RunningTaskInfo amTask : mRunningTasks) { arylistTask.add("" + (i++) + ": " + amTask.baseActivity.getClassName() + "(ID=" + amTask.id + ")"); arylistTaskPackageName.add(amTask.baseActivity.getPackageName()); } /* 将ArrayList放到Bundle并将Activity开启 */ Intent intent = new Intent(); intent.setClass(EX10_10.this, EX10_10_1.class); Bundle bundle = new Bundle(); bundle.putStringArrayList("arylistTask", arylistTask); bundle.putStringArrayList("arylistTaskPackageName", arylistTaskPackageName); intent.putExtras(bundle); startActivity(intent); } }); Button02.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub arylistService = new ArrayList<String>(); arylistServicePackageName = new ArrayList<String>(); mRunningTaskServices = mActivityManager .getRunningServices(intGetTaskCounter); int i = 1; /* 北循环及RunningServiceInfo对象取得服务名称与ID并放入ArrayList */ for (RunningServiceInfo amTask : mRunningTaskServices) { arylistService.add("" + (i++) + ": " + amTask.process + "(ID=" + amTask.pid + ")"); arylistServicePackageName.add(amTask.service.getPackageName()); } /* 将ArrayList放到Bundle并将Activity开启 */ Intent intent = new Intent(); intent.setClass(EX10_10.this, EX10_10_2.class); Bundle bundle = new Bundle(); bundle.putStringArrayList("arylistService", arylistService); bundle.putStringArrayList("arylistServicePackageName", arylistServicePackageName); intent.putExtras(bundle); startActivity(intent); } }); } }
正在运行的程序管理
package irdc.ex10_10; import java.util.ArrayList; import android.app.Activity; import android.app.ActivityManager; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; public class EX10_10_1 extends Activity { private ListView ListView01; private ArrayList<String> arylistTask; private ArrayList<String> arylistTaskPackageName; private ArrayAdapter<String> aryAdapter1; private ActivityManager mActivityManager; int click_id; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_1); /* 取得EX10_10所放的ArrayList */ Bundle bunde = this.getIntent().getExtras(); arylistTask = bunde.getStringArrayList("arylistTask"); arylistTaskPackageName = bunde.getStringArrayList("arylistTaskPackageName"); ListView01 = (ListView) findViewById(R.id.ListView01); /* 将ArrayList放到Adapter */ aryAdapter1 = new ArrayAdapter<String>(EX10_10_1.this, R.layout.simple_list_item_1, arylistTask); ListView01.setAdapter(aryAdapter1); ListView01.setOnItemClickListener(new ListView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int id, long arg3) { // TODO Auto-generated method stub /* 取得ActivityManager */ mActivityManager = (ActivityManager) EX10_10_1.this .getSystemService(ACTIVITY_SERVICE); /* 所点选的index */ click_id = id; AlertDialog.Builder builder = new AlertDialog.Builder(EX10_10_1.this); builder.setCancelable(false); builder.setTitle("Message"); builder.setMessage("确定要删除吗??"); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub /* 停止所点击的应用程序 */ mActivityManager.restartPackage(arylistTaskPackageName .get(click_id).toString()); /* 移除ArrayList里的值并更新ListView */ arylistTask.remove(click_id); ListView01.invalidateViews(); } }); builder.setNegativeButton("NO", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); builder.show(); } }); } }
正在运行的服务管理
package irdc.ex10_10; import java.util.ArrayList; import android.app.Activity; import android.app.ActivityManager; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; public class EX10_10_2 extends Activity { private ListView ListView02; private ArrayList<String> arylistService; private ArrayList<String> arylistServicePackageName; private ArrayAdapter<String> aryAdapter1; private ActivityManager mActivityManager; int click_id; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_2); /* 取得EX10_10所放的ArrayList */ Bundle bunde = this.getIntent().getExtras(); arylistService = bunde.getStringArrayList("arylistService"); arylistServicePackageName = bunde .getStringArrayList("arylistServicePackageName"); ListView02 = (ListView) findViewById(R.id.ListView02); /* 将ArrayList放到Adapter */ aryAdapter1 = new ArrayAdapter<String>(EX10_10_2.this, R.layout.simple_list_item_1, arylistService); ListView02.setAdapter(aryAdapter1); ListView02.setOnItemClickListener(new ListView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int id, long arg3) { // TODO Auto-generated method stub /* 取得ActivityManager */ mActivityManager = (ActivityManager) EX10_10_2.this .getSystemService(ACTIVITY_SERVICE); /* 所点击的index */ click_id = id; AlertDialog.Builder builder = new AlertDialog.Builder(EX10_10_2.this); builder.setCancelable(false); builder.setTitle("Message"); builder.setMessage("确定要删除吗??"); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub /* 停止所点击的服务 */ mActivityManager.restartPackage(arylistServicePackageName.get( click_id).toString()); /* 移除ArrayList里的值并更新ListView */ arylistService.remove(click_id); ListView02.invalidateViews(); } }); builder.setNegativeButton("NO", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); builder.show(); } }); } }
实在不明白的可以下载源码看: