简体   繁体   English

在广播接收器中显示对话框

[英]Show dialog in broadcast receiver

I read other posts and I understood I can do it in broadcast class so I created other Activity, but it crashed.我阅读了其他帖子,我知道我可以在广播课中做到这一点,所以我创建了其他活动,但它崩溃了。 In logcat error is on this line context.startActivity(intent1);logcat错误是在这一行context.startActivity(intent1);

my Broadcast我的广播

private final BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();

        if (action.equals("android.intent.action.BATTERY_CHANGED")) {



                Intent intent1 = new Intent(context.getApplicationContext(),BatteryDialog.class);
                intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent1);



        }

    }
};

BatteryDialog.java电池对话框

public class BatteryDialog extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final Dialog dialog = new Dialog(BatteryDialog.this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.cooler_guide_dialog);
    TextView textViewGuide = (TextView) dialog.findViewById(R.id.textViewGuide);
    Button buttonOkDialog = (Button) dialog.findViewById(R.id.buttonOkDialog);
    textViewGuide.setText("Text");
    buttonOkDialog.setText("Button");
    buttonOkDialog.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            dialog.dismiss();
        }
    });
    dialog.show();
}}

Logcat逻辑猫

Process: com.rezaahmadpour.cooler, PID: 8104
                                                                    java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.BATTERY_CHANGED flg=0x60000010 (has extras) } in com.rezaahmadpour.cooler.BatteryChangeService$1@38f712a
                                                                        at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:894)
                                                                        at android.os.Handler.handleCallback(Handler.java:739)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                        at android.os.Looper.loop(Looper.java:148)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5451)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                     Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.rezaahmadpour.cooler/com.rezaahmadpour.cooler.BatteryDialog}; have you declared this activity in your AndroidManifest.xml?
                                                                        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1801)
                                                                        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1514)
                                                                        at android.app.ContextImpl.startActivity(ContextImpl.java:698)
                                                                        at android.app.ContextImpl.startActivity(ContextImpl.java:680)
                                                                        at android.content.ContextWrapper.startActivity(ContextWrapper.java:338)
                                                                        at com.rezaahmadpour.cooler.BatteryChangeService$1.onReceive(BatteryChangeService.java:93)
                                                                        at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:884)
                                                                        at android.os.Handler.handleCallback(Handler.java:739) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                        at android.os.Looper.loop(Looper.java:148) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5451) 
                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

In logcat the line java:93 is bluelogcat ,线java:93是蓝色的

You should declare all your activities in AndroidManifest.xml before using them as Intent.在将它们用作 Intent 之前,您应该在 AndroidManifest.xml 中声明所有活动。 Place the activity BatteryDialog in the Android manifest properly.将活动BatteryDialog正确放置在 Android 清单中。

Try something like this:尝试这样的事情:

<activity android:name= ".BatteryDialog" />

while calling the intent you just need to put it in a handler with a delay of 400ms在调用意图时,您只需要将其放入延迟 400 毫秒的处理程序中

new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
        
                        Intent intent1 = new 
                        Intent(context.getApplicationContext(),BatteryDialog.class);
                        intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent1);
                        }
                    }, 400);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM