Android 本地通知有时不会显示

标签 android android-notifications android-annotations

我想制作仅在每月的第 1、2 和 3 天出现的本地通知,例如上午 11 点。我在 12:30、14:30 和 18:30 连续设置了 3 天的通知,但它只在 14:30 显示一次。 当我检查那个小时的 android 监视器时,它看起来像 android 只是跳过那个秒?! 这是我的代码:

这是我在 onCreate 的主要 Activity 中设置的服务

`@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);
}`

这是我的广播接收器:

`公共(public)类 BootBro 扩展 BroadcastReceiver { 公共(public) 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 轻松设置这种方法。

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

警报具有以下特征:

  • 它们让您可以在设定的时间和/或间隔触发 Intent。
  • 您可以将它们与广播接收器结合使用以启动服务和执行其他操作。
  • 它们在您的应用程序之外运行,因此您可以使用它们来触发事件或操作,即使您的应用程序未运行,甚至设备本身处于 sleep 状态。
  • 它们可以帮助您最大限度地减少应用的资源需求。您可以在不依赖计时器或持续运行后台服务的情况下安排操作

官方 Android 文档有一个教程,您可以按照此教程进行操作 here

这里还有一个 simple app 的例子您可以设置闹钟来唤醒。

关于Android 本地通知有时不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44638419/

相关文章:

java - 如何将 AndroidAnnotations SharedPref 与 PreferenceActivity 一起使用?

java - Android/Java - 寻求带有textView的拖放GridView示例

使用微调器从国家名称中获取 Android 国家/地区代码

android - 从状态栏中的扩展通知中获取文本?

android - 您如何做到这一点,以便在流程结束时,正在进行的通知振动并播放声音?

android - Roboguice、AndroidAnnotations 和线程间的事件

加载失败的android虚拟设备

Android Studio proguard 不起作用

android - 自定义通知字幕文本不起作用 android

java - AndroidAnnotations Rest SourceHttpMessageConverter错误