gpt4 book ai didi

Android BroadcastReceiver 不启动

转载 作者:行者123 更新时间:2023-11-29 21:07:36 24 4
gpt4 key购买 nike

我正在尝试在给定时间开始通知。我输入了 BroadcastReceiver 但通知没有启动。也许我的错误在 list 中?感谢您的帮助。

 public void to_reminder(View v)
{
Calendar cal=Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 15);
cal.set(Calendar.MINUTE, 16);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, Notifica.class);
PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
am.cancel(pi);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 24*60*60*1000 , pi);
}

.

public class Notifica extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent arg1) {

showNotification(context);
}
private void showNotification(Context context) {
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, Login.class), 0);

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(0)
.setContentTitle("My notification")
.setContentText("Hello World!");
mBuilder.setContentIntent(contentIntent);
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);

NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());

}
}

.

 <!-- Broadcast receiver -->
<receiver android:name="reminder.Notifica"></receiver>


</application>

最佳答案

您需要添加 PendingIntent.getBroadcast 而不是 PendingIntent.getService。有关详细信息,请参阅 this

关于Android BroadcastReceiver 不启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24016370/

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