设置
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); try { js = new JSONArray(timeArray); String str = ""; for(int i=0;i<js.length();i++) { str = js.get(i).toString().replaceAll("T", " "); try { date = format1.parse(str); Log.i(TAG, format1.format(date)+""); calendar.setTime(date); Log.i(TAG, calendar.getTimeInMillis()+" calendar"); } catch (ParseException e) { e.printStackTrace(); } PendingIntent pendingIntent = PendingIntent.getBroadcast(Alarm.this, i, intent, 0); alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); }
接收
public class AlarmReceiver extends BroadcastReceiver { NotificationManager mn ; Notification notification; /* (non-Javadoc) * @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent) */ @Override public void onReceive(Context arg0, Intent data) { Log.d(Alarm.TAG, "the time is up,start the alarm..."); setStatusBar(arg0,R.drawable.smile,"作業時間","作業時間","ただ今新しい作業時間になりました。"); } public void setStatusBar(Context context,int iconImage, String statusBarText,String title,String content) { mn =( NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); notification = new Notification(iconImage,statusBarText, System.currentTimeMillis()); notification.defaults |= Notification.DEFAULT_LIGHTS; notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.message); PendingIntent contentIntent=PendingIntent.getActivity(context, 0, null, 0); notification.setLatestEventInfo(context,title, content, contentIntent); mn.notify(1, notification); } }
配置
<application android:icon="@drawable/icon" android:label="@string/app_name" > <receiver android:name="com.zzh.alermactivity.AlarmReceiver" android:process=":remote" /> <activity android:label="@string/app_name" android:name="com.zzh.alermactivity.Alarm" > <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application>