Here's what I did: from my service instance, I run the following view:
final Intent dialog = new Intent(this, BackGroundDialogs.class);
dialog.setType("text/plain");
dialog.putExtra(android.content.Intent.EXTRA_TEXT, reason);
dialog.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(dialog);
as for the BackGround Dialogs class, it looks like this:
public class BackGroundDialogs extends Activity {
public BroadcastReceiver receiver;
public AlertDialog mAlert;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
IntentFilter filter = new IntentFilter();
filter.addAction(Consts.DISMISS_DIALOG);
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, final Intent intent) {
if (context == null) return;
if (intent.getAction().equals(Consts.DISMISS_DIALOG)) {
finish();
}
}
};
registerReceiver(receiver, filter);
}
public void showAlert(final String reason) {
mAlert = new AlertDialog.Builder(this).create();
mAlert.setCancelable(false);
TextView Msg_tv = new TextView(this);
Msg_tv.setTypeface(null, Typeface.BOLD);
Msg_tv.setTextSize(16.0f);
Msg_tv.setText(reason);
Msg_tv.setGravity(Gravity.CENTER_HORIZONTAL);
mAlert.setView(Msg_tv);
mAlert.setButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
});
mAlert.show();
}
@Override
protected void onResume() {
super.onResume();
Bundle extras = getIntent().getExtras();
String reason = extras.getString(Intent.EXTRA_TEXT);
if (reason.equalsIgnoreCase("DISMISS")) finish();
else showAlert(reason);
}
@Override
protected void onPause() {
super.onPause();
if (mAlert != null) if (mAlert.isShowing()) mAlert.dismiss();
finish();
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
}
BroadcastReceiver, , , .