7

我正在处理Calender事件提醒。在Android中没有本机日历事件提醒,因此用户可以安装不同的日历应用程序。使用PendingIntent显示对话框

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

这里是代码:

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

现在我要显示一个对话框,而不是通知,以便它如何能够使用这些代码,这个悬而未决的意图,应使用在对话框中。

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

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

回答

15

在你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); 

    } 

编辑基于注释:

播放声音,你需要利用我像下面的diaPlayer。

将此行添加到AlarmDialogPopUp活动类的onCreate()中播放声音。

MediaPlayer mediaPlayer; //global variable. 

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

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

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

希望这有助于。

+0

感谢您的帮助。如果我想在对话框显示时显示声音 – User42590 2013-04-08 08:49:14

+0

编辑我的答案。 – Kanth 2013-04-08 08:58:08

+0

你值得拥有超过+1 ...谢谢。 – 2015-05-06 17:12:51