AlertDialog from within BroadcastReceiver?? Can it be done?

后端 未结 5 1420
轻奢々
轻奢々 2020-11-28 13:53

AlertDialog from within BroadcastReceiver? Can it be done? I am working on a app that will pop up a Dialog box if I get SMS message. I am trying to code this within a Broa

5条回答
  •  一生所求
    2020-11-28 14:21

    Principal issue: try to avoid placing time consuming functionalities into BroadcastReceiver. It should just receive and initiate further processing in bound Activity/Service.

    UPDATE:

    Please check following sources that might be helpful:

    Similar questions on StackOverflow:

    How to send data from BroadcastReceiver to an Activity in android?

    Android SMS receiver not working

    Android SDK demo example:

    android-sdk-windows\samples\android-8\ApiDemos\src\com\example\android\apis\os\SmsMessagingDemo.java

    And of course standard Android API documentation: http://developer.android.com/reference/android/content/BroadcastReceiver.html

    UPDATE2:

    Added app skeleton as it should look. Please note that no content view is defined. It is because your app will have transparent screen. To achieve that

    @android:style/Theme.Translucent

    is entered under Theme tag for this activity in AndroidManifest.xml.

    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();
                }
            }
        };    
    }
    

提交回复
热议问题