获得当前运行应用的名称

众所周知,通过PackageManager可以获取手机端已安装的apk文件的信息,具体代码如下

PackageManager packageManager = this.getPackageManager();
List<PackageInfo> packageInfoList = packageManager.getInstalledPackages(0);

通过以上方法,可以得到手机中安装的所有应用程序,既包括了手动安装的apk包的信息,也包括了系统预装的应用软件的信息,要区分这两类软件可使用以下方法:

a.从packageInfoList获取的packageInfo,再通过packageInfo.applicationInfo获取applicationInfo。

b.判断(applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)的值,该值大于0时,表示获取的应用为系统预装的应用,反之则为手动安装的应用。

大家可以看下代码,说明已经写在注释中。

获取应用的代码:



/**
  * 查询手机内非系统应用
  * @param context
  * @return
  */
public static List<PackageInfo> getAllApps(Context context) {
  List<PackageInfo> apps = new ArrayList<PackageInfo>();
  PackageManager pManager = context.getPackageManager();
  //获取手机内所有应用
  List<PackageInfo> paklist = pManager.getInstalledPackages(0);
  for (int i = 0; i < paklist.size(); i++) {
   PackageInfo pak = (PackageInfo) paklist.get(i);
   //判断是否为非系统预装的应用程序
   if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {
    // customs applications
    apps.add(pak);
   }
  }
  return apps;
}



获取图片、应用名、包名:

                      PackageManager pManager = MessageSendActivity.this.getPackageManager();
                     List<PackageInfo> appList  = Utils.getAllApps(MessageSendActivity.this);
                    
                     for(int i=0;i<appList.size();i++) {
                      PackageInfo pinfo = appList.get(i);
                      ShareItemInfo shareItem = new ShareItemInfo();
                      //set Icon
                      shareItem.setIcon(pManager.getApplicationIcon(pinfo.applicationInfo));
                      //set Application Name
                      shareItem.setLabel(pManager.getApplicationLabel(pinfo.applicationInfo).toString());
                      //set Package Name
                      shareItem.setPackageName(pinfo.applicationInfo.packageName);
                     
                     }



其中 ShareItemInfo 类是我本地自定义的,各位可以忽略!

另为,有人可能在找 分享应用列表,在此一并说下吧。

获取支持分享的应用的代码:

  /**
  * 查询手机内所有支持分享的应用
  * @param context
  * @return
  */
public static List<ResolveInfo> getShareApps(Context context){
     List<ResolveInfo> mApps = new ArrayList<ResolveInfo>(); 
     Intent intent=new Intent(Intent.ACTION_SEND,null); 
     intent.addCategory(Intent.CATEGORY_DEFAULT); 
     intent.setType("text/plain"); 
     PackageManager pManager = context.getPackageManager();
     mApps = pManager.queryIntentActivities(intent,PackageManager.COMPONENT_ENABLED_STATE_DEFAULT); 
    
     return mApps; 
}

由于该方法,返回的并不是 PackageInfo 对象。而是 ResolveInfo。因此获取图片、应用名、包名的方法不一样,如下:

                      PackageManager pManager = MessageSendActivity.this.getPackageManager();
                    
                     /*********************查询手机内所有支持分享的应用*********************/
                     List<ResolveInfo> resolveList  = Utils.getShareApps(MessageSendActivity.this);
                    
                     for(int i=0;i<resolveList.size();i++) {
                      ResolveInfo resolve = resolveList.get(i);
                      ShareItemInfo shareItem = new ShareItemInfo();
                      //set Icon
                      shareItem.setIcon(resolve.loadIcon(pManager));
                      //set Application Name
                      shareItem.setLabel(resolve.loadLabel(pManager).toString());
                      //set Package Name
                      shareItem.setPackageName(resolve.activityInfo.packageName);
                     
                     }

总结:

通过 PackageInfo  获取具体信息方法:




包名获取方法:packageInfo.packageName

icon获取获取方法:packageManager.getApplicationIcon(applicationInfo)

应用名称获取方法:packageManager.getApplicationLabel(applicationInfo)

使用权限获取方法:packageManager.getPackageInfo(packageName,PackageManager.GET_PERMISSIONS)

.requestedPermissions




通过 ResolveInfo 获取具体信息方法:




包名获取方法:resolve.activityInfo.packageName

icon获取获取方法:resolve.loadIcon(packageManager)

