我在需要从服务类接收数据并将其传递给广播接收器的情况下工作。我可以在广播接收器中获取数据,但是无法显示AlertDialog。如何从广播接收器显示警报对话框。
Logcat
java.lang.RuntimeException: Error receiving broadcast Intent { act=com.demo.cisive.cisivedemo.A_CUSTOM_INTENT flg=0x10 launchParam=MultiScreenLaunchParams { mDisplayId=0 mBaseDisplayId=0 mFlags=0 } bqHint=1 (has extras) } in com.carco.cisive.Utils.BroadcastTokenCheck@f8d9855
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:1195)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6823)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1557)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:354)
at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:323)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:83)
at android.support.v7.app.AlertController.installContent(AlertController.java:231)
at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:278)
at android.app.Dialog.dispatchOnCreate(Dialog.java:478)
at android.app.Dialog.show(Dialog.java:336)
at com.carco.cisive.Utils.BroadcastTokenCheck.showDialog(BroadcastTokenCheck.java:54)
at com.carco.cisive.Utils.BroadcastTokenCheck.onReceive(BroadcastTokenCheck.java:33)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:1185)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6823)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1557)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
BroadcastTokenCheck.java
public class BroadcastTokenCheck extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String value=intent.getStringExtra("VALUE");
boolean isBackground = intent.getBooleanExtra("isBackground",false);
// if(!isBackground){
if(value.equals("Completing")){
Toast.makeText(context, value, Toast.LENGTH_SHORT).show();
showDialog(context,"Warning","Session is going to expire");
}
// }
}
public void showDialog(final Context cnt,String Title,String message){
final AlertDialog.Builder builder = new AlertDialog.Builder(cnt);
// Set the Alert Dialog Message
builder.setTitle(Title);
builder.setMessage(message)
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.dismiss();
}
})
.create()
.show();
}
}
答案 0 :(得分:0)
使用BroadCastReceiver中的对话框主题开始活动
startActivity(new Intent(this, MsgDialogActivity.class));
清单中带有以下主题:
<activity android:name=".MsgDialogActivity"
android:theme="@android:style/Theme.Dialog"/>
答案 1 :(得分:0)
问题是您试图显示AlertDialog
中的BroadcastReceiver
,这是不允许的。您无法显示AlertDialog
中的BroadcastReceiver
。只有活动可以显示对话框。
您应该执行其他操作,使BroadcastReceiver
像启动时一样在启动时启动,并启动活动以显示对话框。
您应该这样做。
public class NotifySMSReceived extends Activity
{
private static final String LOG_TAG = "SMSReceiver";
public static final int NOTIFICATION_ID_RECEIVED = 0x1221;
static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter(ACTION);
this.registerReceiver(mReceivedSMSReceiver, filter);
}
private void displayAlert()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?").setCancelable(
false).setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
private final BroadcastReceiver mReceivedSMSReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION.equals(action))
{
//your SMS processing code
displayAlert();
}
}
}
}
这只是一个示例,因为您没有告诉hpow您要使用广播接收器,但这是可以解决的。