1

예약 된 알림을 보내려고합니다. 모든 작업 제외 : 응용 프로그램이 활성화되어 있고 최소화 된 경우. 알림 자동은 사용자가 클릭을 기다리지 않고 활동을 시작합니다. reveive에상태 표시 줄 알림이 자동으로 작업을 시작합니다.

:

public void onReceive(Context context, Intent paramIntent) { 

     Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 


     NotificationManager notificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.cancelAll(); 
     Notification notification = new Notification(R.drawable.logo_f, context.getResources().getString(R.string.notification_text), System.currentTimeMillis()); 

     Intent notificationIntent = new Intent(context, TimeLeftActivity.class); 

     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
       | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

     PendingIntent intent = PendingIntent.getActivity(context, 0, 
       notificationIntent, 0); 

     notification.setLatestEventInfo(context, context.getResources().getString(R.string.notification_text), "", intent); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     notification.sound=alarmSound; 
     // Fire the notification 
     notificationManager.notify(1, notification); 


    } 

내 알림 시작 방법 : 키릴 응답 후

private void createScheduledNotification(int sec) 
    { 
     // Get new calendar object and set the date to now 
     Calendar calendar = Calendar.getInstance(); 
     calendar.setTimeInMillis(System.currentTimeMillis()); 
     // Add defined amount of days to the date 
     calendar.add(Calendar.SECOND, sec); 

     // Retrieve alarm manager from the system 
     AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(getBaseContext().ALARM_SERVICE); 

     // Every scheduled intent needs a different ID, else it is just executed once 
     int id = 1; 

     // Prepare the intent which should be launched at the date 
     Intent intent = new Intent(this, TimeAlarm.class); 

     // Prepare the pending intent 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
     alarmManager.cancel(pendingIntent); 
     // Register the alert in the system. You have the option to define if the device has to wake up on the alert or not 
     alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 
    } 

편집 할 수 있습니다. 오류가 계속 발생합니다. 알림은 대기중인 의도를 시작하고 클릭을 기다리지 않습니다. 당신은 당신이 이전 버전을 지원해야하는 경우 Notication.Builder

Notification noti = new Notification.Builder(mContext) 
    .setContentTitle("New mail from " + sender.toString()) 
    .setContentText(subject) 
    .setSmallIcon(R.drawable.new_mail) 
    .setLargeIcon(aBitmap) 
    .build(); 

을 사용한다 코드에서 사용되지 않는 API를 사용하면 NotificationCompat

를 사용할 수 있기 때문에

@Override 
    public void onReceive(Context context, Intent paramIntent) { 

     Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 


     NotificationManager notificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.cancelAll(); 


     Intent notificationIntent = new Intent(context, TimeLeftActivity.class); 

     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
       | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

     PendingIntent intent = PendingIntent.getActivity(context, 0, 
       notificationIntent, 0); 
     Notification notification = new NotificationCompat.Builder(context) 
       .setContentTitle(context.getResources().getString(R.string.notification_text)) 
       .setContentIntent(intent) 
       .setSound(alarmSound) 
       .build(); 

     notification.flags |= Notification.FLAG_AUTO_CANCEL; 

     // Fire the notification 
     notificationManager.notify(1, notification); 


    } 
+0

'PendingIntent 의도 = PendingIntent.getActivity (문맥, 0, notificationIntent, 0);'무엇에 대한 제로? 두 번째 0은 PendingIntent에서 일정해야합니다 –

답변

1

오류를 찾기 어렵다 업데이트

이것은 내 앱에서 가져온 샘플이며, 알림을 표시합니다. y를 클릭하면 인 텐트 추가 방법이 표시됩니다.

String message = context.getString(R.string.notif_message); 
    Intent notificationIntent = new Intent(AddBpRecordActivity.ADD_ACTION); 

    NotificationCompat.Builder nb = new NotificationCompat.Builder(context) 
      .setSmallIcon(R.drawable.ic_notif_logo) 
      .setContentTitle(message) 
      .setContentText(billet.comment) 
      .setDefaults(Notification.DEFAULT_ALL) 
      .setAutoCancel(true) 
     >>> .setContentIntent(PendingIntent.getActivity(context, (int) billet.id, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT)) 
      .setWhen(System.currentTimeMillis()); 
    Notification notification = nb.build(); 

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify((int) billet.id, notification); 
+0

인 텐트를 설정하는 방법은 무엇입니까? 그래서 핥아서 활동을 여는거야? 콘텐츠 의도를 설정 하시겠습니까? – sanevys

+0

@sanevys 답변을 –

+0

님이 자동으로 계속 열립니다. 전화 결함 일 수 있습니까? – sanevys