14
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

自アプリのactivity一覧の取得

Last updated at Posted at 2014-01-23

全アプリケーションのアクティビティ一覧を取得する方法はググると出てきますが、
自分のアプリケーションのアクティビティを取得する方法は見つからなかったので、
実装してみました。

実装方法

private String getMainActivityName(){
    ActivityInfo[] activities = null;
    PackageManager pm = getPackageManager();
    try {
        //当アプリの全アクティビティを取得
        PackageInfo packageInfo = pm.getPackageInfo(getPackageName(), PackageManager.GET_ACTIVITIES);
        activities = packageInfo.activities;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    if(activities.length < 1) return null;
    //端末の全アプリのACTION_MAINで起動できる全アクティビティを取得
    Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    List<ResolveInfo> resolveInfo = pm.queryIntentActivities(intent, PackageManager.GET_RESOLVED_FILTER);

    //全メインアクティビティ内から当アプリのメインアクティビティを探す
    for(int i = 0; i < resolveInfo.size(); i++) {
        String activityName = resolveInfo.get(i).activityInfo.name;
        for(int j = 0; j < activities.length; j++){
            String name = activities[j].name;
            if(activityName.equals(name)) return name;
        }
    }
    return null;
}

中途半端な状態だったのに、ストックしてくれている方がいたので、取り急ぎコード掲載

14
13
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?