2016-09-05 32 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

它延迟通知,但不会重复时关闭应用程序 –

回答