首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在android中设置定时闹钟

如何在android中设置定时闹钟
EN

Stack Overflow用户
提问于 2016-04-27 16:41:46
回答 1查看 432关注 0票数 0
代码语言:javascript
复制
public class SchedulerSetupReceiver extends WakefulBroadcastReceiver {
    private static final String APP_TAG = "com.hascode.android";

    private static final int EXEC_INTERVAL = 10 * 1000;
    DBhelper dbhelper;

    @Override
    public void onReceive(final Context ctx, final Intent intent) {
        Log.d(APP_TAG, "SchedulerSetupReceiver.onReceive() called");
        dbhelper = new DBhelper(ctx);


        String [] ID=dbhelper.FetchAllID();
        String[] time=dbhelper.FetchAlltime();

        for(int k=0;k<ID.length;k++)
        {

        AlarmManager alarmManager = (AlarmManager) ctx
                .getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(ctx, SchedulerEventReceiver.class); // explicit
                                                                    // intent
        PendingIntent intentExecuted = PendingIntent.getBroadcast(ctx, 0, i,
                PendingIntent.FLAG_CANCEL_CURRENT);
        Calendar now = Calendar.getInstance();
        now.add(Calendar.SECOND, 20);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                now.getTimeInMillis(), EXEC_INTERVAL, intentExecuted);


        }

    }

}

这是我的代码,重复闹钟每20秒,但我希望闹钟应该设置为3.30,4.50,8.30谁能建议我将改变设置重复方法,使闹钟将广播式每个3.30,4.50,8.30,以便我可以测试一些条件在广播中。

EN

回答 1

Stack Overflow用户

发布于 2016-04-27 20:28:21

以下是我的示例代码。我想这会对你有帮助

NotificationReceiver:

代码语言:javascript
复制
public class NotificationReceiver extends BroadcastReceiver {
    public static String NOTIFICATION_MESSAGE = "notificationMessage";
    public static int NOW_NOTIFICATION = 101;

    Context context;

    @Override
    public void onReceive(Context context, Intent intent) {

        this.context = context;

        String message = intent.getStringExtra(NOTIFICATION_MESSAGE);
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        //Define sound URI
        Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        Intent notificationIntent = new Intent(context, EmptyActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
        final DateTime dateTime = DateTime.now();
        int color = 0xffffaa00;
//        int color1 = context.getColor(R.color.notificatinBackgroundColor);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setSmallIcon(R.drawable.festi_push_message_small);
        builder.setContentIntent(pendingIntent);
        builder.setContentTitle("Notification Sample");
        builder.setAutoCancel(true);
        builder.setContentText(message);
        builder.setSound(soundUri);
        builder.setLights(Color.RED, 1000, 1000);
        builder.setColor(color);

        Notification notification = builder.build();

        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        notificationManager.notify(102938, notification);

        cancelNotification(NOW_NOTIFICATION);
        createNewNotification();
    }

    public void cancelNotification(int requestCode) {
        try {
            Intent notificationIntent = new Intent(context, NotificationReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            alarmManager.cancel(pendingIntent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void createNewNotification() {

        //Set next alarm date time (dynamically coming date and time) here
        final DateTime dateTime1 = DateTime.now();
        dateTime1.hourOfDay().setCopy(7);
        dateTime1.minuteOfHour().setCopy(30);
        dateTime1.secondOfMinute().setCopy(00);
        final Calendar calendarNotifiedTime1 = Calendar.getInstance();
        calendarNotifiedTime1.set(Calendar.HOUR, dateTime1.getHourOfDay());
        calendarNotifiedTime1.set(Calendar.MINUTE, dateTime1.getMinuteOfHour());
        calendarNotifiedTime1.set(Calendar.SECOND, dateTime1.getSecondOfMinute());
        calendarNotifiedTime1.set(Calendar.AM_PM, Calendar.AM);

        Intent intent = new Intent(context, NotificationReceiver.class);
        intent.putExtra(NotificationReceiver.NOTIFICATION_MESSAGE, "Sample Alert");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                context, NOW_NOTIFICATION, intent, PendingIntent.FLAG_ONE_SHOT);

        cancelNotification(NOW_NOTIFICATION);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendarNotifiedTime1.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

    }

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

https://stackoverflow.com/questions/36884602

复制
相关文章

相似问题

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