Android calls third-party apps, android third-party apps

Source: Internet
Author: User

Android calls third-party apps, android third-party apps

  1. Private List <Map <String, Object> list = null;
  2. Private PackageManager mPackageManager;
  3. Private List <ResolveInfo> mAllApps;
  4. Private Context mContext;
  5. /**
  6. * Check the system application and open
  7. */
  8. Private void openApp (){
  9. // Application filtering Conditions
  10. Intent mainIntent = new Intent (Intent. ACTION_MAIN, null );
  11. MainIntent. addCategory (Intent. CATEGORY_LAUNCHER );
  12. MPackageManager = mContext. getPackageManager ();
  13. MAllApps = mPackageManager. queryIntentActivities (mainIntent, 0 );
  14. // Sort by registration
  15. Collections. sort (mAllApps, new ResolveInfo. DisplayNameComparator (mPackageManager ));
  16. For (ResolveInfo res: mAllApps ){
  17. // The package name and main Activity of the application
  18. String pkg = res. activityInfo. packageName;
  19. String cls = res. activityInfo. name;
  20. // Open QQ
  21. If (pkg. contains ("qq ")){
  22. ComponentName componet = new ComponentName (pkg, cls );
  23. Intent intent = new Intent ();
  24. Intent. setComponent (componet );
  25. Intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK );
  26. MContext. startActivity (intent );
  27. }
  28. }
  29. }

  30. 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
    1. Intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK );

    In this way, you can start the Activity in the new task.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.