2016-09-05 36 views
0

後,我想創建一個通知和時間 這裏後可以重複是我的代碼 NotifyService.class重複通知時間

public class NotifyService extends Service { 
private NotificationManager mNotificationManager; 
private Context ctx; 
private PendingIntent contentIntent; 
@Override 
public void onCreate() { 
    super.onCreate(); 
    ctx=this; 
    sendNotificationCmt(); 
} 
private void sendNotificationCmt() { 
    mNotificationManager = (NotificationManager) 
      ctx.getSystemService(Context.NOTIFICATION_SERVICE); 
    Intent in = new Intent(ctx, MainActivity.class); 
    contentIntent = PendingIntent.getActivity(ctx, 0, in, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA); 
    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(ctx) 
        .setSmallIcon(R.mipmap.ic_launcher) 
        .setContentTitle("hello") 
        .setStyle(new NotificationCompat.BigTextStyle() 
          .bigText("des")) 
        .setContentText("des1"); 

    mBuilder.setContentIntent(contentIntent); 
    mBuilder.setAutoCancel(true); 
    mNotificationManager.notify(2712, mBuilder.build()); 
} 


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

在MainActivity.class我叫:

Intent notificationIntent = new Intent(this, NotifyService.class); 
    PendingIntent contentIntent = PendingIntent.getService(this, 0, notificationIntent, 
      PendingIntent.FLAG_CANCEL_CURRENT); 

    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
    am.cancel(contentIntent); 
    am.setRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime(), 
       2 * 60 * 60, contentIntent); 

在AndroidManifest我補充說:

<service android:name="NotifyService"/> 

但是不重複,Wha發生在這裏,請幫助我。非常感謝

+0

我有同樣的問題。我通過以下鏈接實現了它。 [計劃通知](http://stackoverflow.com/questions/38716989/show-notification-when-xml-item-is-updated/39324437#39324437) – Aamir

+0

它延遲通知,但不會重複時關閉應用程序 –

回答