Notification 监听开机 定时启动服务

通知:

NotificationManager nm = (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);// 获取Notificationmanager
        Notification notification = new Notification(R.drawable.icon ,info.getAsString(Args.Info.TITLE) , System.currentTimeMillis());//初始化Notification
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        Intent intent = new Intent(mContext , InfoActivity.class);//设置 意图
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent  pendingIntent = PendingIntent.getActivity(mContext, R.string.app_name, intent, PendingIntent.FLAG_UPDATE_CURRENT);// 使用PendingIntent 封装意图
        
        notification.setLatestEventInfo(mContext, "重要通知!", info.getAsString(Args.Info.TITLE)  ,pendingIntent);Notification 封装PendingIntent
        nm.notify(R.string.app_name, notification);//nm 调用通知

监听开机广播:

1、设置权限

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

2、注册广播

<receiver android:name=".service.BootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

 

3、实现监听

public class BootReceiver extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Trace.Log("----------BootReceiver start---");
        AlarmPush.runAlarmPush(context);
        Trace.Log("----------success start Alarm---");
    }

}

 

使用闹钟定时启动服务:

public static void runAlarmPush(Context context)
    {
        final int PUSH_TYPE_SHORT = 1;

       
firstTime = SystemClock.elapsedRealtime(); long shortTime = 1*60 * 1000; Intent intentServiceShort = new Intent(context, PushShortService.class); String key = context.getString(R.string.push); intentServiceShort.putExtra(key, PUSH_TYPE_SHORT); PendingIntent mAlarmSenderShort = PendingIntent.getService(context, 0,intentServiceShort, PUSH_TYPE_SHORT); AlarmManager am = (AlarmManager) context.getSystemService(Activity.ALARM_SERVICE); am.cancel(mAlarmSenderShort); am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, shortTime, mAlarmSenderShort); }

 RTC_WAKEUP: 在指定的时刻(使用UTC格式)唤醒设备来触发Intent。

 RTC       : 在一个显式的时间(使用UTC格式)触发Intent,但不唤醒设备。

 ELAPSED_REALTIME       :从设备启动后,如果流逝的时间达到总时间,那么触发Intent,但不唤醒设备。流逝的时间包括设备睡眠的任何时间。注意一点的是,时间流逝的计算点是自从它最后一次启动算起。
 
 ELAPSED_REALTIME_WAKEUP: 从设备启动后,达到流逝的总时间后,如果需要将唤醒设备并触发Intent。
 
posted @ 2012-08-20 16:48  lipeil  阅读(1290)  评论(0编辑  收藏  举报