当前位置:   article > 正文

Android移动开发基础案例教程 第6章 BroadcastReceiver(广播接收者)_时钟演练拯救史迪仔

时钟演练拯救史迪仔


img

6.1 广播接收者简介

img

6.1.1 广播特点

  • Android系统中内置了很多广播,例如手机开机完成、电池电量不足时都会发送一条广播。

  • 为了监听来自系统或者应用程序的广播事件,Android系统提供了BroadcastReceiver(广播接收者)组件。

  • img

6.2 广播接收者入门

img

6.2.1广播接收者的创建

img

1.静态注册

img

<?xml version="1.0" encoding="utf-8"?>
     <manifest ……….  >
          <application ……… > 
              <receiver
                      android:name=".MyReceiver"
                      android:enabled="true"
                      android:exported="true" >
              </receiver> 
         </application>
      </manifest>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
2.动态注册

img

6.2.2 实战演练——拦截史迪仔电话

img

1.mainActivity
package cn.itcast.interceptcall;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
    private EditText et_ipnumber;
    private SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_ipnumber = (EditText) findViewById(R.id.et_ipnumber);
        // 创建SharedPreferences对象
        sp = getSharedPreferences("config", MODE_PRIVATE);
    }
    public void click(View view) {
        // 获取用户输入的拦截号码
        String number = et_ipnumber.getText().toString().trim();
        //创建Editor对象,保存用户输入的拦截号码
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("number", number);
        editor.commit();
        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
2.清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.itcast.interceptcall" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".OutCallReceiver"
            android:enabled="true"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
            </intent-filter>

        </receiver>
    </application>
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
</manifest>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

6.3 自定义广播

6.3.1 自定义广播的发送与接收

img

6.3.2 实战演练——拯救史迪仔

img

img

6.4 广播类型

6.4.1 有序广播和无序广播

Android系统提供了两种广播类型,有序广播和无序广播,开发者可根据需求为程序设置不同的广播类型。

img

img

img

6.4.2 实战演练——拦截史迪仔广播

img

img

6.5 本章小结

​ 本章详细地讲解了广播接收者的相关知识,首先介绍了什么是广播接收者,然后讲解了如何自定义广播以及广播的类型。通过本章的学习,要求初学者能够熟练掌握广播接收者的使用,并在实际开发中进行应用。

【学习笔记】

【学习资料】

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/902070
推荐阅读
相关标签
  

闽ICP备14008679号