5

の助けを借りて、Launcherにあるすべてのアプリケーションを取得することができますIntent.CATEGORY_LAUNCHER

したがって、テスト用にテストアクティビティを作成しました。このアクティビティにはボタンが1つ含まれています。ボタンを押すと、デバイスにアプリケーションが表示されます。

NOTE: `it should not display specific. for example i needed only Browser it should display all browser applications.`

私はこのようにして小さなコードを試しました:

btn.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_APP_BROWSER);

            List<ResolveInfo> mainLauncherList = getPackageManager().queryIntentActivities(intent, 0);
            System.out.println("the list iss = " +mainLauncherList);
        }
    });

リストは、単一のブラウザである1つのブラウザのみを返します。

4

3 に答える 3

7

カテゴリの代わりに、インテントにアクションを与え、次のACTION_VIEWようにURLが可能なものにクエリを実行します。

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.google.com"));

List<ResolveInfo> mainLauncherList = getPackageManager().queryIntentActivities(intent, 0);
Log.e("Browsers","the list iss = " +mainLauncherList);

これは次のようなものを返します:

[ResolveInfo{44e9d350 com.android.browser.BrowserActivity p=0 o=0 m=0x208000}]

また、複数のブラウザがインストールされていると仮定すると、それらすべてが含まれます。たぶん私はあなたの質問を誤解しましたが、これは技術的には、URLを開くインテントを起動しようとした場合と同じアプリケーションを返します。

これらのアプリケーションのランチャーアクティビティを取得することに関しては、すべての主要なアプリケーションを取得する方法をすでに知っているので、私が提供したコードによって、(おそらく)パッケージ名を一致させてランチャーを見つけることができます( ?)

アップデート

ArrayList<String> allLaunchers = new ArrayList<String>();

Intent allApps = new Intent(Intent.ACTION_MAIN);
List<ResolveInfo> allAppList = getPackageManager().queryIntentActivities(allApps, 0);
for(int i =0;i<allAppList.size();i++) allLaunchers.add(allAppList.get(i).activityInfo.packageName);

Intent myApps = new Intent(Intent.ACTION_VIEW);
       myApps.setData(Uri.parse("http://www.google.es"));
List<ResolveInfo> myAppList = getPackageManager().queryIntentActivities(myApps, 0);
for(int i =0;i<myAppList.size();i++){
    if(allLaunchers.contains(myAppList.get(i).activityInfo.packageName)){
        Log.e("match",myAppList.get(i).activityInfo.packageName+"");
    }
}

私が言ったように、ランチャーからすべてのパッケージを取得し、写真を撮ったり、Webを閲覧したりするなど、アクションを実行できるパッケージと照合します。あなたはこれでそのことをうまくやることができるはずです。

于 2012-05-09T09:03:27.183 に答える
2

APIバージョン30より上では、新しいパッケージの可視性ルールqueriesのために、AndroidManifest.xmlにセクションを追加する必要がありました(上記のように使用することに加えて)。PackageManager.MATCH_ALL

<manifest ...>
    <application ... />
    <queries>
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="http" />
        </intent>
    </queries>
</manifest>
于 2021-01-22T20:09:08.063 に答える
1

他の誰かが答えを必要とした場合:

public static void getBrowserApps(Context mcontext) {
        PackageManager packageManager = mcontext.getPackageManager();
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("http://www.google.com"));
        List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
        PackageManager.MATCH_ALL);}

listすべてのブラウザのパッケージ情報が含まれます。

于 2018-02-05T05:59:30.103 に答える