目录

  • 1 第一周 android简介
    • 1.1 第一课 Android基础入门1
    • 1.2 第二课 Android基础入门2
    • 1.3 第三课 Android UI开发介绍
  • 2 第二周 android布局
    • 2.1 第一课 Android 布局文件介绍1
    • 2.2 第二课 Android 布局文件介绍2
    • 2.3 第三课 Android 布局文件介绍3
  • 3 第三周 Android UI组件开发
    • 3.1 第一课 Button和ImageButton
    • 3.2 第二课 TextView和EditText
    • 3.3 第三课 RadioButton和CheckBox
  • 4 第四周 Android UI组件开发
    • 4.1 第一课 Spinner和ListView
    • 4.2 第二课 应用案例-手机信息页面
    • 4.3 第三课 Android生命周期
  • 5 第五周 Android组件通信
    • 5.1 第一课 Intent的介绍
    • 5.2 第二课 Intent实现各种系统功能
    • 5.3 第三课 应用案例-手机注册页面
  • 6 第六周 数据存储
    • 6.1 第一课 应用案例-新建联系人
    • 6.2 第二课 数据存储-简单文件存储
    • 6.3 第三课 SharedPreferences存储
  • 7 第七周 数据存储
    • 7.1 第一课 xml文件存储
    • 7.2 第二课 应用案例-植物百科
    • 7.3 第三课  ListView 组件高级使用
  • 8 第八周 SQLite数据库
    • 8.1 第一课 应用案例-应用列表
    • 8.2 第二课 使用SQLite数据库存储(创建数据库和表)
    • 8.3 第三课 使用SQLite数据库存储(对表数据操作)
  • 9 第九周 项目实战-商品购物车处理
    • 9.1 第一课 使用SQLite数据库存储(对表数据查询)
    • 9.2 第二课 商品购物车处理(布局)
    • 9.3 第三课 商品购物车处理(数据处理)
  • 10 第十周 内容提供者及广播
    • 10.1 第一课 内容提供者的使用
    • 10.2 第二课 广播概念以及如何使用
    • 10.3 第三课 自定义广播
  • 11 第十一周 广播和服务
    • 11.1 第一课 应用案例-电池电量
    • 11.2 第二课 应用案例-通话记录
    • 11.3 第三课 服务简介
  • 12 第十二周 服务应用
    • 12.1 第一课 应用案例—音乐播放器
    • 12.2 第二课 应用案例—地震监测
    • 12.3 第三课 获得手机SIM卡信息
  • 13 第十三周 网络编程
    • 13.1 第一课 网络编程入门
    • 13.2 第二课 使用HttpClient访问网络
    • 13.3 第三课 webview组件
  • 14 第十四周 网络编程应用案例
    • 14.1 第一课 消息机制简介
    • 14.2 第二课 应用案例-应用升级
    • 14.3 第三课 应用案例-应用升级
第一课 应用案例-电池电量

 电池电量

一、案例描述

1、考核知识点

    广播接收者简介

    广播接收者的创建与注册

2、练习目标

    广播的动态注册和销毁

3、需求分析

   广播分为两种注册方式:静态注册和动态注册,上一个案例讲解了静态注册,该案例将对广播进行动态注册与销毁。该案例主要讲解了监听设备的电量和网络状态并展示在界面上。

4、设计思路(实现原理)

1)   在代码中进行监听电池电量和网络状态广播的注册和注销;

2)   将获取到的状态显示在界面上。

二、案例实现

(1)电池电量程序

创建“电池电量”程序,首先创建MainActivity用于显示信息,该程序的界面如图所示。



程序主界面对应的布局文件(activity_main)如下所示:

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

   xmlns:tools="http://schemas.android.com/tools"

   android:layout_width="match_parent"

   android:layout_height="match_parent"

   tools:context=".MainActivity" >

   <LinearLayout

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_centerInParent="true"

       android:orientation="vertical" >

       <TextView

           android:id="@+id/tv_cell"

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="电池电量"

           android:textColor="#000000"

           android:layout_margin="10dp"

           android:textSize="18dp" />

       <TextView

           android:id="@+id/tv_netWork"

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="网络状态"

           android:textColor="#000000"

           android:layout_margin="10dp"

           android:textSize="18dp" />

   </LinearLayout>

