Android – Launch a specific activity from an application

android, android-activity, android-launcher, launch

Just a little problem:

For example I have the activity TestActivity in my Application com.example.testapp
but the activity doesn't have the flag android.intent.category.LAUNCHER

now my question is how can i check if a given activity subpackage.SomeActivity
exists in a Package com.test.somepackage

it would be the same as typing in a console:

adb shell am start -n com.test.somepackage/subpackage.SomeActivity

and if it exists, how can i launch it from another app?

××××× to clarify my question ×××××

i am searching for functions in android like
Intent startthis = new Intent (packagename, activityname);

and if its possible something like
ActivityInfo[] ai = fetchAllActivitysFromPackage(packagename);

is there any function in android that does likely the same?

Best Solution

You can get all activities for packages the following way:

List<PackageInfo> installedPackages = getPackageManager().getInstalledPackages(PackageManager.GET_ACTIVITIES);Iterator packageIterator = installedPackages.iterator();PackageInfo packageInfo = null;while(packageIterator.hasNext()){    packageInfo = (PackageInfo) packageIterator.next();    if(packageInfo.activities != null){        for(ActivityInfo activity : packageInfo.activities){            Log.d("ACTIVITY", activity.name);        }    }}

However, you can only start an external activity if that app has defined an intent-filter for it, and if you now the exact ACTION of the intent filter.

Update:To get all activities for one package name, use:

PackageInfo packageInfo = getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);