Android应用程序每天早晨提醒用户



我正在尝试执行应用程序,以提醒用户使用推送通知。

我一直在使用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);
    }
}

IntentService:

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

最后。经过十个小时的测试,失败,测试,失败。我伤口答案。

华为手机中受保护的应用

,如您所见,我的华为P8手机有问题。应用需要在受保护的应用程序列表中。现在,在少数解决方案中,我测试了工作正常。

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

最新更新