【问题标题】:Android app which reminds user in every morning每天早上提醒用户的安卓应用
【发布时间】:2017-04-21 22:19:24
【问题描述】:

我正在尝试做一个应用程序,它每天早上都会通过推送通知提醒用户。

我一直在使用 AlarmManager、BroadcastReceiver 和 IntentService。

在下面的代码中,我以 60 秒的间隔测试了我的 AlarmManager。

问题: 一切正常,直到我关闭我的应用程序,警报不再响起。

有什么想法可以下一步去哪里吗?

清单:

<service
    android:name=".IntentMentor"
    android:exported="false" >
</service>

<receiver android:name=".AlertReceiver"
    android:process=":remote">
</receiver>

主活动:

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);
// 1s is only for testing
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis, 1000*60, pIntent);

接收者:

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() );
    }
}

【问题讨论】:

  • 我在你的链接后面尝试解决方案。谢谢..但我仍然有同样的问题。应用未打开时,不会触发警报。
  • 确保您授予 WAKE_LOCK 权限
  • Try this set of code with your. 你可以改变这一行 "alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis, 1000*60, pIntent);"
  • 很抱歉成为这样的菜鸟。但我应该在“intentLR”中使用什么。我不明白那是什么意思。 "piLR = PendingIntent.getBroadcast(context, 0, intentLR, PendingIntent.FLAG_UPDATE_CURRENT);"

标签: android alarmmanager intentservice keep-alive


【解决方案1】:

试试这个我更新你的主要活动代码。现在试试这个。它对我来说很好用。

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

【讨论】:

  • 出于某种原因 alarm.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstMillis, 1000*60, pIntent);对我不起作用。 setAndAllowWhileIdle 只需要 3 个参数?我只是迷路了......
  • 我使用:alarm.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, interval, pIntent);而且它只会触发一次通知。
  • 您可以像这样删除 setAndAllow 的 firstmillis:alarm.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,1000*60, pIntent);
  • @Wiltson 试试this one
  • 是的,我意识到了这一点。我无法弄清楚这个 setAndAllow... 函数是如何工作的。当我启动我的应用程序时,第一个通知会立即触发。这是错的吗?它应该在 60 秒后触发吗?另一个问题是警报没有重复。只会触发一个通知。
【解决方案2】:

最后。经过十个小时的测试,失败,测试,失败......我回答了。

Protected Apps in Huawei Phones

如您所见,问题出在我的华为 P8 手机上。应用程序需要在受保护的应用程序列表中。现在,至少我测试过的几个解决方案都可以正常工作。

感谢大家的帮助。我认为安迪和其他解决方案效果很好。但是华为手机有这个特殊问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    • 2018-04-04
    • 2014-04-22
    • 1970-01-01
    相关资源
    最近更新 更多