从调用服务的活动 [英] Calling an Activity from a Service
问题描述
我有一个包含服务方式报警服务的服务类。当报警服务被激活被称为这些方法。我想要做的是调用的意图,另一类是这些方法被调用的服务类(当闹钟响起)之一。它所做的就是调用的意图时,刚旗了错误。这只发生在当报警服务激活(在服务类中的方法)被调用的方法。这是因为类延伸服务
,而不是延伸活动
?我不知道,任何的IDE?
(下面是我的服务类,当意图另一个活动是所谓的在onStart
方法应用程序强制关闭。)
公共类MyAlarmService延伸服务{
@覆盖
公共无效的onCreate(){
// TODO自动生成方法存根
Toast.makeText(这一点,MyAlarmService.onCreate(),Toast.LENGTH_LONG).show();
} @覆盖
公众的IBinder onBind(意向意图){
// TODO自动生成方法存根
Toast.makeText(这一点,MyAlarmService.onBind(),Toast.LENGTH_LONG).show();
返回null;
} @覆盖
公共无效的onDestroy(){
// TODO自动生成方法存根
super.onDestroy();
Toast.makeText(这一点,MyAlarmService.onDestroy(),Toast.LENGTH_LONG).show();
} @覆盖
公共无效调用onStart(意向意图,诠释startId){
// TODO自动生成方法存根
super.onStart(意向,startId); Toast.makeText(这一点,MyAlarmService.onStart(),Toast.LENGTH_LONG).show(); ////////////////////////////////////////////////// //////////////////////////////// 意图I =新意图(com.exercise.AndroidAlarmService.HELLO);
startActivity(ⅰ); 这是故意发送到打开另一个类的活动。
** ////////////////////////////////////////////////// ///////////////////////////////
} @覆盖
公共布尔onUnbind(意向意图){
// TODO自动生成方法存根
Toast.makeText(这一点,MyAlarmService.onUnbind(),Toast.LENGTH_LONG).show();
返回super.onUnbind(意向);
}
}
一对LogCat中的这些错误是:
一月六日至24日:11:36.857:E / AndroidRuntime(10805):了java.lang.RuntimeException:无法启动意图{FLG =为0x4 = CMP服务com.exercise com.exercise.AndroidAlarmService.MyAlarmService@412f23f8。 AndroidAlarmService / .MyAlarmService(有演员)}:android.util.AndroidRuntimeException:从活动上下文之外调用startActivity()需要FLAG_ACTIVITY_NEW_TASK标志。难道这真的是你想要的吗?
块引用>解决方案您是否尝试过什么错误日志显示?
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
I have a service class for an alarm service containing the service methods. These methods are called when the alarm service is activated. What I want to do is to call an intent to another class in one of these methods that are called in the service class (when the alarm goes off). All it does is just flag up errors when calling the intent. This only happens in the methods that are called when the alarm service is activated (methods in service class). Is this because the class
extends Service
and notextends Activity
? I'm not sure, any ides?(Below is my service class, when the intent to another activity is called in the
onStart
method the app force closes.)public class MyAlarmService extends Service { @Override public void onCreate() { // TODO Auto-generated method stub Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show(); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show(); return null; } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show(); } @Override public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub super.onStart(intent, startId); Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show(); ////////////////////////////////////////////////////////////////////////////////// Intent i = new Intent("com.exercise.AndroidAlarmService.HELLO"); startActivity(i); The intent that is send to open another class, an activity. ** ///////////////////////////////////////////////////////////////////////////////// } @Override public boolean onUnbind(Intent intent) { // TODO Auto-generated method stub Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show(); return super.onUnbind(intent); } }
One of these errors on the LogCat is:
06-24 01:11:36.857: E/AndroidRuntime(10805): java.lang.RuntimeException: Unable to start service com.exercise.AndroidAlarmService.MyAlarmService@412f23f8 with Intent { flg=0x4 cmp=com.exercise.AndroidAlarmService/.MyAlarmService (has extras) }: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
解决方案Have you tried what the error log suggests?
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
这篇关于从调用服务的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!