zoukankan      html  css  js  c++  java
  • Android Broadcast Receiver

    说明

    有时候我们在做android系统软件的时候,经常会需要做的事就是开机重新设置上次关机前的状态,当然,我们就会用到这个开机广播:

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

    利用这个开机广播,我们可以将上次app设置的数据,开机后设置回系统,这样看上去系统就像上次的状态。

    Demo使用方法

    AndroidManifest.xml
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    <?xml version="1.0" encoding="utf-8"?>
        package="com.zengjf.ethernet"
        android:versionCode="1"
        android:versionName="1.0"
        android:sharedUserId="android.uid.system" >
    
        <uses-sdk
            android:minSdkVersion="14"
            android:targetSdkVersion="18" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/theme" >
            ......
            <receiver android:name="com.zengjf.ethernet.BootBroadcastReceiver">
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                    <category android:name="android.intent.category.HOME" />
                </intent-filter>
            </receiver>
        </application>
    
        <!-- 开机启动权限 -->
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    </manifest>
    
    BootBroadcastReceiver.java
    package com.zengjf.ethernet;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.res.Resources.Theme;
    import android.util.Log;
    
    public class BootBroadcastReceiver extends BroadcastReceiver{
        /**
         * 背景知识:当Android启动时,会发出一个系统广播,内容为ACTION_BOOT_COMPLETED,
         * 它的字符串常量表示为 android.intent.action.BOOT_COMPLETED。只要在程序中“捕捉”
         * 到这个消息,再启动之即可。记住,Android框架说:Don''t call me, I''ll call you back。
         * 我们要做的是做好接收这个消息的准备,而实现的手段就是实现一个BroadcastReceiver。
         */
        static final String action_boot="android.intent.action.BOOT_COMPLETED";
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(action_boot)){
                new EthernetManager(context).resetInterface();
            }   
        }
    }
    

    总结

    站在使用的角度来说,所有的Android的应用都是可以有这些功能的,像我们常用的一些软件,开机就开始跟踪我们的一些数据的后台程序,其一般都使用了这些功能。

  • 相关阅读:
    了解 NoSQL 的必读资料
    关于什么时候用assert(断言)的思考
    这次见到了一些大侠
    NetBeans 时事通讯(刊号 # 87 Jan 12, 2010)
    动态链接库dll,静态链接库lib, 导入库lib
    新女性十得 写得了代码,查得出异常
    记录系统乱谈
    新女性十得 写得了代码,查得出异常
    fullpage.js禁止滚动
    RunningMapReduceExampleTFIDF hadoopclusternet This document describes how to run the TFIDF MapReduce example against ascii books. This project is for those who wants to experiment hadoop as a skunkworks in a small cluster (110 nodes) Google Pro
  • 原文地址:https://www.cnblogs.com/zengjfgit/p/5451929.html
Copyright © 2011-2022 走看看