Android Broadcast Receiver showing a dialog?

前端 未结 3 449
南笙
南笙 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: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

    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 
        }
      }
      

提交回复
热议问题