Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

android - How to setup Alertbox from BroadcastReceiver

I have implemented alarm in android app. Alarm is working fine. Toast message is visible. Now I want to make Alert Box Notification to user.

Here is code from ReceiverActivity Class. which I tried

public class ReceiverActivity extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

// Code....


    new AlertDialog.Builder(context)
    .setTitle("Alert Box")
    .setMessage("Msg for User")
    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface arg0, int arg1) {
        // TODO Auto-generated method stub
            // some coding...
        }
    })
    .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) {
            arg0.dismiss();
    }
}).create().show();
}

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Although you can not show AlertDialog from Receivers because it needs ActivityContext.

You have an alternate solution to show an Activity like AlertDialog from Receiver. This is possible.

To start Activity as dialog you should set theme of activity in manifest as <activity android:theme="@android:style/Theme.Dialog" />

Style Any Activity as an Alert Dialog in Android


To start Activity from Receiver use code like

    //Intent mIntent = new Intent();
    //mIntent.setClassName("com.test", "com.test.YourActivity"); 
    Intent mIntent = new Intent(context,YourActivity.class) //Same as above two lines
    mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(mIntent);

And one more reason behind not using AlertDialog from receiver (Even if you managed to show AlertDialog) is

A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). Once your code returns from this function, the system considers the object to be finished and no longer active.

This has important repercussions to what you can do in an onReceive(Context, Intent) implementation: anything that requires asynchronous operation is not available, because you will need to return from the function to handle the asynchronous operation, but at that point the BroadcastReceiver is no longer active and thus the system is free to kill its process before the asynchronous operation completes.

In particular, you may not show a dialog or bind to a service from within a BroadcastReceiver. For the former, you should instead use the NotificationManager API. For the latter, you can use Context.startService() to send a command to the service. More...

So the better way is 'show notification' and alternate way is 'to use Activity as an Alert..'

Happy coding :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...