我在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);
哦,它正在开始在我的自定义对话框上使用wiith onclick按钮,该对话仅在第一个应用程序启动下才是apear。请帮助我!
编辑
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());
}
}
使用Service
类安排您的通知。在此处找到文档-Service |Android开发人员
如果您的应用在此期间重新启动警报被取消。方法是正确的。
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>
使用权限:
您已设置的警报尝试这个
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
如下所述。
在飞溅或主屏幕中实现此功能此方法设置了一天中特定时间的警报。
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
}
}
最后在Menifest中注册该广播接收器
<service android:name=".YourReceiverNameHere" />