7

Tôi đang làm việc trên Lời nhắc sự kiện lịch. Không có lời nhắc sự kiện Lịch nào trong Android để người dùng cài đặt các ứng dụng lịch khác nhau.Hiển thị Hộp thoại bằng cách sử dụng PendingIntent

Giờ đây, các ứng dụng này có thể khác khi nhắc nhở các sự kiện như trên thông báo lời nhắc có thể được hiển thị. Bây giờ tôi muốn rằng tôi thiết lập một sự kiện lập trình trong các ứng dụng lịch sự kiện và thời gian đạt đến không hiển thị bất kỳ thông báo thay vì một cửa sổ pop up tin nhắn sẽ được hiển thị với báo động như âm thanh. Lúc đó tôi sử dụng mã từ trang đó. Nó hoạt động nhưng nó hiển thị lời nhắc dưới dạng thông báo.

Đây là mã:

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

Bây giờ tôi muốn hiển thị một hộp thoại thay vì thông báo vậy làm thế nào nó có thể được có thể từ việc sử dụng những dòng mã mà ý định cấp phát này nên được sử dụng trong hộp thoại.

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

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

Trả lời

15

Trong Lớp người nhận, chỉ cần mã để hộp thoại hiển thị thay vì Thông báo.

Lớp hiển thị Dialog:

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

Trong onReceive của bạn để hiển thị hộp thoại:

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

    } 

EDIT dựa trên nhận xét:

Để chơi âm thanh, bạn cần tận dụng diaPlayer như dưới đây.

Thêm dòng này trong onCreate() trong số AlarmDialogPopUp lớp hoạt động để phát âm thanh.

MediaPlayer mediaPlayer; //global variable. 

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

Thêm các dòng dưới đây trong onClick() của hộp thoại để ngăn chặn âm thanh:

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

Hope this helps.

+0

cảm ơn sự giúp đỡ. Nếu tôi muốn hiển thị âm thanh khi hộp thoại được hiển thị – User42590

+0

Đã chỉnh sửa câu trả lời của tôi. – Kanth

+0

Bạn xứng đáng hơn 1 ... Cảm ơn. –

Các vấn đề liên quan