使用 BroadcastReceiver 和 AlarmManager 在特定时间通知 [英] Notification at Specific Time using BroadcastReceiver and AlarmManager

查看:28
本文介绍了使用 BroadcastReceiver 和 AlarmManager 在特定时间通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用广播接收器在特定时间发送通知.在这方面已经阅读了许多教程视频和答案,并且所有这些都很清楚.但我找不到下面代码的问题在哪里,因为 CODE 仍然不起作用.所有这些都是基于Android 中的每日重复本地通知 - YouTube"

I want to send notification at specific time using broadcast receiver. many tutorials videos and also answers has been read in this regard and all of them was clear. but I couldn't find where is the problem of below codes because CODE still does not work. all of this was implemented based on "Daily Repeating Local Notification In Android - YouTube"

这是 LAUNCHER Activity 中的代码:

public class Splash extends Activity {

@RequiresApi(Build.VERSION_CODES.N)
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    NotificationCheckPoint();

}

@RequiresApi(Build.VERSION_CODES.N)
private void NotificationCheckPoint() {

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 19);
    calendar.set(Calendar.MINUTE, 53);
    calendar.set(Calendar.SECOND, 0);

    Intent MyIntent = new Intent(getApplicationContext(), BroadastNotification.class);
    PendingIntent MyPendIntent = PendingIntent.getBroadcast(getApplicationContext(), 100,
            MyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager MyAlarm = (AlarmManager) getSystemService(ALARM_SERVICE);
    MyAlarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, MyPendIntent);
       }
}

这是广播接收器代码:

public class BroadastNotification extends BroadcastReceiver {

@Override
 public void onReceive(Context context, Intent intent) {
MyNotification(context);
}
 private void MyNotification(Context context) {
 String BigNotificqationText = "BigNotificqationText";
 String NotificationTitle = "NotificationTitle";
 String NotificationTicker = " NotificationTicker ";

 NotificationManager MyNotifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

 Intent MyIntent = new Intent(context, Splash.class);
 MyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

 PendingIntent MyPendingIntent = PendingIntent.getActivity(context, 100, MyIntent,
         PendingIntent.FLAG_UPDATE_CURRENT);

 NotificationCompat.Builder MyNB = new NotificationCompat.Builder(context);
 MyNB.setSmallIcon(R.drawable.icon);
 MyNB.setContentTitle(NotificationTitle);
 MyNB.setContentText(BigNotificqationText);
 MyNB.setTicker(NotificationTicker);
 MyNB.setPriority(NotificationCompat.PRIORITY_MAX);
 MyNB.setDefaults(NotificationCompat.DEFAULT_SOUND);
 MyNB.setAutoCancel(true);
 MyNB.setContentIntent(MyPendingIntent);

 Bitmap MyPicture = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);
 MyNB.setLargeIcon(MyPicture);

 NotificationCompat.BigPictureStyle MyPicStyle = new NotificationCompat.BigPictureStyle().bigPicture(MyPicture);
 MyPicStyle.setSummaryText("Etude can makes our life Enlightened");
 MyNB.setStyle(MyPicStyle);

 NotificationCompat.BigTextStyle MyTextStyle = new NotificationCompat.BigTextStyle();

 MyTextStyle.bigText(BigNotificqationText);
 MyTextStyle.setBigContentTitle(NotificationTitle);
 MyNB.setStyle(MyTextStyle);


 MyNotifyManager.notify(100, MyNB.build());

 }

最后是清单定义:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.ietude.etude">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:replace="icon, label">

    <receiver android:name=".BroadastNotification">
        <!--android:enabled="true"-->
        <!--android:exported="true">-->
        <!--<intent-filter>-->
            <!--<action android:name="android.media.VOLUME_CHANGED_ACTION" />-->
        <!--</intent-filter>-->
    </receiver>

    <activity android:name=".Splash"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

推荐答案

如果您希望计时器在确切的时间到期,那么您可以按如下方式配置计时器.

If you wish to have a timer expire at an exact time, then you can configure the timer as follows.

Calendar alarmFor = Calendar.getInstance();
alarmFor.set(Calendar.HOUR_OF_DAY, 7);
alarmFor.set(Calendar.MINUTE, 45);
alarmFor.set(Calendar.SECOND, 0);

Intent MyIntent = new Intent(getApplicationContext(), BroadcastNotification.class);
PendingIntent MyPendIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, MyIntent, PendingIntent.FLAG_CANCEL_CURRENT);

AlarmManager MyAlarm = (AlarmManager) getSystemService(ALARM_SERVICE);
MyAlarm.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmFor.getTimeInMillis(), MyPendIntent);

在这个例子中,onReceive()如下:

In this example, onReceive() is as follows:

@Override
public void onReceive(Context context, Intent intent) {
    Log.d("MyAPP", "onReceive() called");
}

当我运行它时,在请求的确切时间记录以下内容:

When I run this, the following is logged at the the exact time requested:

11-22 07:45:00.730 5833-5833/com.sap.myapplication D/MyAPP: onReceive() called

这篇关于使用 BroadcastReceiver 和 AlarmManager 在特定时间通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