从服务中的BroadcastReceiver启动活动 [英] Starting an Activity from BroadcastReceiver in Service

查看:132
本文介绍了从服务中的BroadcastReceiver启动活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在服务和广播接收器的帮助下在特定时间开始一项活动.问题是,一旦时间到了,我的应用就会崩溃.

I am trying to start an activity at a certain time with the help of a service and broadcastreceiver. The problem is, once the time hits, my app crashes.

这是我的AlarmService类,用于扩展Service 假设除了代码中说明的意图之外,其他所有东西都可以正常工作.

Here's my AlarmService class that extends Service Assume that everything works besides the intent stated in the code.

public void onCreate(){

    audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
    Intent i = new Intent(this, GoTime.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    myReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context c, Intent i) {    

          if(checkquery((currentTime < (startTime)))){

            // RINGER is set to Vibrate.
            audioManager.setRingerMode(1);  

            startActivity(i); //This crashes the app. If i take out this, everything works.
          }
        }
    };

    registerReceiver(myReceiver, new IntentFilter("setVibrate") );                    
    alarmManager = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));     
}

我认为它不起作用的原因是因为我已经有一个意图过滤器来使手机振动. 不知道该怎么办或如何更改它.

The reason i believe its not working is because i already have an intent filter to make the phone vibrate. Not sure what else to do or how to change it.

我想做的是,创建一个闹钟,一旦时间到,我希望它开始响铃,然后启动一个显示贪睡的活动.

What i am trying to do is, to create an alarm clock, and once a time hits, i want it to start ringing, then start an activity that would display the snooze.

它现在振动的原因仅仅是为了让它在我每次尝试对其进行测试时都不会响起.

The reason its on vibrate right now is just so it doesn't ring everytime i am trying to test it.

任何提示和建议将不胜感激.谢谢.

Any tips and advice would be appreciated. Thank you.

我尝试使用标志活动新任务,这是我的日志记录:

I've tried using the flag activity new task, and this is my logcat:

08-01 15:23:00.508: E/AndroidRuntime(2244): FATAL EXCEPTION: main
08-01 15:23:00.508: E/AndroidRuntime(2244): java.lang.RuntimeException: Error receiving broadcast Intent { act=setVibrate flg=0x14 (has extras) } in cs.AlarmManagerService$1@42093aa0
08-01 15:23:00.508: E/AndroidRuntime(2244):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:768)
08-01 15:23:00.508: E/AndroidRuntime(2244):     at android.os.Handler.handleCallback(Handler.java:725)
08-01 15:23:00.508: E/AndroidRuntime(2244):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-01 15:23:00.508: E/AndroidRuntime(2244):     at android.os.Looper.loop(Looper.java:137)
08-01 15:23:00.508: E/AndroidRuntime(2244):     at android.app.ActivityThread.main(ActivityThread.java:5293)
08-01 15:23:00.508: E/AndroidRuntime(2244):     at java.lang.reflect.Method.invokeNative(Native Method)
08-01 15:23:00.508: E/AndroidRuntime(2244):     at java.lang.reflect.Method.invoke(Method.java:511)
08-01 15:23:00.508: E/AndroidRuntime(2244):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
08-01 15:23:00.508: E/AndroidRuntime(2244):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
08-01 15:23:00.508: E/AndroidRuntime(2244):     at dalvik.system.NativeStart.main(Native Method)
08-01 15:23:00.508: E/AndroidRuntime(2244): 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?
08-01 15:23:00.508: E/AndroidRuntime(2244):     at android.app.ContextImpl.startActivity(ContextImpl.java:1235)
08-01 15:23:00.508: E/AndroidRuntime(2244):     at android.app.ContextImpl.startActivity(ContextImpl.java:1222)
08-01 15:23:00.508: E/AndroidRuntime(2244):     at android.content.ContextWrapper.startActivity(ContextWrapper.java:291)
08-01 15:23:00.508: E/AndroidRuntime(2244):     at cs.AlarmManagerService$1.onReceive(AlarmManagerService.java:70)
08-01 15:23:00.508: E/AndroidRuntime(2244):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:758)
08-01 15:23:00.508: E/AndroidRuntime(2244):     ... 9 more

Edit2:

好吧,经过更多研究,我尝试了:

Ok so after some more researching, i tried:

Intent mIntent = new Intent(getBaseContext(), GoTime.class);
mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(mIntent);

这一次,它奏效了. 谢谢您的所有时间!

and this time, it worked. Thank you for all of your time!

推荐答案

要在Activity Context之外启动Activity,您需要使用标志FLAG_ACTIVITY_NEW_TASK.将此添加到您的Intent

To start an Activity outside of an Activity Context you need to use the flag FLAG_ACTIVITY_NEW_TASK. Add this to your Intent

 Intent in = new Intent(this, GoTime.class);
 in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

如果这不能解决您的问题,请发布日志.

if this doesn't solve your problem then please post the logcat.

意图

这篇关于从服务中的BroadcastReceiver启动活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