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