每天早上提醒用户的Android应用程序
我正在尝试做的应用程序,提醒用户每天早上推通知 我一直在使用AlarmManager、BroadcastReceiver和IntentService 在下面的代码中,我每隔60秒测试AlarmManager 问题: 一切正常,直到我关闭我的应用程序,警报不再触发 你知道下一步该去哪里吗 舱单:每天早上提醒用户的Android应用程序,android,alarmmanager,intentservice,keep-alive,Android,Alarmmanager,Intentservice,Keep Alive,我正在尝试做的应用程序,提醒用户每天早上推通知 我一直在使用AlarmManager、BroadcastReceiver和IntentService 在下面的代码中,我每隔60秒测试AlarmManager 问题: 一切正常,直到我关闭我的应用程序,警报不再触发 你知道下一步该去哪里吗 舱单: <service android:name=".IntentMentor" android:exported="false" > </service> <r
<service
android:name=".IntentMentor"
android:exported="false" >
</service>
<receiver android:name=".AlertReceiver"
android:process=":remote">
</receiver>
接收人:
public class AlertReceiver extends BroadcastReceiver {
private static final String TAG = "MyReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "MyReceiver on receive");
Intent i = new Intent(context, IntentMentor.class);
context.startService(i);
}
}
意向服务:
public class IntentMentor extends IntentService {
NotificationManager notificationManager;
int notifID = 33;
private static final String TAG = "MonitorService";
public IntentMentor() {
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d("TAG", "Service method was fired.");
pushNotification();
}
public void pushNotification(){
NotificationCompat.Builder notificBuilder = new NotificationCompat.Builder(this);
notificBuilder.setContentTitle("test");
notificBuilder.setContentText("this is text");
notificBuilder.setTicker("this is Ticker?");
notificBuilder.setSmallIcon(R.drawable.ic_info_black_24dp);
notificBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
Intent intent = new Intent(this, Card.class);
TaskStackBuilder tStackBuilder = TaskStackBuilder.create(this);
tStackBuilder.addParentStack(Card.class);
tStackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = tStackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
notificBuilder.setContentIntent(pendingIntent);
notificationManager = (NotificationManager) getSystemService((Context.NOTIFICATION_SERVICE));
notificationManager.notify(notifID, notificBuilder.build() );
}
}
试试这个,我更新你的主要活动代码。现在试试这个。它对我很好用
Intent intent = new Intent(getApplicationContext(), AlertReceiver.class);
final PendingIntent pIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
long firstMillis = System.currentTimeMillis();
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= 23)
{
alarm.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
firstMillis, 1000*60, pIntent);
}
else if (android.os.Build.VERSION.SDK_INT >= 19
&& android.os.Build.VERSION.SDK_INT < 23)
{
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP,
firstMillis, 1000*60, pIntent);
}
else
{
alarm.setRepeating(AlarmManager.RTC_WAKEUP,
firstMillis, 1000*60, pIntent);
}
Intent Intent=newintent(getApplicationContext(),AlertReceiver.class);
final PendingIntent pIntent=PendingIntent.getBroadcast(这个,0,
意向、挂起内容、标志(更新当前);
long firstMillis=System.currentTimeMillis();
AlarmManager alarm=(AlarmManager)this.getSystemService(Context.alarm\u服务);
如果(android.os.Build.VERSION.SDK_INT>=23)
{
alarm.SetAndAllowHileId(AlarmManager.Fassed\u REALTIME\u WAKEUP,
第一毫,1000*60,小帐篷);
}
else if(android.os.Build.VERSION.SDK_INT>=19
&&android.os.Build.VERSION.SDK_INT<23)
{
alarm.setInexactRepeating(AlarmManager.RTC_唤醒,
第一毫,1000*60,小帐篷);
}
其他的
{
alarm.setRepeating(AlarmManager.RTC_唤醒,
第一毫,1000*60,小帐篷);
}
最后。经过十个小时的测试,失败,测试,失败。。我没有回答
如你们所见,问题出在我的华为P8手机上。应用程序需要位于受保护的应用程序列表中。现在,至少有两种我测试过的解决方案可以正常工作
谢谢大家的帮助。我认为Andy和其他解决方案很好。但华为手机有一个特殊问题。我正在尝试您链接背后的解决方案。谢谢但我还是有同样的问题。当应用程序未打开时,不会触发警报。请确保您授予唤醒锁定权限,您可以更改此行“alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP,firstMillis,1000*60,pIntent)”;“很抱歉成为这样一个noob。。但是我应该在“intentLR”中使用什么呢。我不明白那是什么意思。“piLR=PendingEvent.getBroadcast(context,0,intentLR,PendingEvent.FLAG_UPDATE_CURRENT);”出于某种原因,alarm.SetAndAllowHileId(AlarmManager.Appeased_REALTIME_WAKEUP,firstMillis,1000*60,pIntent);对我不起作用。SetAndAllowHileIDLE只接受3个参数?我太迷路了…我使用:alarm.SetExactAndAllowHileIDLE(AlarmManager.Appeased\u REALTIME\u WAKEUP,interval,pIntent);它只触发一次通知。您可以删除setAndAllowHileId的第一个毫秒,如下所示:alarm.setAndAllowHileId(AlarmManager.Appeased\u REALTIME\u WAKEUP,1000*60,pIntent)@威尔森:是的,我意识到了。我不明白这是怎么设置的允许。。。功能正常。当我启动应用程序时,第一个通知会立即发出。错了吗?它应该在60秒后开火吗?另一个问题是警报没有重复。只有一个通知被触发。
Intent intent = new Intent(getApplicationContext(), AlertReceiver.class);
final PendingIntent pIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
long firstMillis = System.currentTimeMillis();
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= 23)
{
alarm.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
firstMillis, 1000*60, pIntent);
}
else if (android.os.Build.VERSION.SDK_INT >= 19
&& android.os.Build.VERSION.SDK_INT < 23)
{
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP,
firstMillis, 1000*60, pIntent);
}
else
{
alarm.setRepeating(AlarmManager.RTC_WAKEUP,
firstMillis, 1000*60, pIntent);
}