Android gets the running application

Source: Internet
Author: User

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, which are displayed in a listview, and added the click (reply to the interface you opened) and long press events (close the application ). Look at the code: paste it directly and add a comment (directly write it in a file): 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: <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: <? 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> in the manifest file, you must grant the following permissions: <uses-permission android: name = "android. permission. RESTART_PACKAGES "/> mainly refers to the preceding am. killBackgroundProcesses (packageName); this permission is required for the method.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.