gpt4 book ai didi

java - AlarmManager 似乎不起作用

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

我正在尝试安排使用 AlarmManager 调用的方法,但它似乎不起作用。我看过其他例子,但他们的例子不适合我。所以我认为这是我的代码中的一些内容。这是 AlarmManager 代码:

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(SplashScreenActivity.this, KinectReceiver.class);

PendingIntent pendingIntent = PendingIntent.getBroadcast(SplashScreenActivity.this, 0, intent, 0);

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 2);
calendar.set(Calendar.MINUTE, 25);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

还有我的广播接收器:

public class KinectReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Done", Toast.LENGTH_LONG).show();
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_stat_notify)
.setContentTitle("Kinect")
.setColor(ContextCompat.getColor(context, R.color.colorAccent))
.setWhen(System.currentTimeMillis())
.setContentText("Your Kinects and Likes have been refilled. Now get to swiping")
.setAutoCancel(true)
.setSound(uri)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("Your Kinects and Likes have been refilled. Now get to swiping"))
.setVibrate(new long[] { 100, 500, 100, 500, 100 });

Intent targetIntent = new Intent(context, MainActivity.class);
targetIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
targetIntent.putExtra("action", "main");
targetIntent.putExtra("id", "");
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
builder.setContentIntent(pendingIntent);

NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
}

Toast 和通知均不显示。

AlarmManager 代码是在我的启动器 Activity 中运行的第一件事,以防有帮助。谢谢

最佳答案

请像这样在 list 文件中注册您的接收器

在应用程序标签中

<receiver
android:name=".KinectReceiver">

关于java - AlarmManager 似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44124688/

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