AlarmManager与Android的通知

编程入门 行业动态 更新时间:2024-10-11 13:20:43
本文介绍了AlarmManager与Android的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想给用户一个通知,在一定的时间每​​一天,所以我用一个AlarmManager一个通知。我有这样的:

I'm trying to give the user a notification each day on a certain time so I use an AlarmManager with a notification. I have this:

public void check_products(int hour, int minute){= Calendar calendar = Calendar.getInstance(); Intent intent = new Intent(MainActivity.this, AlarmReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE); am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000, pendingIntent); } public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); Intent notificationIntent = new Intent(context, MainActivity.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(context) .setContentTitle("Reminder") .setContentText("You need to eat") .setWhen(System.currentTimeMillis()) .setContentIntent(pendingIntent); notificationManager.notify(1, mNotifyBuilder.build()); } }

和在清单(根据应用程序的权限它说:设置闹钟):

And in the manifest (under the app permissions it says "set an alarm"):

<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/> <receiver android:name="com.example.check_products.AlarmReceiver"/>

在我主我称之为带参数的功能check_products,但无论我尝试做;它不显示任何东西...是否有人知道我在做什么错了?

In my main I call the function check_products with the parameters, but whatever I try to do; it doesn't show anything... Does someone know what I'm doing wrong?

我柠新到Android编程,所以我不知道如何将alarmmanager和通知相结合...

I'm verry new to android programming so I wouldn't know how to combine the alarmmanager and notifications...

推荐答案

在您的清单文件调用接收器。

Call receiver in your manifest file.

筛选

<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <receiver android:name="myPackage.AlarmReceiver" android:enabled="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"></action> </intent-filter> </receiver> </application>

试试这个例子整体

MainActivity.java

private PendingIntent pendingIntent; Intent intent = new Intent(MainActivity.this, AlarmReceiver.class); pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent , 0); AlarmManager littlefluppy = (AlarmManager) getSystemService(Context.ALARM_SERVICE); littlefluppy.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 2000, pendingIntent);

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver { public AlarmReceiver() { } @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Log.e("alram set.....",""); } }

更多推荐

AlarmManager与Android的通知

本文发布于:2023-11-27 11:34:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637893.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通知   AlarmManager   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!