Android project combat (22): start another APP or restart this APP

Original: Android project combat (22): start another APP or restart this APP

1. Start another APP

At present, the company's project needs, a main APP, needs to open some small APPs. These small APPs are integrated with Unity, but they are still android programs (the package names of all small APPs are known).

I haven't done it before, and I checked the implementation method. In fact, it is quite simple, and the test can also be done.

 

The code is relatively simple, paste it directly:

     try {
            PackageManager packageManager = getActivity().getPackageManager();
            Intent intent=new Intent();
            intent = packageManager.getLaunchIntentForPackage( " com.maiji.textviewchangedemo " ); //The parameter here is the package name of the app you want to open
            startActivity(intent);
        } catch (Exception e) {
            Log.e( " Error opening another app " ,e.getMessage()); //Not opened, maybe the app to be opened is not installed, need to be processed here
        }

 

2. Restart the APP

This function is relatively rare. It is seen in QQ that will restart the APP after setting the font size.

Code:

                Intent intent = getBaseContext().getPackageManager()
                        .getLaunchIntentForPackage(getBaseContext().getPackageName());
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325034670&siteId=291194637