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。
     
  • 相关阅读:
    严重: Parse error in application web.xml file at jndi:/localhost/ipws/WEBINF/web.xml java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml
    Failed to install .apk on device 'emulator5554': timeout解决方法
    java.lang.NoClassDefFoundError:org.jsoup.Jsoup
    Conversion to Dalvik format failed: Unable to execute dex:解决方法
    apache Digest: generating secret for digest authentication ...
    Description Resource Path Location Type Project has no default.properties file! Edit the project properties to set one.
    android service随机自启动
    MVC3 安装部署
    EF 4.3 CodeBased 数据迁移演练
    SQL Server 2008开启sa账户
  • 原文地址:https://www.cnblogs.com/lipeil/p/2647795.html
Copyright © 2011-2022 走看看