Android 在Service中调用Activity

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

上面就是在Service中调用Activity的方法,需要非常注意的一点是,一定要加上call.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  这一行,否则程序会出现
ANR的异常提示,这是因为在官方文档中对startActivity(Intent intent)方法有如下说明:

   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 froman 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.

文章摘自红黑联盟,再次注明出处

posted @ 2014-10-26 19:11  RoperLee  阅读(803)  评论(0)    收藏  举报