Android 安卓本地通知有时赢得';不显示

Android 安卓本地通知有时赢得';不显示,android,android-notifications,android-annotations,Android,Android Notifications,Android Annotations,我想制作仅在一个月的第一、第二和第三天出现的本地通知,例如上午11点。我在12:30、14:30和18:30设置了连续三天的通知,但只在14:30显示了一次。 当我检查安卓显示器那一小时的时候,看起来安卓只是跳过了那一秒?! 这是我的密码: 这是我的服务,它是在onCreate的主活动中设置的 `@EService public class NotifiService extends Service { public int counter = 0; public NotifiService(

我想制作仅在一个月的第一、第二和第三天出现的本地通知,例如上午11点。我在12:30、14:30和18:30设置了连续三天的通知,但只在14:30显示了一次。 当我检查安卓显示器那一小时的时候,看起来安卓只是跳过了那一秒?! 这是我的密码:

这是我的服务,它是在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扩展广播接收器{ 公共int 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>`
`
`

这种方法可以通过AlarmManager轻松设置

此类提供对系统报警服务的访问。这些服务允许您安排应用程序在将来某个时间运行

警报具有以下特征:

  • 它们可以让你在设定的时间和/或间隔发出意图
  • 您可以将它们与广播接收器结合使用来启动服务和执行其他操作
  • 它们在应用程序之外运行,因此即使应用程序未运行,甚至设备本身处于休眠状态,您也可以使用它们触发事件或操作
  • 它们可以帮助您最小化应用程序的资源需求。您可以调度操作,而无需依赖计时器或连续运行后台服务
Android官方文档中有一个教程,您可以遵循


这里还有一个例子,你可以设置闹钟来唤醒你。

你介意用不同的方式来做吗?我觉得你的选择听起来不错,非常感谢。我今天会检查:)