应用名称获取方法:resolve.loadLabel(packageManager).toString()
===========================================================================
ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = this.getPackageManager();
while(i.hasNext()) {
  ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
  try {
    CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
    Log.w("LABEL", c.toString());
  }catch(Exception e) {
    //Name Not FOund Exception
  }
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
我现在的解决办法是,先通过 ActivityManager.RunningAppProcessInfo.processName 得到正在运行的进程名processName,然后再通过PackageManager.getInstallApplications()得到所有系统安装的应用程序信息,包括processName,lable(真实的应用程序名),icon,memData(应用程序占用内存大小)。通过之前得到的运行应用的processName,匹配后边得到的processName,得到相应的应用名之类的信息。
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  最近想做一个任务管理器练一练手,第一道题就是获取手机中正在运行的程序。后来在网上找了一下资料,终于有了眉目。废话不多说!看代码。

ActivityMain.java

view plain
public class ActivityMain extends ListActivity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 
        
       List<Programe> list = getRunningProcess(); 
       ListAdapter adapter = new ListAdapter(list,this); 
       getListView().setAdapter(adapter); 
    } 
     
    //正在运行的 
    public List<Programe> getRunningProcess(){ 
        PackagesInfo pi = new PackagesInfo(this); 
         
        ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE); 
        //获取正在运行的应用 
        List<RunningAppProcessInfo> run = am.getRunningAppProcesses(); 
        //获取包管理器,在这里主要通过包名获取程序的图标和程序名 
        PackageManager pm =this.getPackageManager(); 
        List<Programe> list = new ArrayList<Programe>();     
         
        for(RunningAppProcessInfo ra : run){ 
            //这里主要是过滤系统的应用和电话应用,当然你也可以把它注释掉。 
            if(ra.processName.equals("system") || ra.processName.equals("com.android.phone")){ 
                continue; 
            } 
             
            Programe  pr = new Programe(); 
            pr.setIcon(pi.getInfo(ra.processName).loadIcon(pm)); 
            pr.setName(pi.getInfo(ra.processName).loadLabel(pm).toString()); 
            System.out.println(pi.getInfo(ra.processName).loadLabel(pm).toString()); 
            list.add(pr); 
        } 
        return list; 
    } 
     





ListAdapter.java

view plain
public class ListAdapter extends BaseAdapter { 
    List<Programe> list = new ArrayList<Programe>(); 
    LayoutInflater la; 
    Context context; 
     
    public ListAdapter(List<Programe> list ,Context context){ 
        this.list = list; 
        this.context = context; 
    } 
     
    @Override 
    public int getCount() { 
        // TODO Auto-generated method stub 
        return list.size(); 
    } 
    @Override 
    public Object getItem(int position) { 
        // TODO Auto-generated method stub 
        return list.get(position); 
    } 
    @Override 
    public long getItemId(int position) { 
        // TODO Auto-generated method stub 
        return position; 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
        ViewHolder holder; 
        if(convertView == null) 
        {   
            la = LayoutInflater.from(context); 
            convertView=la.inflate(R.layout.list_item, null); 
             
            holder = new ViewHolder(); 
            holder.imgage=(ImageView) convertView.findViewById(R.id.image); 
            holder.text = (TextView) convertView.findViewById(R.id.text); 
             
            convertView.setTag(holder); 
        }else{ 
            holder = (ViewHolder) convertView.getTag(); 
        } 
         final Programe pr = (Programe)list.get(position); 
        //设置图标 
        holder.imgage.setImageDrawable(pr.getIcon()); 
        //设置程序名 
        holder.text.setText(pr.getName()); 
         
        return convertView; 
    } 

class ViewHolder{ 
     TextView text; 
    ImageView imgage; 





PackagesInfo.java

view plain
public class PackagesInfo { 
    private List<ApplicationInfo> appList; 
     
    public PackagesInfo(Context context){ 
        //通包管理器,检索所有的应用程序(甚至卸载的)与数据目录 
        PackageManager pm = context.getApplicationContext().getPackageManager(); 
        appList = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES); 
    } 
     
     
     
    /**
     * 通过一个程序名返回该程序的一个Application对象。
     * @param name  程序名
     * @return  ApplicationInfo 
     */ 
     
    public ApplicationInfo getInfo(String name){ 
        if(name == null){ 
            return null; 
        } 
        for(ApplicationInfo appinfo : appList){ 
            if(name.equals(appinfo.processName)){ 
                return appinfo; 
            } 
        } 
        return null; 
    } 
     



Programe.java



view plain
public class Programe { 
    //图标 
    private Drawable icon;   
    //程序名 
    private String name; 
     
    public Drawable getIcon() { 
        return icon; 
    } 
    public void setIcon(Drawable icon) { 
        this.icon = icon; 
    } 
    public String getName() { 
        return name; 
    } 
    public void setName(String name) { 
        this.name = name; 
    } 
     





list_item.xml

view plain
<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
   > 
    <ImageView 
        android:id="@+id/image" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginRight="10dip" 
    />  
  <TextView 
    android:id="@+id/text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
  /> 
    
   </LinearLayout> 

你可能感兴趣的:(应用)