7

나는 캘린더 이벤트 알림을 작업 중입니다. 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

대화 상자 대신 알림의 표시 얻을 수 있습니다. 대화 상자가 표시됩니다

클래스 :

사운드를 재생하려면 : 코멘트를 기반으로

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

    } 

편집 : 당신의 onReceive에서

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

대화 상자를 표시합니다 너는 나를 사용해야한다. diaPlayer는 아래와 같습니다.

AlarmDialogPopUp 액티비티 클래스에이 라인을 추가하여 사운드를 재생하십시오.

MediaPlayer mediaPlayer; //global variable. 

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

사운드를 중지하려면 대화 상자의 onClick()에 아래 줄을 추가 :이 도움이

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

희망을.

+0

도움 주셔서 감사합니다. 대화 상자가 표시 될 때 소리를 표시하려면 – User42590

+0

내 대답을 편집했습니다. – Kanth

+0

당신은 +1 이상의 가치가 있습니다 ... 고마워. –

관련 문제