Android中PackageManager查询,安装,启动,读取meta等操作

  1. 查询手机上的所有app

首先建立一个Model

class ApplicationInfo {

    CharSequence title;

    Intent intent;

    Drawable icon;

    boolean filtered;

  //启动app初始化设置方式一
    final void setActivity(ComponentName className, int launchFlags) { 
        intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setComponent(className);
        intent.setFlags(launchFlags);
    }

     //直接获得启动器,启动初始化方式二
   final void setActivity(Context cxt,String packageName, int launchFlags) {
    	 intent = cxt.getPackageManager().getLaunchIntentForPackage(packageName);
         intent.setFlags(launchFlags);
    }
    
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof ApplicationInfo)) {
            return false;
        }

        ApplicationInfo that = (ApplicationInfo) o;
        return title.equals(that.title) &&
                intent.getComponent().getClassName().equals(
                        that.intent.getComponent().getClassName());
    }

    @Override
    public int hashCode() {
        int result;
        result = (title != null ? title.hashCode() : 0);
        final String name = intent.getComponent().getClassName();
        result = 31 * result + (name != null ? name.hashCode() : 0);
        return result;
    }
}

  第一种方式: 又一次被网络给坑了,找到的代码只能查询到系统图的app,不能查询到全部pp,代码如下:

private void loadAppVerion1() {
    
        PackageManager manager = getPackageManager();

        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); //可能出现误差,因为Action任何Activity都可以相同
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        final List<ResolveInfo> apps = manager.queryIntentActivities(mainIntent, 0);
        Collections.sort(apps, new ResolveInfo.DisplayNameComparator(manager));
        
        if (apps != null) {
            final int count = apps.size();

            if (mApplications == null) {
                mApplications = new ArrayList<ApplicationInfo>(count);
            }
            mApplications.clear();

            for (int i = 0; i < count; i++) {
                ApplicationInfo application = new ApplicationInfo();
                ResolveInfo info = apps.get(i);

                application.title = info.loadLabel(manager);
                application.setActivity(new ComponentName(
                        info.activityInfo.applicationInfo.packageName,
                        info.activityInfo.name),
                        Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                application.icon = info.activityInfo.loadIcon(manager);

                mApplications.add(application);
            }
        }
}

第二种方式可获得所有的app(推荐):

private void loadAppVerion2()
{
PackageManager packageManager = this.getPackageManager();
List<PackageInfo> packageInfoList = packageManager.getInstalledPackages(PackageManager.GET_ACTIVITIES);
     
if(packageInfoList!=null)
{
     if (mApplications == null)
     {
         mApplications = new ArrayList<ApplicationInfo>();
     }
     mApplications.clear();
             
     for (PackageInfo packageInfo : packageInfoList)
      {
        	ApplicationInfo application = new ApplicationInfo();
        	if(android.os.Build.VERSION.SDK_INT>=9)
        	{
        		application.icon  = packageInfo.applicationInfo.loadLogo(packageManager);
        	 }else{
        	    application.icon = packageInfo.applicationInfo.loadIcon(packageManager);
        	}
        	    application.title = packageInfo.applicationInfo.loadLabel(packageManager);
        	    String packageName = packageInfo.packageName;
        	    application.setActivity(this, packageName,Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        	    mApplications.add(application);
		}
        }
 }

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

下面是关于app的安装

// 安装APK程序代码
private void openArcInStall(File file) {//传入app的下载路径构造的File文件即可

    Intent intent = new Intent();
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setAction(android.content.Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
    startActivity(intent);
}


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

体验一下查询的功能吧


查找可以分享文本的app

/**
* 查询手机内所有支持分享的应用
* @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;  
	}

查询最近打开过的应用(任务栈)

   private void searchRecents()
    {
        final PackageManager manager = getPackageManager();
        final ActivityManager tasksManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        final List<ActivityManager.RecentTaskInfo> recentTasks = tasksManager.getRecentTasks(4, 0);

        final int count = recentTasks.size();
        final ArrayList<ApplicationInfo> recents = new ArrayList<ApplicationInfo>();
    }

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

读取Meta信息

1> application
<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="application_meta_data"
            android:value="application_meta_data_value" />  <!--可以关联资源ID, 支持多种数据类型 -->

ApplicationInfo info = null;
        
     try {
         info = getPackageManager()
                   .getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
     } catch (NameNotFoundException e) {
         e.printStackTrace();
     }
Log.v(TAG, "application meta data value:" + info.metaData.getString("application_meta_data"));
2> Activity 

<activity
        android:name="fyc.app.testandroid.MainActivity" >
        <meta-data
            android:name="activity_meta_data"
            android:value="activity_meta_data_value" />
</activity>

ActivityInfo info = null;
        
    try {
        info = getPackageManager()
                 .getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
Log.v(TAG, "activity meta data value:" + info.metaData.getString("activity_meta_data"));
3> Service

<service android:name="fyc.app.testandroid.MyService" >
            <meta-data
                android:name="service_meta_data"
                android:value="service_meta_data_value" />
</service>

ServiceInfo info = null;
        
    try {
        info = getPackageManager()
                .getServiceInfo(new ComponentName(this, MyService.class), PackageManager.GET_META_DATA); 
    } catch (NameNotFoundException e) {
            e.printStackTrace();
    }
Log.v(TAG, "service meta data value:" + info.metaData.getString("service_meta_data"));
4> BroadcastReceiver

<receiver android:name="fyc.app.testandroid.MyReceiver" >
        <meta-data
                android:name="receiver_meta_data"
                android:value="receiver_meta_data_value" />
</receiver>

ActivityInfo info = null;
        
    try {
        info = getPackageManager()
                .getReceiverInfo(new ComponentName(this, MyReceiver.class), PackageManager.GET_META_DATA);
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
Log.v(TAG, "receiver meta data value:" + info.metaData.getString("receiver_meta_data"));


你可能感兴趣的:(Packager管理)