Android calls Activity in Service

Source: Internet
Author: User

A few days ago, I made a small application and needed to call the Activity in the service. However, I found that ANR is always present and Baidu has found various statements. However, after trying, I found that the problem was not serious, you only need to add a Code. The Code is as follows:


 

Public class XXXService extends Service {public void onCreate () {super. onCreate (); // call Intent call = new Intent ("android. intent. action. CALL ", Uri. parse ("tel: 110"); call. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK); startActivity (call) ;}} public class XXXService extends Service {public void onCreate () {super. onCreate (); // call Intent call = new Intent ("android. intent. action. CALL ", Uri. parse ("tel: 110"); call. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK); startActivity (call );}

The key statement is call. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK). The reason may be that the android system considers stability (ANR is prone ).

 


You can refer to the startActivity () method descriptions in the official documentation:

Note that if this method is being called from outside of an Activity Context, then the Intent must include the FLAG_ACTIVITY_NEW_TASK launch flag. This is because, without being started from
An existing Activity, there is no existing task in which to place the new activity and thus it needs to be placed in its own separate task.

 


The general meaning is as follows: Note that if an external Activity Context calls this method, the Intent object must contain the FLAG_ACTIVITY_NEW_TASK flag. This is because, the Activity to be created is not started from an existing Activity (the Activity is not in the task stack). It does not have an existing task, therefore, it needs to be placed in its own independent tasks (that is, to create a new task in the task stack ).

 


 

Related Article

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.