</RelativeLayout>


MainActivity的代码如下所示:

 1  public class MainActivity extendsActivity {

 2      private TextView tv_cell, tv_netWork;

 3      private BatteryReceiver batteryReceiver;

 4      private ConnectivityReceiver connectivity;

 5      @Override

 6      protected void onCreate(Bundle savedInstanceState) {

 7          super.onCreate(savedInstanceState);

 8          setContentView(R.layout.activity_main);

 9          tv_cell = (TextView) findViewById(R.id.tv_cell);

 10         tv_netWork = (TextView) findViewById(R.id.tv_netWork);

 11         // 注册广播接受者java代码

 12         IntentFilter intentFilter = new IntentFilter(

 13                 Intent.ACTION_BATTERY_CHANGED);

 14         batteryReceiver = new BatteryReceiver();

 15         // 注册receiver

 16         registerReceiver(batteryReceiver, intentFilter);

 17         IntentFilter mFilter = new IntentFilter();

 18         mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);

 19         connectivity =  newConnectivityReceiver();

 20         registerReceiver(connectivity, mFilter);

 21     }

 22     /**

 23      * 广播接受者

 24      */

 25     private class BatteryReceiver extends BroadcastReceiver {

 26         @Override

 27         public void onReceive(Context context, Intent intent) {

 28             // TODO Auto-generated method stub

 29             // 判断它是否是为电量变化的Broadcast Action

 30             if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())){

 31                 // 获取当前电量

 32                 int level = intent.getIntExtra("level", 0);

 33                 // 电量的总刻度

 34                 int scale = intent.getIntExtra("scale",100);

 35                 // 把它转成百分比

 36                 tv_cell.setText("电池电量为" + ((level * 100) / scale) + "%");

 37             }

 38         }

 39     }

 40     private class ConnectivityReceiver extends BroadcastReceiver {

 41         @Override

 42         public void onReceive(Context context, Intent intent) {

 43             State wifiState = null;

 44             State mobileState = null;

 45             ConnectivityManager cm = (ConnectivityManager) context

 46                     .getSystemService(Context.CONNECTIVITY_SERVICE);

 47             wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)

 48                     .getState();

 49             mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)

 50                     .getState();

 51             if (wifiState != null && mobileState != null

 52                     && State.CONNECTED != wifiState

 53                     && State.CONNECTED == mobileState) {

 54  

 55                 Toast.makeText(context, "手机网络连接成功", 0).show();

 56                 tv_netWork.setText("手机网络连接成功");

 57                 // 手机网络连接成功

 58             } else if (wifiState != null && mobileState != null

 59                     && State.CONNECTED != wifiState

 60                     && State.CONNECTED != mobileState) {

 61                 // 手机没有任何的网络

 62                 Toast.makeText(context, "手机没有网络", 0).show();

 63                 tv_netWork.setText("手机没有网络");

 64             } else if (wifiState != null && State.CONNECTED== wifiState) {

 65                 // 无线网络连接成功

 66                 Toast.makeText(context, "无限网络连接成功", 0).show();

 67                 tv_netWork.setText("无限网络连接成功");

 68             }

 69         }

 70     }

 71     @Override

 72     protected void onDestroy() {

 73         super.onDestroy();

 74         unregisterReceiver(batteryReceiver);

 75         unregisterReceiver(connectivity);

 76     }

 77 }


2)运行程序

运行“电池电量”程序,结果如图7-7所示。



如图所示,运行程序之后,当前设备的电量和网络状态都显示在了界面上。

三、案例总结

1、广播可以进行静态注册和动态注册。

2、广播在代码中动态注册需要有unregisterReceiver()操作。