java - 在特定时间发送通知

标签 java android

我有这个发送通知的代码。我从这里的一个论坛复制的!所以这段代码说在特定时间发送通知,但我无法发送它,我的代码有什么问题吗?

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

您可以通过此功能添加闹钟

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

希望对您有帮助。现在您可以在其中添加您的日历 Intent ,它将起作用。

关于java - 在特定时间发送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45886279/

相关文章:

java - Java switch 语句中需要的常量表达式

java - Hibernate 父表的分组依据

java - 否定复杂的正则表达式

java - 如果没有 'retrolambda' 语法,我该如何编写?

java - 如何获得 "For"操作的结果?

java - 我怎样才能批量插入?

android - AndEngine (Android) 中的多点触控缩放功能

android - ViewFlipper 中的 Textview 重叠问题..

java - Android Studio 验证在 API < 17 上运行的错误

Android 4.2 锁屏只有小部件是不可能的。漏洞?