Android Broadcast Receiver showing a dialog?

前端 未结 3 447
南笙
南笙 2021-01-22 03:39

I\'ve got an app that responds to an Alert - and I really need to show an alert with a password entry before going on to the next screen; the trouble is I don\'t seem to be able

相关标签:
3条回答
  • 2021-01-22 04:11

    Create an activity, using Theme.Dialog, and call startActivity() to open it. While you cannot use a "real" Dialog, you can create a UI that looks like a Dialog.

    Bear in mind that popping up an activity based on a broadcast may greatly irritate some users, who might not appreciate your dialog appearing in the middle of their game or text message conversation or whatever. You may wish to consider actually raising a Notification, then displaying the dialog when they open up that Notification.

    0 讨论(0)
  • 2021-01-22 04:17

    Go through the link below this is the complete example how to make a custom dialog and call it automatically and gets pop up when the internet gets down hope it works for you

    Click Here

    0 讨论(0)
  • 2021-01-22 04:27

    You can't open Dialog from Receivers because it need ActivityContext

    Alternate way You can open Acivity like dialog. Full example :

    1. MyReceiver.kt

      class AlarmReceiver : BroadcastReceiver {
        private val REMINDER_BUNDLE = "MyReminderBundle"
      
        override fun onReceive(context: Context?, intent: Intent?) {
          val mIntent = Intent(context, DialogActivity::class.java)
          mIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
          context?.startActivity(mIntent)
        }
      }
      
    2. AndroidManife.xml

      <activity android:name=".appview.settings.view.DialogActivity" android:excludeFromRecents="true" android:theme="@style/Theme.AppCompat.Dialog"/>

    3. DialogActivity.kt

      class DialogActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_dialog)
          this.setFinishOnTouchOutside(true)
      
          //  create custom view of your dialog  in activity_dialog
          // or you can direct call alert dialog 
        }
      }
      
    0 讨论(0)
提交回复
热议问题