總網頁瀏覽量

關於我自己

我的相片
人生的必修課是接受無常,人生的選修課是放下執著。

2012年3月3日 星期六

區分系統AP & 手動AP

==========================第1層:抓出所有AP==========================
PackageManager packageManager = context.getPackageManager();
List<ApplicationInfo> allappInfo = packageManager.getInstalledApplications(PackageManager.GET_ACTIVITIES);       
for(int i = 0 ; i <allappInfo.size() ; i ++)
{  
    //appPI取得如ApplicationInfo{2ad14680 com.android.launcher}的資訊              
    ApplicationInfo appPI = allappInfo.get(i); 
}

=======================第2層:可以被launch的AP=======================
PackageManager packageManager = context.getPackageManager();
List<ApplicationInfo> allappInfo = packageManager.getInstalledApplications(PackageManager.GET_ACTIVITIES);
for(int i = 0 ; i <allappInfo.size() ; i ++)
{
    ApplicationInfo appPI = allappPagInfo.get(i);
    Intent intent = packageManager.getLaunchIntentForPackage(appPI.packageName);
    //如果沒有android.intent.action.MAIN←launch進入點, 則intent=null
    if (intent != null)
    {
        //appPI2會抓到可以被launch的AP資訊
        ApplicationInfo appPI2 = allappPagInfo.get(i);
    }
}

========================第3層:只需要系統或手動AP========================
PackageManager packageManager = context.getPackageManager();
List<ApplicationInfo> allappInfo = packageManager.getInstalledApplications(PackageManager.GET_ACTIVITIES);
for(int i = 0 ; i <allappInfo.size() ; i ++)
{
    ApplicationInfo appAI = allappInfo.get(i);
    Intent intent = packageManager.getLaunchIntentForPackage(appAI.packageName);
    if (intent != null)
    {
        //位於/system/app/的AP → FLAG_SYSTEM>0
        if ((appAI.flags & appAI.FLAG_SYSTEM) > 0)
        {
            //appPI2只會抓到可以被launch的系統AP資訊
            ApplicationInfo appPI2 = allappInfo.get(i);
        }
        else
        {
            //appPI3只會抓到可以被launch的手動AP資訊
            ApplicationInfo appPI3 = allappInfo.get(i);
        }
    }
}

以(ApplicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)的值來判別,
該值大於0時,表示取得的AP為系統預設(整合在Image中, 放在system/app/),
該值小於等於0時, 表示取得的AP為之後安裝的(未整合在Image中, 放在data/app/)

沒有留言:

張貼留言