2011-11-28 9 views
15

私は本質的にホームスクリーンであり、デフォルトのホームスクリーン( "キオスク"アプリケーション)として使用されるはずのbuissnessアプリケーションを開発しています。私のアプリケーションがデフォルトのランチャーであるかどうかをチェックする方法

LauncherがデフォルトのLauncherであるかどうかを確認する方法はありますか? ありがとう!

ps。ここ 同様の例が、GPS-設定をチェックするため

LocationManager alm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 
if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) { 
    Stuffs&Actions; 
} 

答えて

24

ます優先する活動のリストをPackageManagerから得ることができます。方法はgetPreferredActivities()です。

boolean isMyLauncherDefault() { 
    final IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN); 
    filter.addCategory(Intent.CATEGORY_HOME); 

    List<IntentFilter> filters = new ArrayList<IntentFilter>(); 
    filters.add(filter); 

    final String myPackageName = getPackageName(); 
    List<ComponentName> activities = new ArrayList<ComponentName>(); 
    final PackageManager packageManager = (PackageManager) getPackageManager(); 

    // You can use name of your package here as third argument 
    packageManager.getPreferredActivities(filters, activities, null); 

    for (ComponentName activity : activities) { 
     if (myPackageName.equals(activity.getPackageName())) { 
      return true; 
     } 
    } 
    return false; 
} 
+0

正常に動作します。私は第3のパラメータとしてパッケージ名を使用し、 'activities'リストの長さをチェックしました。 0の場合、ランチャーではないことを意味します。 –

+0

この場合、このクエリで複数の項目が「アクティビティ」で満たされますか? –

+2

[getPreferredActivities](http://developer.android.com/reference/android/content/pm/PackageManager.html#getPreferredActivities%28java.util.List%3Candroid.content.IntentFilter%3E,%20java.util)のドキュメント.List%3Candroid.content.ComponentName%3E、%20java.lang.String%29)は、最初の引数がメソッドによって設定された空のリストであることを示唆しています。あなたの例のようにすでに人目を引いたリストを与えているときの動作はどういうものでしょうか? – achoo5000

5

発見私の答え:私の活動は、デフォルトのランチャーであるかどうかは私に語っ

Which launcher is running?

..

+0

ランチャーが実行されている場合、それはあなただけに伝えます。たとえば、Google NowランチャーとNOVAランチャーの両方が実行されている場合は、両方が返されます。 –

0
boolean isHomeApp() { 
    final Intent intent = new Intent(Intent.ACTION_MAIN); 
    intent.addCategory(Intent.CATEGORY_HOME); 
    final ResolveInfo res = getPackageManager().resolveActivity(intent, 0); 
    if (res.activityInfo != null && getPackageName() 
      .equals(res.activityInfo.packageName)) { 
     return true; 
    } 
    return false; 
} 
0

Kotlinバージョン:

val Context.isMyLauncherDefault: Boolean 
    get() = ArrayList<ComponentName>().apply { 
    packageManager.getPreferredActivities(
     arrayListOf(IntentFilter(ACTION_MAIN).apply { addCategory(CATEGORY_HOME) }), 
     this, 
     packageName 
    ) 
    }.isNotEmpty() 
関連する問題