Getting "You need to use Theme.AppCompat (or child) theme with this action." trying to show AlertDialog from BroadcastReceiver

I send Broadcast from a service and then get it back from the BroadcastReceiver inner class. I have to show the AlertDialog based on some logic, but trying to do this I get this runtime error: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

Here's the MyBroadcastReceiver class:

 public class MyBroadcastReceiver extends BroadcastReceiver { public MyBroadcastReceiver(){ super(); } @Override public void onReceive(final Context context, Intent intent) { if (intent.getAction() != null && intent.getAction().equals(getString(R.string.broadcast_id))) { Intent intent1 = new Intent(MyService.this, MainActivity.class); intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent1); @Override public void run() { if (someCondition) { android.support.v7.app.AlertDialog.Builder builder1 = new android.support.v7.app.AlertDialog.Builder(getBaseContext()); builder1.setView(R.layout.dialog); builder1.setPositiveButton( "OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Intent notificationIntent = new Intent(getBaseContext(), Notification.class); notificationIntent.putExtra(Notification.NOTIFICATION, getNotificationGame()); PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), m, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE); alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, finalFutureInMillis, pendingIntent); postMethod(MainActivity.name, MainActivity.uidOfProfilePic, String.valueOf(MainActivity.currentLatDouble), String.valueOf(MainActivity.currentLngDouble), null, s, v, sA, requestID, user.getUid(), nP); dialog.cancel(); } }); final android.support.v7.app.AlertDialog alert11 = builder1.create(); alert11.setCancelable(false); alert11.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); // error on line below alert11.show(); } } } } } 

I searched the Internet for this and came up with one that suggests creating activity and making AlertDialog code in it, but, as you can see, m do some work when the user clicks OK and this code is extracted in Service , and I can’t get to him access from another action.

Please let me know what should I do now?

0
android android-service illegalstateexception android-alertdialog broadcastreceiver
Feb 17 '17 at 2:42 on
source share
1 answer

Hammad, the documentation says that you cannot launch a popup dialog in your onReceive () implementation. (as the answers say) stack overflow

Receivers are considered accepted and initiate a new process. I say that, since after a few seconds of processing the data, the receiver can be blocked by the system, so always remember when you impose some logic on it.

However, like this other answer

( https://stackoverflow.com/a/3776268/ ) (you can try the https://stackoverflow.com/a/3187758/ ) in the NotificationManager API.

Both links have good answers to achieve what you want.

Hope this helps.

0
Feb 17 '17 at 3:34 on
source share



All Articles