【发布时间】:2019-07-23 17:55:12
【问题描述】:
我想使用广播接收器在特定时间发送通知。在这方面已经阅读了许多教程视频和答案,所有这些都很清楚。但我找不到以下代码的问题所在,因为 CODE 仍然不起作用。 所有这些都是基于“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>
【问题讨论】:
-
当你运行这段代码时会发生什么? onReceive() 会被调用吗?
-
我通过放置 Toast 消息对其进行了测试,似乎 onReceive() 没有调用,但是当我在清单中添加接收器的意图操作(例如 VOLUMECONTROLCHANGED)时,在音量更改时调用了广播。
-
确认一下 - 你等到 19:53 过去了?
-
是的,我很挑衅地改了它可能的时间,然后等待过去,但似乎有一个问题,我找不到它。我也用华为和三星设备测试过,结果是一样的
-
您能否更新问题以包含广播接收器的完整清单和完整类?