2 myq 26 myq_26 于 2013.06.09 18:24 提问

字符串的date转换为alarm

我想设置alarms 功能,我把时间存储在一个cvs文件中。格式是hh:mm,如02:13。我想在这个时间点设置闹钟。

 SimpleDateFormat  dt = new SimpleDateFormat("hh:mm"); 
      dt.parse(str); // GET TIME HERE

NotificationManager manger = (NotificationManager) this.getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.launcher, "Mosque Prayer Times", time);
        PendingIntent contentIntent = PendingIntent.getActivity(this.getActivity(), 0, new Intent(this.getActivity(), AlarmReceiver.class), 0);
        notification.setLatestEventInfo(this.getActivity(), name, title, contentIntent);
        notification.flags = Notification.FLAG_INSISTENT;
        notification.sound = Uri.parse("android.resource://com.trib.app.mosqueprayertimes/raw/adhan.mp3");
        manger.notify(id, notification);        
    }

我如何从date 格式中提取时间然后在通知中设置?

2个回答

shaojie519
shaojie519   2013.06.09 18:40
已采纳
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
  SimpleDateFormat  dt = new SimpleDateFormat("hh:mm"); 
  Date date = new Date();  //当前时间日期
     try {
                 Date dateTime = dt.parse(str);
         date.setHours(dateTime.getHours());
             date.setMinutes(dateTime.getMinutes());

     } catch (ParseException e) {
            e.printStackTrace();
    }         
 am.set(AlarmManager.RTC_WAKEUP,date.getTime(), contentIntent);
curie_87
curie_87   2013.06.13 10:39

试一下:

SimpleDateFormat  dt = new SimpleDateFormat("hh:mm"); 
     Date date=  dt.parse(str); // GET TIME HERE
    Calender cal=Calender.getInstance();
    cal.setTime(date);
    String hours=cal.get(Calendar.HOUR);
    String minutes=cal.get(Calendar.MINUTE);
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!