gpt4 book ai didi

java - 广播接收者永远不会收到 Intent

转载 作者:行者123 更新时间:2023-12-02 03:35:35 25 4
gpt4 key购买 nike

我在接收通知时遇到问题。我确信它已发送,因为我看到了发送日志。但我从未收到它,因为我从未看到日志或通知。我不确定自己做错了什么。我不想重复闹钟。
在 Marshmallow 上使用 Nexus 7。

PracticeWordsActivity.java

public void setAlarm(int day){
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

alarmIntent = new Intent(PracticeWordsActivity.this,AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);

//alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis()+15000, pendingIntent);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+15000, pendingIntent);
Log.e(TAG,"Lift off");

}

报警服务

public class AlarmService extends IntentService 
{

private static final int NOTIFICATION_ID = 1;
private static final String TAG = "AlarmService";
private NotificationManager notificationManager;
private PendingIntent pendingIntent;


public AlarmService() {
super("AlarmService");
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent,flags,startId);
}

@Override
protected void onHandleIntent(Intent intent) {
Log.e(TAG,"Alarm Service has started.");
Context context = this.getApplicationContext();
notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent mIntent = new Intent(this,MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("test", "test");
mIntent.putExtras(bundle);
pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Resources res = this.getResources();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

builder.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
.setTicker("TITLE")
.setAutoCancel(true)
.setContentTitle("TITLE2")
.setContentText("SUBJECT");

notificationManager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());

Log.e(TAG,"Notifications sent.");
}

AlarmReceiver.java

 public class AlarmReceiver extends WakefulBroadcastReceiver {
private static final String TAG = "AlarmReceiver";
Intent intent;
PendingIntent pendingIntent;
NotificationManager notificationManager;

@Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG, "BroadcastReceiver has received alarm intent.");
Intent service1 = new Intent(context, AlarmService.class);
context.startService(service1);
}
}

AndroidManifest.xml

在 AndroidManifest 中声明最后一个 Activity 后,我已经设置了接收器和服务。

...
</activity>
<receiver android:name=".AlarmReceiver"
android:enabled="true"/>
<service android:name=".AlarmService" />
</application>

最佳答案

您正在使用 AlarmManager.ELAPSED_REALTIME_WAKEUP 设置闹钟。 ELAPSED_REALTIME_WAKEUP 表示设备启动后耗时。如果您希望闹钟在设置后 15 秒触发,请使用:

alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+15000, pendingIntent);

alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+15000, pendingIntent);

此外,我发现您正在 WAKEUP 模式下触发闹钟,并且您正在广播接收器中启动一项服务。设备有可能在服务启动之前重新进入休眠状态。您可以使用WakefulBroadcastReceiver为了防止这种情况发生。

您无需担心重复闹钟,如果您使用相同的 Intent 设置新闹钟,则之前使用相同 Intent 的闹钟将被取消。

关于java - 广播接收者永远不会收到 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37488359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com