【问题标题】:Notifications in specific time every day android每天特定时间的通知android
【发布时间】:2012-10-29 12:33:14
【问题描述】:

我想制作一个应用程序,让用户决定每天什么时候希望他通过通知提醒某事......

我想知道我应该如何在用户想要的特定时间触发通知,例如上午 7:00,让他点击通知并在特定活动中输入应用程序。但是当用户不想再收到任何通知时(点击按钮)我如何取消所有通知...?

我做了类似的东西

Intent intent = new Intent(this, main.class);
Bundle bundle = new Bundle();
bundle.putString("title", "Some Title");\
intent.putExtras(bundle);   
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
String body = "Tap to see the Activity"; 
String title = "Title of notification"; 
Notification n = new Notification(R.drawable.ic_launcher, body, System.currentTimeMillis());
n.setLatestEventInfo(this, title, body, pi);
n.defaults = Notification.DEFAULT_ALL;
nm.notify(uniqueID, n);

但直到现在都没有运气......

谢谢...

>

我正在这样做

        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

        int icon = R.drawable.notification_icon;
        CharSequence tickerText = "Ome TickerText"; 
        long when = System.currentTimeMillis();
        Context context = getApplicationContext();
        CharSequence contentTitle = "Some Title"; 
        CharSequence contentText = "Some Text"; 

        Intent notificationIntent = new Intent(context, LandingActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("title", "a title");
        bundle.putString("title2", "title number 2");
        bundle.putString("action", "tip");
        bundle.putString("greek", "somehting else");
        bundle.putInt("action_num", 2);
        notificationIntent.putExtras(bundle);
        alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        contentIntent = PendingIntent.getActivity(Notifications.this, 0, notificationIntent, 0);

        Notification notification = new Notification(icon, tickerText, when);
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        notification.defaults = Notification.DEFAULT_ALL;

        mNotificationManager.notify(UniqueID, notification);

        int hour = tp.getCurrentHour();
        int minutes = tp.getCurrentMinute();

        contentIntent = PendingIntent.getService(Notifications.this, 0, notificationIntent, 0);
        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minutes);
        calendar.set(Calendar.SECOND, 00);
        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), contentIntent);

但没有运气......

请帮忙?

【问题讨论】:

    标签: android alarmmanager android-notifications


    【解决方案1】:

    使用alarm manager 并将您的通知放在NotifyService 类中

    Intent myIntent = new Intent(Current.this , NotifyService.class);     
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    pendingIntent = PendingIntent.getService(ThisApp.this, 0, myIntent, 0);
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 12);
    calendar.set(Calendar.MINUTE, 00);
    calendar.set(Calendar.SECOND, 00);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY , pendingIntent);  //set repeating every 24 hours
    

    【讨论】:

    • 即使应用程序关闭,我如何在每天的特定时间显示之前使用条件进行检查
    【解决方案2】:

    我知道这是一个老问题,但我有类似的要求,这就是我所做的。我发现了当前系统时间和发送通知的预期时间之间的差异,以毫秒为单位,并在 android 服务中实现了一个可运行的。以下代码将出现在 Notifyservice 中。

    private Handler notificationTimer = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if(msg.what==0) {
                    sendNotifications();
            }
            super.handleMessage(msg);
        }
    };
    private Runnable notificationCaller = new Runnable() {
    
        @Override
        public void run() {
            Message msg = notificationTimer.obtainMessage();
            msg.what = 0;
            notificationTimer.sendMessage(msg);
        }
    };        
    
    
        Calendar calendar = Calendar.getInstance();
        Calendar timefornotification = Calendar.getInstance();
        timefornotification.set(Calendar.HOUR_OF_DAY, 12);
        timefornotification.set(Calendar.MINUTE, 30);
        timefornotification.set(Calendar.SECOND, 00);
    
        Long difference = calendar.getTimeInMillis() - timefornotification.getTimeInMillis();
    
        if(difference<0){
            notificationTimer.postDelayed(notificationCaller, -(difference));
        }
        else{
            notificationTimer.postDelayed(notificationCaller, difference);
        }
    
       public void sendNotifications(){
            ------Your code here-------
            notificationTimer.removeCallbacksAndMessages(null);
            notificationTimer.postDelayed(notificationCaller, 86400000);
            ------86400000 is one day. So it will repeat everyday-------
       }
    

    然后您可以从主类调用服务以在后台运行它。

       Intent service = new Intent(this, NotifyService.class);
        this.startService(service);
    

    【讨论】:

      猜你喜欢
      • 2019-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-26
      相关资源
      最近更新 更多