开发者

start application knowing package name

开发者 https://www.devze.com 2023-01-10 23:29 出处:网络
How can开发者_开发技巧 i start a new application using its package name? I don\'t have information about what activity is the main one.Just use these following two lines, so you can launch any install

How can开发者_开发技巧 i start a new application using its package name? I don't have information about what activity is the main one.


Just use these following two lines, so you can launch any installed application whose package name is known:

Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.example.abc");
startActivity( launchIntent );

If you don't know the package name of application that you wanted to launch then try your hand on:

PackageManager pm;
pm = getPackageManager();
//  get a list of installed apps.
packages = pm.getInstalledApplications(0);

For more information, refer to this link: Using Package Manager.


Try using PackageManager and getLaunchIntentForPackage()


You can get the launch intent through the PackageManager class:

PackageManager pm = context.getPackageManager();
Intent launchIntent = pm.getLaunchIntentForPackage("com.example.package");
context.startActivity(launchIntent);

Note that getLaunchIntentForPackage returns null if the package isn't found. So you might want to add a null check:

if (launchIntent != null) {
    context.startActivity(launchIntent);
} else {
    Toast.makeText(context, "Package not found", Toast.LENGTH_SHORT).show();
}


Intent intent;                                        
PackageManager pm = getActivity().getPackageManager();

intent = pm.getLaunchIntentForPackage("com.package.name");                       
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(intent);


    Intent intent = getPackageManager().getLaunchIntentForPackage("app.package.name");
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    if (intent != null) {
        startActivity(intent);
    } else {
        Toast.makeText(context, "Package not found", Toast.LENGTH_SHORT).show();
    }


val packageName = "com.google.android.youtube"
var intent = activity!!.packageManager.getLaunchIntentForPackage(packageName)

          if (intent == null) {
            if (intent == null) {
                    intent = try {
                        Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$packageName"))
                    } catch (e: Exception) {
                        Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$packageName"))
                    }
                }
             startActivity(intent)

For Android 11 (API level 30) or higher, in AndroidManifest.xml,

<queries>
    <package android:name="com.google.android.youtube" />
    <package android:name="com.example.app" />
</queries>

Or simply we can allow for all packages (not recommended)

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" tools:ignore="QueryAllPackagesPermission" />

References

Package visibility filtering on Android

0

精彩评论

暂无评论...
验证码 换一张
取 消