android自定义广播_无序广播

概述

无序广播:类似新闻联播:广播不可以被终止 数据不可以被修改 

有序广播:类似中央发送的红头文件 按照优先级一级一级的接收 有序广播可以被终止 数据可以被修改

自定义无序广播发送和接收实例

一、项目目录结构


二、发送activity_main.xml代码

<RelativeLayout xmlns: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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.zgs.Send.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    
    <Button
        android:onClick="click"
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="77dp"
        android:text="发送无序广播" />

</RelativeLayout>
三、发送MainActivity.java代码
package com.zgs.Send;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	//点击按钮发送一条无序广播
	public void click(View v) {
		Intent intent = new Intent();
		//设置action 
		intent.setAction("com.zgs.custombroadcast");
		intent.putExtra("name", "无序广播开始发送了,注意接收哦!!!");
		//发送一条广播 发送无序广播
		sendBroadcast(intent);

	}
}
四、接收ReceiveCustomReceiver.java代码
package com.zgs.Receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class ReceiveCustomReceiver extends BroadcastReceiver {
	//当我发送自定义广播的时候 这个方法就会接收到 
	@Override
	public void onReceive(Context context, Intent intent) {
		//[0]终止广播
		//			abortBroadcast();
		//[1]取出我们发送广播携带的数据 
		String content = intent.getStringExtra("name");
		//[2]把获取到的数据展示到toast上
		Toast.makeText(context, content, 0).show();
	}

}
五、AndroidManifest.xml代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.zgs.Receiver"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="22" />

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="com.zgs.Receiver.ReceiveCustomReceiver">
            <intent-filter >
                <action android:name="com.zgs.custombroadcast"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>
六、操作演示

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值