我在Youtube上发了一些教程通知。我认为一切正常,因为我看到了通知。问题是我在第二天等待通知。它不起作用。它没有出现。
这是一个带小时等的代码:
button1 = (Button)dialog.findViewById(R.id.btnRegister);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 20);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 30);
Intent intent = new Intent(getApplicationContext(), Notification_receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
哦,它开始在我的自定义对话框上使用onclick按钮,该按钮仅在第一次启动应用程序时出现。请帮帮我们!
NotificationReceiver java:
package com.justfashion;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationBuilderWithBuilderAccessor;
import android.support.v4.app.NotificationCompat;
/**
* Created by otsma on 24.11.2016.
*/
public class Notification_receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent repeating_intent = new Intent(context,ActivitySplashScreen.class);
repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context,100,repeating_intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(android.R.drawable.arrow_up_float)
.setContentTitle("Hey You!")
.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.sound))
.setContentText("Collect today's diamonds!")
.setAutoCancel(true);
notificationManager.notify(100,builder.build());
}
}
答案 0 :(得分:0)
使用Service
课程安排通知。在此处查找文档 - Service | Android Developers
答案 1 :(得分:0)
如果你的应用程序在此期间重新启动,警报会被取消。所以在另一天没有通知。所以使用广播接收器解决这个问题。请不要使用服务只显示通知。你的方法是正确的。
enter code here
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("BootReceiver","BootReceiver");
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
// Set the alarm here.
}
}
}
<receiver android:name=".alarmManager.BootReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
使用权限:
答案 2 :(得分:0)
您设置的闹钟可能出现问题 试试这个
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
如上所述[{3}}和here。
答案 3 :(得分:0)
在启动画面或主屏幕中实现此功能 此方法为特定时间设置警报。
public void startWeatherAlert() {
Calendar updateTime = Calendar.getInstance();
updateTime.setTimeZone(TimeZone.getTimeZone("IST"));
updateTime.set(Calendar.HOUR_OF_DAY, 8);//set hour of day when you want to trigger
updateTime.set(Calendar.MINUTE, 15);//set minute of day
Intent intent = new Intent(this, YourReceiverNameHere.class);//Broadcast receiver here
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);//INTERVAL_DAY means that it will trigger only once in a day.
}
现在设置广播接收器
public class YourReceiverNameHere extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//your logic here
//this can be used to invoke service, launch activity or to generate notification
}
}
最后在清单中注册该广播接收器
<service android:name=".YourReceiverNameHere" />