To add this function to the framework, write an appliction to obtain the running application:
Let's take a look at the figure first:
This app is mainly used to obtain non-system applications and some common system applications, display them in a listview, and add clicks (reply to the interface you opened) and long press events (close the application ).
Let's look at the Code:
Paste it directly and add a comment (directly written in a file ):
Copy codeThe Code is as follows: package andorid. tasks;
Import java. io. File;
Import java. util. ArrayList;
Import java. util. HashMap;
Import java. util. List;
Import android. app. Activity;
Import android. app. ActivityManager;
Import android. app. ActivityManager. RunningAppProcessInfo;
Import android. app. AlertDialog;
Import android. content. Context;
Import android. content. DialogInterface;
Import android. content. DialogInterface. OnClickListener;
Import android. content. Intent;
Import android. content. pm. ApplicationInfo;
Import android. content. pm. PackageManager;
Import android. graphics. drawable. Drawable;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. AdapterView;
Import android. widget. AdapterView. OnItemLongClickListener;
Import android. widget. ImageView;
Import android. widget. ListView;
Import android. widget. SimpleAdapter;
Import android. widget. AdapterView. OnItemClickListener;
Import android. widget. SimpleAdapter. ViewBinder;
Public class ManagerTasksActivity extends Activity {
Private ListView listView;
Private PackageManager pm;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. task_main );
Pm = this. getPackageManager (); // obtain the Package Manager
ListView = (ListView) findViewById (R. id. list_view );
LoadList (this); // load the listview
}
Private void LoadList (Context context)
{
ArrayList <HashMap <String, Object> list = new ArrayList <HashMap <String, Object> ();
Try {
ActivityManager am = (ActivityManager) context. getSystemService (Context. ACTIVITY_SERVICE); // obtain the ActivityManager object
List <RunningAppProcessInfo> runningTasks = am. getRunningAppProcesses (); // obtain all ongoing programs and store them in a list.
For (int I = 0; I <runningTasks. size (); I ++)
{
PackageInfo pInfo = new PackageInfo (context); // obtain the PackageInfo object
// Get application which is not in system and the usually
// Add non-system applications and some common applications to the list.
If (pInfo. getInfo (runningTasks. get (I). processName). flags & pInfo. getInfo (runningTasks. get (I). processName). FLAG_SYSTEM) = 0
| (RunningTasks. get (I). processName). equals ("com. android. contacts ")
| (RunningTasks. get (I). processName). equals ("com. android. email ")
| (RunningTasks. get (I). processName). equals ("com. android. settings ")
| (RunningTasks. get (I). processName). equals ("com. android. music ")
| (RunningTasks. get (I). processName). equals ("com. android. calendar ")
| (RunningTasks. get (I). processName). equals ("com. android. calculator2 ")
| (RunningTasks. get (I). processName). equals ("com. android. browser ")
| (RunningTasks. get (I). processName). equals ("com. android. camera ")
| (RunningTasks. get (I). processName). equals ("com. cooliris. media ")
| (RunningTasks. get (I). processName). equals ("com. android. bluetooth ")
| (RunningTasks. get (I). processName). equals ("com. android. mms "))
{
String dir = pInfo. getInfo (runningTasks. get (I). processName). publicSourceDir;
Float size = Float. valueOf (float) (new File (dir ). length () * 1.0); // obtain the application size. If the size is greater than one MB, M is used; otherwise, KB is used.
// Long date = new Date (new File (dir). lastModified (). getTime ();
// System. out. println (pInfo. getInfo (runningTasks. get (I). processName). loadIcon (pm ));
HashMap <String, Object> map = new HashMap <String, Object> ();
Map. put ("icon", pInfo. getInfo (runningTasks. get (I). processName). loadIcon (pm ));
Map. put ("name", pInfo. getInfo (runningTasks. get (I). processName). loadLabel (pm ));
If (size> 1024*1024)
Map. put ("info", size/1024/1024 + "MB ");
Else
Map. put ("info", size/1024 + "KB ");
Map. put ("packagename", runningTasks. get (I). processName. toString (); // obtain the package name for later use
List. add (map );
}
}
} Catch (Exception ex)
{}
SimpleAdapter listadapter = new SimpleAdapter (this, list, R. layout. task_list, new String [] {"icon", "name", "info"}, new int [] {R. id. icon, R. id. name, R.id.info });
ListView. setAdapter (listadapter); // The listview Loader
// The following method is used to refresh the image, because pInfo. getInfo (runningTasks. get (I). processName). loadIcon (pm) cannot display the image
Listadapter. setViewBinder (new ViewBinder (){
Public boolean setViewValue (View view, Object data, String textRepresentation ){
If (view instanceof ImageView & data instanceof Drawable ){
ImageView iv = (ImageView) view;
Iv. setImageDrawable (Drawable) data );
Return true;
}
Else
Return false;
}
});
// Click events for adding items to listView
ListView. setOnItemClickListener (new OnItemClickListener (){
@ Override
Public void onItemClick (AdapterView <?> Parent, View view, int position,
Long id ){
// Get the item of the list to a hashmap
HashMap <?, ?> Map = (HashMap <?, ?>) Parent. getItemAtPosition (position );
// Get package name from map
String packageName = (String) map. get ("packagename"); // obtain the package name from the preceding map.
// If we onclick the item then start the application
// Open the application based on the package name
Intent intent = new Intent ();
Intent = pm. getLaunchIntentForPackage (packageName );
StartActivity (intent );
Finish (); // open the application and log out of the application.
}
});
// Add a long press event for the listview item
ListView. setOnItemLongClickListener (new OnItemLongClickListener (){
@ Override
Public boolean onItemLongClick (AdapterView <?> Parent, View view,
Int position, long id ){
Final HashMap <?, ?> Long_map = (HashMap <?, ?>) Parent. getItemAtPosition (position );
New AlertDialog. Builder (ManagerTasksActivity. this). setTitle ("Are you sure close ")
. SetPositiveButton ("sure", new OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
ActivityManager am = (ActivityManager) getSystemService (Context. ACTIVITY_SERVICE );
String packageName = (String) long_map.get ("packagename ");
// Base packagename to kill appliction
// Kills the application based on the package name
Am. killBackgroundProcesses (packageName );
// Refash list
// Refresh the listview
LoadList (ManagerTasksActivity. this );
}
}). SetNegativeButton ("cancle", new OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
}
}). Show ();
Return false;
}
});
}
}
// Class PackageInfo mainly includes the ApplicationInfo getInfo (String name) Method
Class PackageInfo {
Private List <ApplicationInfo> appList;
Public PackageInfo (Context context ){
// Get all package data
PackageManager pm = context. getApplicationContext (). getPackageManager ();
AppList = pm. getInstalledApplications (PackageManager. GET_UNINSTALLED_PACKAGES );
}
Public ApplicationInfo getInfo (String name ){
If (name = null ){
Return null;
}
For (ApplicationInfo appInfo: appList ){
If (name. equals (appInfo. processName )){
Return appInfo;
}
}
Return null;
}
}
Xml file:
Lsit:Copy codeThe Code is as follows: <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: orientation = "horizontal"
>
<ImageView
Android: id = "@ + id/icon"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
/>
<TextView
Android: layout_width = "40dip"
Android: layout_height = "40dip"
/>
<LinearLayout
Android: orientation = "vertical"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
>
<TextView
Android: id = "@ + id/name"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: textStyle = "bold"
/>
<TextView
Android: id = "@ + id/info"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
/>
</LinearLayout>
</LinearLayout>
Main:Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<ListView
Android: id = "@ + id/list_view"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: orientation = "vertical"
> </ListView>
</LinearLayout>
Add a permission to the manifest file:Copy codeThe Code is as follows: <uses-permission android: name = "android. permission. RESTART_PACKAGES"/>
The preceding am. killBackgroundProcesses (packageName) method requires this permission.