Android应用程序,每天早上提醒用户

时间:2017-04-21 08:27:05

标签: android alarmmanager intentservice keep-alive

我正在尝试使用推送通知来提醒用户每天早上。

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

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

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

下一步要去哪里?

清单:

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

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

MainActivity:

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

2 个答案:

答案 0 :(得分:0)

试试这个我更新您的主要活动代码。现在尝试一下。它对我来说很好。

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

答案 1 :(得分:0)

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

Protected Apps in Huawei Phones

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

感谢大家的帮助。我认为Andy和其他解决方案运行良好。但华为手机有这个特殊问题。