【发布时间】:2017-08-11 17:18:15
【问题描述】:
我正在编写通知应用程序。要设置通知,我使用 AlarmManager。
一切似乎都运行良好,不幸的是在华为没有。 当用户关闭应用程序通知时不来(在其他设备上 LG、NEXUS 都运行良好)。
知道怎么解决吗?
intent = new Intent(context, AlarmReceiver.class);
sender = PendingIntent.getBroadcast(context, alarmId, intent, 0);
am.set(android.app.AlarmManager.RTC_WAKEUP, timeToAlarm, sender);
编辑
public class AlarmReceiver extends BroadcastReceiver {
private static final String TAG = "AlarmReceiver";
private PowerManager.WakeLock wakeLock;
@Override
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager) context
.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
wakeLock.acquire();
new NotificationUtils(context, null).postNotification(context.getString(R.string.notification_title), context.getString(R.string.notification_message));
unlock();
}
private void unlock() {
if (wakeLock != null)
if (wakeLock.isHeld())
wakeLock.release();
wakeLock = null;
}
}
【问题讨论】:
-
你说你关闭了你的应用程序。好的,但是后台有服务或广播接收器吗?如果你关闭应用程序 android 可以杀死它。
-
我只是把它从进程列表中刷出来。是否有可能仅在这种类型的设备上,在它杀死所有广播之后?如果是,我能做些什么来防止它?
-
你不能依赖你的应用来启动通知,因为它可以随时取消。您应该在后台实现广播接收器或服务并从中启动通知。在其他手机上可以,但是是随机的。
-
如你所见,我已经注册了AlarmReceiver
-
哦,好吧,如果 android.stackexchange.com/questions/152649/… 不起作用很奇怪:\
标签: android notifications huawei-mobile-services