我有这段代码可以发送通知。我从这里的一个论坛复制了!所以这段代码说在一定时间内发送通知,但是我无法发送通知,我的代码有任何问题吗?
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 17);
calendar.set(Calendar.MINUTE, 51);
calendar.set(Calendar.SECOND, 20);
Intent intent1 = new Intent(Testes.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Testes.this, 0,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) Testes.this.getSystemService(Testes.this.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
AlarmReceiver.class:
public class AlarmReceiver extends BroadcastReceiver {
public static int MID = 0;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, Testes.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context).setSmallIcon(R.drawable.grades_icon)
.setContentTitle("Alarm Fired")
.setContentText("Events To be PErformed").setSound(alarmSound)
.setAutoCancel(true).setWhen(when)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
notificationManager.notify(MID, mNotifyBuilder.build());
MID++;
}
}
最佳答案
这是我的警报代码,效果很好。
public class Alarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "");
wl.acquire();
Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example
sendNotification("Alarm",context);
intent = new Intent();
intent.setClass(context, AlarmActivity.class); //AlarmActivity is a dummy class name where to redirect
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("msg","Task Pending");
context.startActivity(intent);
wl.release();
}
sendNotification(String s,Context c)
在下面给出。private void sendNotification(String body,Context context) {
Intent intent = new Intent(context, WelcomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0/*Request code*/, intent, PendingIntent.FLAG_ONE_SHOT);
//Set sound of notification
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
R.mipmap.ic_launcher_round);
NotificationCompat.Builder notifiBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setAutoCancel(true)
.setContentText("lalalalalala lalalalalal lalalalalalal lalalalaaa")
.setSound(notificationSound)
.setContentIntent(pendingIntent);
Notification not = notifiBuilder
.setContentTitle("The Big Notification")
.setContentText(body)
.setStyle(new NotificationCompat.BigPictureStyle(notifiBuilder)
.bigLargeIcon(icon))
.build();
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(123456 /*ID of notification*/, notifiBuilder.build());
}
然后在我的AlarmActivity中,代码如下。
public class AlarmActivity extends AppCompatActivity {
private Ringtone r;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
setContentView(R.layout.activity_alarm);
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
r = RingtoneManager.getRingtone(this, notification);
r.play();
ImageView buttonCancelAlarm = (ImageView) findViewById(R.id.buttonCancelAlarm);
buttonCancelAlarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cancelAlarm(Test.this);
finish();
}
});
}
public void cancelAlarm(Context context)
{
r.stop();
Intent intent = new Intent(context, Alarm.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 12345, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
您可以通过此
function
添加警报 public void setAlarm(Context context, long millisec)
{
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 17);
calendar.set(Calendar.MINUTE, 51);
calendar.set(Calendar.SECOND, 20);
AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, Alarm.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 12345, i, PendingIntent.FLAG_CANCEL_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Toast.makeText(context,"Alarm Saved!",Toast.LENGTH_SHORT).show();
}
希望对您有所帮助。现在,您可以在其中添加日历意图,它将起作用。