我读了其他帖子,我知道我可以在广播课中这样做,所以我创建了其他活动,但它崩溃了。 logcat
错误出现在此行context.startActivity(intent1);
我的广播
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)
在logcat
中,java:93
行为蓝色
答案 0 :(得分:1)
在将它们用作Intent之前,您应该在AndroidManifest.xml中声明所有活动。将活动BatteryDialog
正确放入Android清单。
尝试这样的事情:
<activity android:name= ".BatteryDialog" />
答案 1 :(得分:0)
在调用意图时,您只需要将其放入延迟 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);