Android calls a third-party App

Source: Internet
Author: User

Android calls a third-party App

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

  33. 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] 
    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.