0

날짜 선택 도구를 통해 선택한 특정 날짜에 특정 알림을 표시하려면 NotifyService를 앱에 포함 시켰습니다 ... 최근 알림은 이전 알림이 아닌 &입니다. 나는 또한 고유 알림 ID를주는 시도했지만 여전히 작동하지 않았다 ... 여러 알림이 작동하지 않습니다.

을 heres MainActivity -

public class MainActivity extends Activity { 
private ScheduleClient scheduleClient; 
private DatePicker picker; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    scheduleClient = new ScheduleClient(this); 
    scheduleClient.doBindService(); 

    picker = (DatePicker) findViewById(R.id.scheduleTimePicker); 
} 


public void onDateSelectedButtonClick(View v){ 
    int id = MyApp.preferences.getInt("notif", 0); 
    id++; 
    MyApp.preferences.edit().putInt("notif" , id).apply(); 

    int day = picker.getDayOfMonth(); 
    int month = picker.getMonth(); 
    int year = picker.getYear(); 

    Calendar c = Calendar.getInstance(); 
    c.set(year, month, day); 
    c.set(Calendar.HOUR_OF_DAY, 0); 
    c.set(Calendar.MINUTE, 0); 
    c.set(Calendar.SECOND, 0); 
    // Ask our service to set an alarm for that date, this activity talks to the client that talks to the service 
    scheduleClient.setAlarmForNotification(c); 

    Toast.makeText(this, "Notification set for: "+ day +"/"+ (month+1) +"/"+ year, Toast.LENGTH_SHORT).show(); 
} 

@Override 
protected void onStop() { 
    if(scheduleClient != null) 
     scheduleClient.doUnbindService(); 
    super.onStop(); 
} 

}

ScheduleCli 엔트 -

public class ScheduleClient { 

private ScheduleService mBoundService; 
private Context mContext; 
private boolean mIsBound; 

public ScheduleClient(Context context) { 
    mContext = context; 
} 


public void doBindService() { 
    mContext.bindService(new Intent(mContext, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE); 
    mIsBound = true; 
} 


private ServiceConnection mConnection = new ServiceConnection() { 
    public void onServiceConnected(ComponentName className, IBinder service) { 
     mBoundService = ((ScheduleService.ServiceBinder) service).getService(); 
    } 

    public void onServiceDisconnected(ComponentName className) { 
     mBoundService = null; 
    } 
}; 


public void setAlarmForNotification(Calendar c){ 
    mBoundService.setAlarm(c); 
} 


public void doUnbindService() { 
    if (mIsBound) { 
     // Detach our existing connection. 
     mContext.unbindService(mConnection); 
     mIsBound = false; 
    } 
} 

}

예약 서비스 -

public class ScheduleService extends Service{ 

public class ServiceBinder extends Binder { 
    ScheduleService getService() { 
     return ScheduleService.this; 
    } 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Log.i("ScheduleService", "Received start id " + startId + ": " + intent); 

    // We want this service to continue running until it is explicitly stopped, so return sticky. 
    return START_STICKY; 
} 

@Override 
public IBinder onBind(Intent intent) { 
    return mBinder; 
} 

// This is the object that receives interactions from clients. See 
private final IBinder mBinder = new ServiceBinder(); 


public void setAlarm(Calendar c) { 
    new AlarmTask(this, c).run(); 
} 

}

AlarmTask -

public class AlarmTask implements Runnable{ 

private final Calendar date; 
private final AlarmManager am; 
private final Context context; 

public AlarmTask(Context context, Calendar date) { 
    this.context = context; 
    this.am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
    this.date = date; 
} 

@Override 
public void run() { 
    Intent intent = new Intent(context, NotifyService.class); 
    intent.putExtra(NotifyService.INTENT_NOTIFY, true); 
    PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0); 

    // Sets an alarm - note this alarm will be lost if the phone is turned off and on again 
    am.set(AlarmManager.RTC, date.getTimeInMillis(), pendingIntent); 
} 

}

& 마지막으로 NotifyService - 그래서 여기

public class NotifyService extends Service { 

/** 
* Class for clients to access 
*/ 
public class ServiceBinder extends Binder { 
    NotifyService getService() { 
     return NotifyService.this; 
    } 
} 

// Unique id to identify the notification. 

public static final String INTENT_NOTIFY = "com.blundell.tut.service.INTENT_NOTIFY"; 
private NotificationManager mNM; 
private Notification notif; 

@Override 
public void onCreate() { 
    Log.i("NotifyService", "onCreate()"); 
    mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Log.i("LocalService", "Received start id " + startId + ": " + intent); 

    if(intent.getBooleanExtra(INTENT_NOTIFY, false)) 
     showNotification(); 

    return START_NOT_STICKY; 
} 

@Override 
public IBinder onBind(Intent intent) { 
    return mBinder; 
} 

// This is the object that receives interactions from clients 
private final IBinder mBinder = new ServiceBinder(); 

/** 
* Creates a notification and shows it in the OS drag-down status bar 
*/ 
private void showNotification() { 
    CharSequence title = "Alarm!!"; 
    int icon = R.drawable.ic_dialog_alert; 
    CharSequence text = "Your notification time is upon us.";  

    Notification.Builder notification = new Notification.Builder(NotifyService.this); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, SecondActivity.class), 0); 
    int Notif = MyApp.preferences.getInt("notif", 1); 

    notification.setContentTitle(title) 
    .setContentText(text) 
    .setSmallIcon(icon) 
    .setContentIntent(contentIntent); 

    notif = notification.getNotification(); 
    notif.flags |= Notification.FLAG_AUTO_CANCEL; 
    mNM.notify(Notif, notif); 

    stopSelf(); 
}} 

, 내가 예에 대한 일을 선택 31/12/2017는 다시 것 같아요 후 1 일이 제거 2018년 1월 1일에 다른 설정 NOTIF &을 설정 ... 어떤 도움을 이해할 수있을 것이다 :

답변

1

당신의 NotifyService 클래스에서 notify() 메소드 NotificationManager에 속하는 객체 mNM은 통지에 대해 다른 ID가 필요합니다. 그렇지 않으면 기존 통지를 대체합니다. NotificationManager.notify() docs here

을 읽으십시오.