使用PendingIntent显示对话框 [英] Show Dialog using PendingIntent
问题描述
我正在处理日历事件提醒。 Android中没有本机的日历事件提醒,因此用户可以安装其他日历应用。
I am working on Calender Events reminder . There is no native Calender events reminder in Android so user install different calender apps.
现在,这些应用在提醒事件(例如可以显示提醒通知)上可以有所不同。现在,我希望我在这些事件日历应用程序中以编程方式设置一个事件,并且在达到指定时间后不显示任何通知,而是会显示一条弹出消息,并发出声音之类的警报。那时候我使用那个站点的代码。
Now these apps can be different on reminding events like on reminder notifications can be shown. Now I want that I set an event programmatically in these event calender apps and on time reached not show any notification rather a pop up message will be shown with alarm like sound. At that I using a code from that site . Its working but it showing reminders in form of notifications.
这里是代码:
OnReceive
OnReceive
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);
}
现在,我想显示一个对话框而不是通知,以便如何显示
Now I want to show a dialog box instead of notification so how it can be possible from using these lines of code that this pending intent should be used in dialogue box.
Intent notificationIntent = new Intent(this, ReminderEditActivity.class);
notificationIntent.putExtra(RemindersDbAdapter.KEY_ROWID, rowId);
PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
推荐答案
在您的接收器类中,只需编写代码即可显示对话框而不是显示通知。
In your Receiver class, just code to get the dialog to display instead of Notification.
显示对话框的类:
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
显示对话框:
In your onReceive
to show dialog :
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。
To play sound, you need to make use of MediaPlayer like below.
在 onCreate()
活动类的 AlarmDialogPopUp
播放声音。
MediaPlayer mediaPlayer; //global variable.
mediaPlayer = MediaPlayer.create(this,R.raw.alarmsound);
在 onClick()
停止声音的对话框:
mediaPlayer.stop();
mediaPlayer.release();
希望这会有所帮助。
这篇关于使用PendingIntent显示对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!