zoukankan      html  css  js  c++  java
  • 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。
     
  • 相关阅读:
    红旗桌面版本最新行使方法和效果解答100例4
    红旗桌面版本最新运用行为和标题问题解答100例3
    红旗桌面版本最新运用方式和成效解答100例2
    Linux下Resin JSP MySQL的部署和设置配备铺排2
    在Linux下Turbomail简易快捷的装置体式格局
    企业级Linux红旗桌面版可以可能登岸日本
    从红旗5.0说起——看Linux的内存管理
    红旗Linux桌面4.1文本布置过程图解(一)
    Linux下Resin JSP MySQL的装置和设置装备安排1
    Ubuntu 8.04中竖立Apache PHP MySQL基本环境
  • 原文地址:https://www.cnblogs.com/lipeil/p/2647795.html
Copyright © 2011-2022 走看看