Android calls third-party apps, android third-party apps
- Private List <Map <String, Object> list = null;
- Private PackageManager mPackageManager;
- Private List <ResolveInfo> mAllApps;
- Private Context mContext;
- /**
- * Check the system application and open
- */
- Private void openApp (){
- // Application filtering Conditions
- Intent mainIntent = new Intent (Intent. ACTION_MAIN, null );
- MainIntent. addCategory (Intent. CATEGORY_LAUNCHER );
- MPackageManager = mContext. getPackageManager ();
- MAllApps = mPackageManager. queryIntentActivities (mainIntent, 0 );
- // Sort by registration
- Collections. sort (mAllApps, new ResolveInfo. DisplayNameComparator (mPackageManager ));
- For (ResolveInfo res: mAllApps ){
- // The package name and main Activity of the application
- String pkg = res. activityInfo. packageName;
- String cls = res. activityInfo. name;
- // Open QQ
- If (pkg. contains ("qq ")){
- ComponentName componet = new ComponentName (pkg, cls );
- Intent intent = new Intent ();
- Intent. setComponent (componet );
- Intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK );
- MContext. startActivity (intent );
- }
- }
- }
Many people encounter the following exceptions when using startActivity:
Caused by: android. util. AndroidRuntimeException: Calling startActivity () from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
I once met, but after studying it, I understood the principle and wrote it down. I thought I was confused by the same troubles of my brothers.
We all know that there is a startActivity method in the Context. The Activity inherits from the Context and the startActivity method is overloaded. If you use the startActivity method of Activity, there is no restriction. If you use the startActivity method of Context, you need to start a new task and encounter the exception above, it is because the startActivity method of Context is used. The solution is to add a flag.
[Java]View plaincopy
- Intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK );
In this way, you can start the Activity in the new task.