【问题标题】:Show Dialog using PendingIntent使用 PendingIntent 显示对话框
【发布时间】:2013-04-08 08:04:34
【问题描述】:

我正在处理日历事件提醒。 Android 没有原生的日历事件提醒,所以用户需要安装不同的日历应用。

现在这些应用程序可以在提醒事件上有所不同,例如可以显示提醒通知。现在我希望我在这些事件日历应用程序中以编程方式设置一个事件,并且在到达时间时不显示任何通知,而是会显示一条弹出消息,并带有警报声。那时我使用该站点的代码。它可以工作,但它会以通知的形式显示提醒。

代码如下:

接收时

void doReminderWork(Intent intent) {
    Long rowId = intent.getExtras().getLong(RemindersDbAdapter.KEY_ROWID);

    NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(this, ReminderEditActivity.class); 
    notificationIntent.putExtra(RemindersDbAdapter.KEY_ROWID, rowId); 

    PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT); 

    Notification note=new Notification(android.R.drawable.stat_sys_warning, getString(R.string.notify_new_task_message), System.currentTimeMillis());
    note.setLatestEventInfo(this, getString(R.string.notify_new_task_title), getString(R.string.notify_new_task_message), pi);
    note.defaults |= Notification.DEFAULT_SOUND; 
    note.flags |= Notification.FLAG_AUTO_CANCEL; 


    int id = (int)((long)rowId);
    mgr.notify(id, note);
}

现在我想显示一个对话框而不是通知,因此如何通过使用这些代码行来在对话框中使用这个待处理的意图。

 Intent notificationIntent = new Intent(this, ReminderEditActivity.class); 
notificationIntent.putExtra(RemindersDbAdapter.KEY_ROWID, rowId); 

PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT); 

【问题讨论】:

    标签: android android-pendingintent android-alarms android-event


    【解决方案1】:

    在您的 Receiver 类中,只需编写代码来显示对话框而不是通知。

    显示对话框的类:

    public class AlarmDialogPopUp extends Activity
    {
    
        private int m_alarmId;
    
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
    
            // Get the alarm ID from the intent extra data
            Intent intent = getIntent();
            Bundle extras = intent.getExtras();
    
            if (extras != null) {
                m_alarmId = extras.getInt("AlarmID", -1);
            } else {
                m_alarmId = -1;
            }
    
            // Show the popup dialog
            showDialog(0);
        }
    
        @Override
        protected Dialog onCreateDialog(int id)
        {
            super.onCreateDialog(id);
    
            // Build the dialog
            AlertDialog.Builder alert = new AlertDialog.Builder(this);
    
            alert.setTitle("ALARM REMINDER");
            alert.setMessage("Its time for the alarm ");
            alert.setCancelable(false);
    
            alert.setPositiveButton("Dismiss", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    AlarmDialogPopUp.this.finish();
                }
            });
    
            // Create and return the dialog
            AlertDialog dlg = alert.create();
    
            return dlg;
        }
    }
    

    在您的onReceive 中显示对话框:

    public void onReceive(Context context, Intent intent)
        {
              // Launch the alarm popup dialog
                Intent alarmIntent = new Intent("android.intent.action.MAIN");
    
                alarmIntent.setClass(context, AlarmDialogPopUp .class);
                alarmIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    
                // Pass on the alarm ID as extra data
                alarmIntent.putExtra("AlarmID", intent.getIntExtra("AlarmID", -1));
    
                // Start the popup activity
                context.startActivity(alarmIntent);
    
        }
    

    根据评论编辑:

    要播放声音,您需要使用 MediaPlayer,如下所示。

    AlarmDialogPopUp活动类的onCreate()中添加这一行来播放声音。

    MediaPlayer mediaPlayer; //global variable.
    
        mediaPlayer = MediaPlayer.create(this,R.raw.alarmsound);
    

    在对话框的onClick() 中添加以下行以停止声音:

    mediaPlayer.stop();
    mediaPlayer.release();
    

    希望这会有所帮助。

    【讨论】:

    • 感谢您的帮助。如果我想在显示对话框时显示声音
    • 你应该得到更多的 +1...谢谢。
    猜你喜欢
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    • 2020-04-05
    • 2019-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多