服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android开发之BroadcastReceiver用法实例分析

Android开发之BroadcastReceiver用法实例分析

2021-03-23 15:43imzoer Android

这篇文章主要介绍了Android开发之BroadcastReceiver用法,实例分析了Android中广播的相关使用技巧,需要的朋友可以参考下

本文实例讲述了Android开发中BroadcastReceiver用法。分享给大家供大家参考。具体分析如下:

在Android系统中,广播(Broadcast)是在组件之间传播数据(Intent)的一种机制。

Braodcast Receiver顾名思义就是广播接收器,它和事件处理机制类似,但是事件处理机制是程序组件级别的(比如:按钮的单击事件),而广播事件处理机制是系统级别的。我们可以用Intent来启动一个组件,也可以用sendBroadcast()方法发起一个系统级别的事件广播来传递消息。我们同样可以在自己的应用程序中实现Broadcast Receiver来监听和响应广播的Intent。

事件的广播通过创建Intent对象并调用sendBroadcast()方法将广播发出。事件的接受是通过定义一个继承BroadcastReceiver的类来实现的,继承该类后覆盖其onReceive()方法,在该方法中响应事件。

下面是android系统中定义了很多标准的Broadcast Action来响应系统的广播事件。

①ACTION_TIME_CHANGED(时间改变时触发)
②ACTION_BOOT_COMPLETED(系统启动完成后触发)--比如有些程序开机后启动就是用这种方式来实现的
③ACTION_PACKAGE_ADDED(添加包时触发)
④ACTION_BATTERY_CHANGED(电量低时触发)

下面看一个例子:

我们在一个按钮上绑定一个事件,事件通过发送一个广播来触发logcat打出一个log。

先看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
31
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.test"
  android:versionCode="1"
  android:versionName="1.0" >
  <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />
  <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
      android:name="com.example.broadcast.BroadcastReceiverActivity"
      android:label="@string/app_name_bc" >
      <intent-filter>
        <action android:name="android.intent.action.MAIN" >
        </action>
        <category android:name="android.intent.category.LAUNCHER" >
        </category>
      </intent-filter>
    </activity>
    <receiver android:name="com.example.broadcast.HelloBroadReciever" >
      <intent-filter>
        <action android:name="comz.test.printlog" >
        </action>
      </intent-filter>
    </receiver>
  </application>
</manifest>

上面声明了一个receiver。接收名字是comz.test.printlog的消息。

看activity:

?
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
package com.example.broadcast;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.example.test.R;
public class BroadcastReceiverActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b1 = (Button) findViewById(R.id.broadcastBtn);
    b1.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Log.e("mason", "here");
        // 定义一个intent
        Intent intent = new Intent().setAction("comz.test.printlog")
            .putExtra("info", "here is your info.");
        // 广播出去
        sendBroadcast(intent);
      }
    });
  }
}

在这段代码中,定义一个intent并发送广播出去。

看BroadReceiver的代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.example.broadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class HelloBroadReciever extends BroadcastReceiver {
  // 如果接收的事件发生
  @Override
  public void onReceive(Context context, Intent intent) {
    Log.e("mason", "on receive");
    if (intent.getAction().equals("comz.test.printlog")) {
      Log.e("mason", intent.getStringExtra("info"));
    }
  }
}

这是BroadcastReceiver的代码。

在接收到消息之后,如果消息是comz.test.printlog,则打印消息。

希望本文所述对大家的Android程序设计有所帮助。

延伸 · 阅读

精彩推荐