简体   繁体   中英

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);

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 blue

You should declare all your activities in AndroidManifest.xml before using them as Intent. Place the activity BatteryDialog in the Android manifest properly.

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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