2

我在前台服务、alarmManager 和通知方面遇到了一些问题。

基本上我正在尝试提供每天早上发出通知的服务。我正在尝试使用 foregroundService 和 cpuWakeLock 执行此操作,但出于某种原因,我仍然无法每天早上收到此通知启动。在模拟器中一切正常。

使用前台服务的主要原因是,如果没有它,我还没有开始工作,当应用程序关闭时警报会继续触发。

我有点绝望

这是我的代码(清理版)

public class MentorService_cleaned extends Service {
NotificationManager notificationManager;

private PowerManager.WakeLock cpuWakeLock = null;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    super.onCreate();

    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);
    Notification notification = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.pficon)
            .setContentTitle("xxx")
            .setContentText("xxx")
            .setContentIntent(pendingIntent).build();

    notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;

    startForeground(1337, notification);


    Calendar alarmStartTime = Calendar.getInstance();
    Calendar now = Calendar.getInstance();

    int alarmHour = 7;
    int alarmMinute = 0;

    alarmStartTime = Calendar.getInstance();
    alarmStartTime.set(Calendar.HOUR_OF_DAY, alarmHour);
    alarmStartTime.set(Calendar.MINUTE, alarmMinute);
    alarmStartTime.set(Calendar.SECOND, 0);
    if (alarmStartTime.before(now)) { //if it's after 7:00 move to next day
        alarmStartTime.add(Calendar.DATE, 1);
    }

    Intent _Intent = new Intent(getApplicationContext(), AlertReceiver.class);
    final PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, _Intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    alarm.set(AlarmManager.RTC_WAKEUP, alarmStartTime.getTimeInMillis(), pIntent);


    // test pushNotification
    pushNotification();

    return START_STICKY;
}

@Override
public void onCreate() {

    try {
        if ((cpuWakeLock == null) || cpuWakeLock.isHeld() == false) {
            PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
            PowerManager.WakeLock cpuWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ShakeEventService onCreate Tag");
            cpuWakeLock.acquire();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (this.cpuWakeLock.isHeld())
        this.cpuWakeLock.release();
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}


public void pushNotification(){

    try {

        NotificationCompat.Builder notificBuilder = new NotificationCompat.Builder(this);
        notificBuilder.setContentTitle("xxx");
        //notificBuilder.setContentText(nextCard.caption);
        //notificBuilder.setTicker(nextCard.caption);

        notificBuilder.setContentText("xxx");
        notificBuilder.setTicker("xxx");

        notificBuilder.setSmallIcon(R.drawable.book);
        notificBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);

        Intent intent = new Intent(this, MainActivity.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));

        if (PFContents.profile.Notification) {
            notificationManager.notify(1, notificBuilder.build());
        }
    }catch(Exception e)
    {

    }

}}

警报接收器级

public class AlertReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context ctxt, Intent i) {

    Intent in = new Intent(ctxt, MentorService.class);
    in.putExtra("fromMain", "false");
    ctxt.startService(in);
}

}

这里是我的清单文件:

<uses-permission android:name="android.permission.WAKE_LOCK" />
<service
            android:name=".MentorService"
            android:enabled="true"
            android:exported="true"
            android:process=".myprocess"
            android:stopWithTask="false" />
4

2 回答 2

0
  • 您不需要服务来传递通知。它会耗尽你的电池。
  • 使用警报管理器执行您的任务,您可以在 24 小时后定期设置警报。
  • 当一个警报触发时 - 生成下一个警报。

除此之外,您可以尝试 GCMNetworkManager,它是 Alarmmanager 的扩展。

AlarManager 代码:

private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
...
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

// Set the alarm to start at 8:30 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);

// setRepeating() lets you specify a precise custom interval--in this case,
// 20 minutes.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
    1000 * 60 * 20, alarmIntent);

在这里,您的AlarmReceiver.class(即扩展 BroadcastReciever)将触发您可以做您的事情的地方: - 创建您的通知。- 如果需要,修改下一个警报。

于 2017-09-21T12:50:00.487 回答
0

重复闹钟——因为您希望每天早上都收到通知。

只要确保您以毫秒为单位正确传递时间,警报就会有效地工作。它也适用于背景。

于 2017-09-22T16:40:29.313 回答