2017-06-19 84 views
2

我想让本地通知仅在本月的第一天,第二天和第三天出现,例如上午11点。我在12:30,14:30和18:30连续三天设置了通知,但在14:30仅显示一次。 当我查看那个小时的android显示器,它看起来像android只跳过那秒?! 这里是我的代码:Android本地通知有时不会显示

这里是我的服务,这是建立在主要活动中的onCreate

`@EService 
public class NotifiService extends Service { 
public int counter = 0; 

public NotifiService(Context applicationContext) { 
    super(); 
    Log.w("here!", "here i am"); 
} 
public NotifiService(){ 

} 




@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    super.onStartCommand(intent,flags,startId); 
    Log.i("-----b", "onStartCommand"); 

    startTimer(); 


    return Service.START_STICKY; //super.onStartCommand(intent, flags, startId); 

} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    Intent broadCastIntent = new Intent("com.Yyyyy.Xxxxx.Yyyyy.BootBro"); 
    sendBroadcast(broadCastIntent); 
    stopTimerTask(); 
} 

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

@Background 
public void checkDate(){ 
    Log.w("f", "if working"); 
    Calendar cal = Calendar.getInstance(); 
    int d = cal.get(Calendar.DAY_OF_MONTH); 
    int h = cal.get(Calendar.HOUR_OF_DAY); 
    int s = cal.get(Calendar.SECOND); 
    int m = cal.get(Calendar.MINUTE); 
    if(d==17 || d == 18 || d == 19){ 
     if (h == 12 || h == 14 || h == 16) { 

      if (m == 30) { 
       if (s == 30) { 


       Log.w("notify", "working>>>>"); 
        Intent intent = new Intent(getApplicationContext(), MainActivity_.class); 
        PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

        NotificationCompat.Builder b = new NotificationCompat.Builder(getApplicationContext()); 

        b.setAutoCancel(false) 
          .setDefaults(Notification.DEFAULT_ALL) 
          .setWhen(System.currentTimeMillis()) 
          .setSmallIcon(R.drawable.ic_app_icon) 
          .setTicker("FabReminder") 
          .setContentTitle("FabReminder") 
          .setContentText(getString(R.string.notification)) 
          .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND) 
          .setContentIntent(contentIntent); 
        //.setContentInfo("Tap this bar and select shop"); 


        NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); 
        notificationManager.notify(1, b.build()); 
       } 
      } 
     } 
    } 

} 
private Timer timer; 
private TimerTask timerTask; 
long oldTime=0; 
public void stopTimerTask(){ 
    if (timer != null){ 
     timer.cancel(); 
     timer = null; 
    } 
} 
public void initializeTimerTask(){ 
    timerTask = new TimerTask() { 
     @Override 
     public void run() { 
      checkDate(); 
     } 
    }; 
} 
public void startTimer(){ 
    timer = new Timer(); 
    initializeTimerTask(); 
    timer.schedule(timerTask,1000,1000); 
} 

@Override 
public void onTaskRemoved(Intent rootIntent) { 
    super.onTaskRemoved(rootIntent); 
    Log.w("killed task >>>"," task killed"); 
    Intent broadCastIntent = new Intent("com.Yyyyy.Xxxxx.Yyyyy.BootBro"); 
    sendBroadcast(broadCastIntent); 
}` 

这里是我的广播接收器:

'公共类BootBro扩展广播接收器{ 公众诠释MID;

@Override 
public void onReceive(Context context, Intent intent) { 
    //context.startService(new Intent(context, NotifiService_.class)); 
    Log.w(BootBro.class.getSimpleName(), "Service stops!"); 
context.startService(new Intent(context, NotifiService_.class)); 


} 

}`

和AndroidManifest

`<service android:name="com.Yyyyy.Xxxx.Yyyyy.NotifiService_" android:enabled="true"/> 
    <receiver android:name="com.Yyyyy.Xxxx.Yyyy.BootBro" android:enabled="true" 
     android:exported="true" 
     android:label="RestartServiceWhenStopped"> 

     <intent-filter><action android:name="android.intent.action.BOOT_COMPLETED"> 

     </action> 
      <action android:name="com.Yyyyy.Xxxx.Yyyy.BootBro"/> 
     </intent-filter> 
    </receiver>` 
+0

你介意以不同的方式做它吗? – AndroidStorm

+0

我认为你的选择听起来不错,非常感谢。我今天会检查一下:) – Gorthez

回答

1

这种方法,您可以轻松安装使用AlarmManager

这个类提供访问系统报警服务。这些允许您安排您的应用程序在未来的某一时刻运行...More

报警具有以下特点:

  • 他们让你在设定的时间和/或时间间隔触发意图。
  • 您可以将它们与广播接收器一起使用以启动服务并执行其他操作。
  • 它们在您的应用程序之外运行,因此即使您的应用程序未运行,即使设备本身处于睡眠状态,您也可以使用它们来触发事件或操作。
  • 它们可以帮助您最大限度地减少应用程序的资源需求。您可以调度操作,而不依赖定时器或连续运行的后台服务

Android官方文档有,您可以按照here

和这里是一个simple app一个例子的教程,你可以设置一个报警起床。