Android implementation gets the running application _android

Source: Internet
Author: User
Because you want to add this functionality in the framework, you write a appliction to get the running application:
Let's look at the picture first:

This app is simply implemented to get a system-less application and some common system applications, shown in a ListView, and add clicks (Revert to your Open Interface) and long press events (close the application).
Let's look at the code:
Just post it and add a comment (write it directly in a file):
Copy Code code 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 ()//Get Package Manager
listview= (ListView) Findviewbyid (R.id.list_view);
Loadlist (this);//Load ListView
}
private void Loadlist (context context)
{
Arraylisttry{
Activitymanager am = (activitymanager) context.getsystemservice (Context.activity_service); Get Activitymanager Object
list<runningappprocessinfo> runningtasks = am.getrunningappprocesses (); Get all ongoing programs stored in a list
for (int i=0;i<runningtasks.size (); i++)
{
PackageInfo pinfo=new PackageInfo (context)//Get PackageInfo Object
Get application which isn't in system and the usually
If a system-neutral application and some common applications are added 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 ((New File (dir). Length () *1.0))//Get application size if size is greater than one m, use m as unit, otherwise kb
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 ());//Get package name for back
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);//listview load recognizer
The following method is mainly used to refresh the picture because Pinfo.getinfo (Runningtasks.get (i). processname). LoadIcon (PM) Get picture cannot be displayed
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 event to add Item 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");//Get package name from previous map
If we onclick the item then start the application
Open an application based on the package name
Intent intent=new Intent ();
Intent =pm.getlaunchintentforpackage (PackageName);
StartActivity (Intent);
Finish ()//unregister the application after opening 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
Kill an application based on the package name
Am.killbackgroundprocesses (PackageName);
Refash List
Refresh 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 {
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 Code code 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 Code code 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>

To add a permission in the manifest file:
Copy Code code as follows:

<uses-permission android:name= "Android.permission.RESTART_PACKAGES"/>

The main is the front of the Am.killbackgroundprocesses (PackageName) method to this permission.
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.