警报管理器和通知显示较晚

时间:2018-08-18 12:32:25

标签: android service broadcastreceiver alarmmanager android-pendingintent

我是Android开发的初学者。我正在开发一个健身应用程序。我有一个具有时间选择器和“设置通知”按钮的活动。在该活动上,用户选择时间以获取锻炼通知。我的问题是通知没有收到或延迟显示。例如,用户选择了7:12 AM。有时通知不会收到,或者如果收到则将在7.14或7.15。 这是我的代码

 mSetNotif.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent notifyIntent = new Intent(AlarmActivity.this,MyReceiver.class);
            pendingIntent = PendingIntent.getBroadcast
                    (AlarmActivity.this, NOTIFICATION_REMINDER_NIGHT, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            alarmManager = (AlarmManager) AlarmActivity.this.getSystemService(Context.ALARM_SERVICE);


            mHour=timePicker.getCurrentHour();
            mMintues=timePicker.getCurrentMinute();
            Calendar cal=Calendar.getInstance();
            cal.setTimeInMillis(System.currentTimeMillis());
            cal.set(Calendar.HOUR_OF_DAY,mHour);
            cal.set(Calendar.MINUTE,mMintues);

            assert alarmManager != null;
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),
                    1000 * 60 * 60 * 24, pendingIntent);
            Toast.makeText(AlarmActivity.this, "Notification Enabled", Toast.LENGTH_SHORT).show();
        }
    });

这是我的Receiver类

public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}

@Override
public void onReceive(Context context, Intent intent) {

    Intent intent1 = new Intent(context, MyNewIntentService.class);
    context.startService(intent1);
}

}

这是我的服务课程

public class MyNewIntentService extends IntentService {
private static final int NOTIFICATION_ID = 3;

public MyNewIntentService() {
    super("MyNewIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {
    Notification.Builder builder = new Notification.Builder(this);
    builder.setContentTitle("My Title");
    builder.setContentText("This is the Body");
    builder.setSmallIcon(R.drawable.ic_launcher_background);
    builder.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND);
    Intent notifyIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 2, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    //to be able to launch your activity from the notification
    builder.setContentIntent(pendingIntent);
    Notification notificationCompat = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
        notificationCompat = builder.build();
    }
    NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
    managerCompat.notify(NOTIFICATION_ID, notificationCompat);
}

}

这是清单文件的代码

  <receiver
        android:name=".MyReceiver"
        android:enabled="true"
        android:exported="false" >
    </receiver>
    <service
        android:name=".MyNewIntentService"
        android:exported="false" >
    </service>

请告诉我我想念的是什么或做错了什么?任何帮助或建议,将不胜感激和欢迎。 谢谢

0 个答案:

没有答案