castReceiver和AlarmManager在特定时间进行

castReceiver和AlarmManager在特定时间进行

本文介绍了使用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活动中的代码:

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);
       }
}

这是BroadcastReceiver的代码:

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在特定时间进行通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 09:05