六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 56|回复: 0

Android开发片段–代码中设定系统定时

[复制链接]

升级  21.33%

22

主题

22

主题

22

主题

秀才

Rank: 2

积分
82
 楼主| 发表于 2013-1-14 18:13:03 | 显示全部楼层 |阅读模式
设置 

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 blocke.printStackTrace();} 接收

public class AlarmReceiver extends BroadcastReceiver {NotificationManager mn ;Notification notification;/* (non-Javadoc) * @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent) */@Overridepublic 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>
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表