欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android實(shí)現(xiàn)獲取正在運(yùn)行的應(yīng)用程序

 更新時(shí)間:2013年01月08日 17:57:13   投稿:whsnow  
android如何獲取正在運(yùn)行的應(yīng)用程序,因?yàn)樵趂ramework中想添加這個(gè)功能,所以寫了個(gè)appliction來實(shí)現(xiàn)一下獲取正在運(yùn)行的應(yīng)用程序

因?yàn)樵趂ramework中想添加這個(gè)功能,所以寫了個(gè)appliction來實(shí)現(xiàn)一下獲取正在運(yùn)行的應(yīng)用程序:
還是先看圖吧:

這個(gè)app主要是簡(jiǎn)單的實(shí)現(xiàn)了獲取非系統(tǒng)的應(yīng)用程序和一些常用的系統(tǒng)應(yīng)用程序,顯示在一個(gè)listview中,并添加了點(diǎn)擊(回復(fù)到你打開的界面)和長(zhǎng)按事件(關(guān)閉應(yīng)用程序)。
看看代碼吧:
直接貼出來再加上注釋吧(直接寫在一個(gè)文件里):

復(fù)制代碼 代碼如下:

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();//獲得包管理器
listView=(ListView)findViewById(R.id.list_view);
LoadList(this);//加載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); //獲得ActivityManager對(duì)象
List<RunningAppProcessInfo> runningTasks = am.getRunningAppProcesses(); //獲得所有正在進(jìn)行的程序存放在一個(gè)list中
for(int i=0;i<runningTasks.size();i++)
{
PackageInfo pInfo=new PackageInfo(context);//獲得PackageInfo對(duì)象
//get application which is not in system and the usually
//如果是非系統(tǒng)應(yīng)用程序以及一些常用的應(yīng)用程序就加到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)));//獲得應(yīng)用程序的大小如果size大于一M就用M為單位,否則用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());//獲得包名給后面用
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加載識(shí)別器
//下面這個(gè)方法主要是用來刷新圖片,因?yàn)閜Info.getInfo(runningTasks.get(i).processName).loadIcon(pm)獲得圖片不能被顯示出
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;
}
});
//為listView添加item的點(diǎn)擊事件
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");//從前面的map中獲得包名
//if we onclick the item then start the application
//根據(jù)包名打開應(yīng)用程序
Intent intent=new Intent();
intent =pm.getLaunchIntentForPackage(packageName);
startActivity(intent);
finish();//打開應(yīng)用程序之后注銷本應(yīng)用程序
}
});
//為listview的item添加長(zhǎng)按事件
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
am.killBackgroundProcesses(packageName);
//refash list
//刷新listview
LoadList(ManagerTasksActivity.this);
}
}).setNegativeButton("cancle", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
return false;
}
});
}
}
//類PackageInfo主要包括ApplicationInfo getInfo(String name)方法
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文件:
lsit:
復(fù)制代碼 代碼如下:

<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:
復(fù)制代碼 代碼如下:

<?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>

在manifest文件中要加一個(gè)權(quán)限:
復(fù)制代碼 代碼如下:

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

主要是前面的am.killBackgroundProcesses(packageName);方法要這個(gè)權(quán)限。

相關(guān)文章

  • Android?Studio調(diào)試Gradle插件詳情

    Android?Studio調(diào)試Gradle插件詳情

    這篇文章主要介紹了Android?Studio調(diào)試Gradle插件詳情,文章圍繞主題展開詳細(xì)的內(nèi)容戒殺,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • Android 定時(shí)器實(shí)現(xiàn)圖片的變換

    Android 定時(shí)器實(shí)現(xiàn)圖片的變換

    這篇文章主要介紹了Android 定時(shí)器實(shí)現(xiàn)圖片的變換的相關(guān)資料,利用到定時(shí)器和handler,message的結(jié)合實(shí)現(xiàn)改功能,需要的朋友可以參考下
    2017-08-08
  • Android仿考拉全局滑動(dòng)返回及聯(lián)動(dòng)效果的實(shí)現(xiàn)方法

    Android仿考拉全局滑動(dòng)返回及聯(lián)動(dòng)效果的實(shí)現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于Android仿考拉全局滑動(dòng)返回及聯(lián)動(dòng)效果的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08
  • Android 使用PDF.js瀏覽pdf的方法示例

    Android 使用PDF.js瀏覽pdf的方法示例

    這篇文章主要介紹了Android 使用PDF.js瀏覽pdf的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-03-03
  • android開發(fā)實(shí)踐之ndk編譯命令簡(jiǎn)單示例

    android開發(fā)實(shí)踐之ndk編譯命令簡(jiǎn)單示例

    這篇文章主要給大家介紹了在android中ndk編譯命令使用的相關(guān)資料,文中詳細(xì)介紹了ndk-build命令行參數(shù),并通過簡(jiǎn)單的示例代碼給大家介紹了如何編寫 .c 文件,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-06-06
  • 深入Android Handler,MessageQueue與Looper關(guān)系

    深入Android Handler,MessageQueue與Looper關(guān)系

    這篇文章主要介紹了深入Android Handler,MessageQueue與Looper關(guān)系,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-08-08
  • Android評(píng)分控件RatingBar使用實(shí)例解析

    Android評(píng)分控件RatingBar使用實(shí)例解析

    這篇文章主要為大家詳細(xì)介紹了Android評(píng)分控件RatingBar使用實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • Android實(shí)現(xiàn)PDF預(yù)覽打印功能

    Android實(shí)現(xiàn)PDF預(yù)覽打印功能

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)PDF預(yù)覽打印功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • Android上下文菜單用法實(shí)例分析

    Android上下文菜單用法實(shí)例分析

    這篇文章主要介紹了Android上下文菜單用法,以完整實(shí)例形式分析了Android上下文菜單的定義、布局及功能實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-09-09
  • 詳解Flutter中l(wèi)istview的高級(jí)用法

    詳解Flutter中l(wèi)istview的高級(jí)用法

    一般我們使用Listview的方式是構(gòu)建要展示的item,然后將這些item傳入ListView的構(gòu)造函數(shù)即可,通常情況下這樣做是夠用了,但是不排除我們會(huì)有一些其他的特殊需求。今天我們會(huì)來講解一下ListView的一些高級(jí)用法,希望對(duì)大家有所幫助
    2023-01-01

最新評(píng)論