当前位置:天才代写 > tutorial > 安卓教程 > android基础—->Broadcast的使用

android基础—->Broadcast的使用

2021-02-15 10:24 星期一 所属: 安卓教程 浏览:24

  在Android中,Broadcast是一种普遍应用的在应用软件中间传送信息内容的体制。而BroadcastReceiver是对推送出去的 Broadcast开展过虑接受并回应的一类部件。

 

文件目录导航栏:

  1.  广播节目Broadcast的简要说明
  2.  广播节目Broadcast的准备工作
  3.  Broadcast广播节目的检测
  4.  友链

 

广播节目Broadcast的简要说明

一、 广播节目的类型:规范广播节目和井然有序广播节目

  • 规范广播节目:是一种彻底多线程实行的广播节目,在广播节目传出以后,全部的广播节目信号接收器基本上都是会在同一時刻接受到这一条广播节目信息,因而他们中间沒有一切顺序可谈。这类广播节目的高效率会较为高,但另外也代表着它是没法被断开的

   

  • 井然有序广播节目:则是一种同歩实行的广播节目,在广播节目传出以后,同一時刻总是有一个广播节目信号接收器可以接到这条广播节目信息,当这一广播节目信号接收器中的逻辑性实行结束后,广播节目才会再次传送

  

二、 申请注册BroadcastReceiver的二种方法:

  • 静态数据申请注册:在AndroidManifest.xml选用标识性命申请注册,并在标识上用标识设定过滤装置
<receiver android:name=".NetworkChangeReceiver">
    <intent-filter>
        <action android:name="com.example.linux.broadcasttest.MyBroadcast" />
    </intent-filter>
</receiver>
  •  动态性申请注册:在java编码中启用registerReceiver(BroadcastReceiver receiver, IntentFilter filter)方式
Intent intent = new Intent();
intent.putExtra("username", "linux");
intent.setAction("com.example.linux.broadcasttest.MyBroadcast");
sendBroadcast(intent);

 

广播节目Broadcast的准备工作

大家创建一个android新项目,构造以下:

 

一、 在MainActivity中复位一些信息内容:

private final static String TAG = "OtherReceiver";
private IntentFilter intentFilter;
private NetworkChangeReceiver networkChangeReceiver;
private LocalBroadcastManager localBroadcastManager;
private LocalReceiver localReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "main on create");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

 

二、 建立三个广播节目信号接收器,用以广播节目的检测:他们各自以下

  • LocalReceiver当地广播节目:
package com.example.linux.broadcasttest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * Created by Linux on 2016/3/7.
 */
public class LocalReceiver extends BroadcastReceiver {
    private final static String TAG = "OtherReceiver";

    public LocalReceiver() {
        Log.i(TAG, "local receiver constructor.");
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String username = intent.getStringExtra("username");
        Log.i(TAG, "local username: "   username);
    }
}
  • OtherReceiver用以检测井然有序广播节目:
package com.example.linux.broadcasttest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class OtherReceiver extends BroadcastReceiver {
    private final static String TAG = "OtherReceiver";
    public OtherReceiver() {
        Log.i(TAG, "other receiver constructor.");
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String username = intent.getStringExtra("username");
        Log.i(TAG, "other username: "   username);
        abortBroadcast();
    }
}
  • NetworkChangeReceiver用以检测系统软件广播节目的应用:
package com.example.linux.broadcasttest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * Created by Linux on 2016/3/7.
 */
public class NetworkChangeReceiver extends BroadcastReceiver {
    private final static String TAG = "OtherReceiver";

    public NetworkChangeReceiver() {
        Log.i(TAG, "network receiver constructor.");
    }

    @Override
    //Intent是sendBroadcast中的主要参数
    public void onReceive(Context context, Intent intent) {
        String username = intent.getStringExtra("username");
        Log.i(TAG, "network username: "   username);
    }
}

 

三、 因为广播节目会牵涉到申请注册,这儿我将广播节目的申请注册编码贴上去:在AndroidManifest.xml文件中

<receiver android:name=".NetworkChangeReceiver">
    <intent-filter>
        <action android:name="com.example.linux.broadcasttest.MyBroadcast" />
    </intent-filter>
</receiver>
<receiver android:name=".OtherReceiver">
    <intent-filter android:priority="100">
        <action android:name="com.example.linux.broadcasttest.MyBroadcast" />
    </intent-filter>
</receiver>
<receiver android:name=".LocalReceiver">
    <intent-filter>
        <action android:name="com.example.linux.broadcasttest.MyBroadcast" />
    </intent-filter>
</receiver>

 

Broadcast广播节目的检测

一、 定义方法sendSystemBroad,用以推送系统软件广播节目

// 推送系统软件广播节目
public void sendSystemBroad(View view) {
    intentFilter = new IntentFilter();
    intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
    networkChangeReceiver = new NetworkChangeReceiver();
    registerReceiver(networkChangeReceiver, intentFilter);
}
  • 复印的結果:
03-23 23:03:20.087 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: other receiver constructor.
03-23 23:03:20.087 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: other username: linux
03-23 23:03:20.097 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: network receiver constructor.
03-23 23:03:20.107 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: network username: linux
03-23 23:03:20.107 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: local receiver constructor.
03-23 23:03:20.107 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: local username: linux

 

二、 定义方法sendBroadCast,用以检测推送规范广播节目

// 推送规范广播节目
public void sendBroadCast(View view) {
    Intent intent = new Intent();
    intent.putExtra("username", "linux");
    intent.setAction("com.example.linux.broadcasttest.MyBroadcast");
    sendBroadcast(intent);
}
  • 复印的結果:
03-23 23:05:47.957 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: network receiver constructor.
03-23 23:05:47.967 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: network username: null

 

三、 定义方法sendOrderBroad,用以检测井然有序广播节目

// 推送井然有序广播节目
public void sendOrderBroad(View view) {
    Intent intent = new Intent();
    intent.putExtra("username", "linux");
    intent.setAction("com.example.linux.broadcasttest.MyBroadcast");
    sendOrderedBroadcast(intent, null);
}
  • 复印的結果:
03-23 23:07:28.877 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: other receiver constructor.
03-23 23:07:28.877 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: other username: linux

 

四、 定义方法sendLocalBroad,用以检测当地广播节目

// 应用当地广播节目
public void sendLocalBroad(View view) {
    localBroadcastManager = LocalBroadcastManager.getInstance(this);
    Intent intent = new Intent();
    intent.putExtra("username", "linux");
    intent.setAction("com.example.linux.broadcasttest.MyBroadcast");
    localBroadcastManager.sendBroadcast(intent);
}
  • 复印的結果:
03-23 23:28:09.327 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: local username: linux
03-23 23:28:09.327 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: local username: linux
03-23 23:28:09.327 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: local username: linux

 

友链

  • 广播节目的检测源码      浏览登陆密码 1e16
 

    关键字:

天才代写-代写联系方式