首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何设置每天设置的重复通知8时钟?

如何设置每天设置的重复通知8时钟?
EN

Stack Overflow用户
提问于 2021-04-20 21:13:14
回答 1查看 21关注 0票数 0

我需要每天收到8个时钟的重复通知。我已经修复了它的一些编码,我看到了一个错误。下面是一些精选。如何设置8点的闹钟?

AlarmActivity

代码语言:javascript
复制
public class AlarmActivity extends AppCompatActivity {
private AlarmManager alarmManager;
private PendingIntent pendingIntent;
private int REQUEST_CODE = 45645;
private String id = "myname";
private long timeInMilliSeconds = 30;
static String APP_TAG = "classname";
private BroadcastReceiver broadcastReceiver;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    initAlarm();
}

private void initAlarm() {

    //creating intent
    Intent intent = new Intent(id);
    boolean alarmRunning = PendingIntent.getBroadcast(
            this,
            REQUEST_CODE,
            intent,
            PendingIntent.FLAG_NO_CREATE
    ) != null;

    //setting broadcast
    broadcastReceiver = new MyReceiver();
    registerReceiver(
            broadcastReceiver,
            new IntentFilter(id)
    );

    //setting alarm
    long ensurePositiveTime = Math.max(timeInMilliSeconds, 0L);
    pendingIntent = PendingIntent.getBroadcast(
            this,
            REQUEST_CODE,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT
    );

    //Check if alarm is already running
    if (!alarmRunning) {
        alarmManager.set(
                AlarmManager.RTC_WAKEUP,
                System.currentTimeMillis() + ensurePositiveTime,
                pendingIntent
        );

    } else {
        updateAlarm();
        Log.e("Alarm", "Alarm already running.!");
    }
}

private void updateAlarm() {

    //calculating alarm time and creating pending intent
    Intent intent = new Intent(id);
    long ensurePositiveTime = Math.max(timeInMilliSeconds, 0L);
    pendingIntent = PendingIntent.getBroadcast(
            this,
            REQUEST_CODE,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT
    );

    //removing previously running alarm
    alarmManager.cancel(pendingIntent);
    unregisterReceiver(broadcastReceiver);

    //setting broadcast
    broadcastReceiver = new MyReceiver();
    registerReceiver(
            broadcastReceiver,
            new IntentFilter(id)
    );

    //Check if alarm is already running
    alarmManager.set(
            AlarmManager.RTC_WAKEUP,
            System.currentTimeMillis() + ensurePositiveTime,
            pendingIntent
    );

    Log.e("Alarm", "Alarm updated..!");

}

/**
 * Use this to cancel alarm
 */
private void cancelAlarm() {


    if (pendingIntent != null) {
        alarmManager.cancel(pendingIntent);
    }
    if (broadcastReceiver != null) {
        unregisterReceiver(broadcastReceiver);
        broadcastReceiver = null;
    }

    Log.e("Alarm", "Alarm has been canceled..!");
}

}

MyReceiver

代码语言:javascript
复制
class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        WakeLocker.acquire(context);
        createNotification(context);
        WakeLocker.release();
    }

    /***
     * It creates notification
     * @param context
     */
    private void createNotification(Context context) {
        String channelId = "fcm_default_channel";
        String channelName = "notification";

        Uri defaultSoundUri =
                RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, channelId)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentText("I am alarm from my activity")
                .setContentTitle(context.getString(R.string.app_name))
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setPriority(NotificationCompat.PRIORITY_HIGH);

        NotificationManager mNotificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel =
                    new NotificationChannel(
                            channelId,
                            channelName,
                            NotificationManager.IMPORTANCE_HIGH
                    );
            notificationBuilder.setChannelId(channelId);
            mNotificationManager.createNotificationChannel(channel);
        }

        Notification notification = notificationBuilder.build();
        mNotificationManager.notify(0, notification);
    }

}

WakeLocker

代码语言:javascript
复制
public abstract class WakeLocker {
    private static PowerManager.WakeLock wakeLock;

    public static void acquire(Context c) {
        if (wakeLock != null) wakeLock.release();

        PowerManager pm = (PowerManager) c.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
                PowerManager.ACQUIRE_CAUSES_WAKEUP |
                PowerManager.ON_AFTER_RELEASE, AlarmActivity.APP_TAG);
        wakeLock.acquire();
    }

    public static void release() {
        if (wakeLock != null){
            wakeLock.release();
        }
        wakeLock = null;
    }
}

如何设置8点闹钟模式?如果你知道,我请求你的帮助。在此代码中,我只收到一次警报。

EN

回答 1

Stack Overflow用户

发布于 2021-04-21 13:27:52

只需修改初始化告警代码,更新告警即可

这将在每天上午8点触发警报

在initAlarm()中

代码语言:javascript
复制
private void initAlarm() {

    //creating intent
    Intent intent = new Intent(id);
    boolean alarmRunning = PendingIntent.getBroadcast(
            this,
            REQUEST_CODE,
            intent,
            PendingIntent.FLAG_NO_CREATE
    ) != null;

    //setting broadcast
    broadcastReceiver = new MyReceiver();
    registerReceiver(
            broadcastReceiver,
            new IntentFilter(id)
    );

    //setting alarm
    pendingIntent = PendingIntent.getBroadcast(
            this,
            REQUEST_CODE,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT
    );

    //Check if alarm is already running
    if (!alarmRunning) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 8);
        calendar.set(Calendar.MINUTE, 0);
        long startUpTime  = calendar.getTimeInMillis();

        alarmManager.setRepeating(
                AlarmManager.RTC_WAKEUP,
                startUpTime,
                AlarmManager.INTERVAL_DAY,
                pendingIntent
        );
    } else {
        updateAlarm();
        Log.e("Alarm", "Alarm already running.!");
    }
}

在updateAlarm()中

代码语言:javascript
复制
private void updateAlarm() {

    //calculating alarm time and creating pending intent
    Intent intent = new Intent(id);
    pendingIntent = PendingIntent.getBroadcast(
            this,
            REQUEST_CODE,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT
    );

    //removing previously running alarm
    alarmManager.cancel(pendingIntent);
    unregisterReceiver(broadcastReceiver);

    //setting broadcast
    broadcastReceiver = new MyReceiver();
    registerReceiver(
            broadcastReceiver,
            new IntentFilter(id)
    );

    //Check if alarm is already running
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 8);
    calendar.set(Calendar.MINUTE, 0);
    long startUpTime  = calendar.getTimeInMillis();

    alarmManager.setRepeating(
            AlarmManager.RTC_WAKEUP,
            startUpTime,
            AlarmManager.INTERVAL_DAY,
            pendingIntent
    );

    Log.e("Alarm", "Alarm updated..!");

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67179686

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档