android_无序广播

新建三个Module
分别是send发送,receiver01接受1,receiver02接受2,
一个发送多个接受

send发送Module

activitymain.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
 android:orientation="vertical" android:layout_height="match_parent" tools:context="com.example.sendr.MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/et_main_content" android:hint="请输入要发送的内容"
 /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="发送" android:onClick="send" /></LinearLayout>
MainActivity.java
package com.example.sendr;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private EditText et_main_content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_main_content = (EditText) findViewById(R.id.et_main_content);
    }
    public void send(View view){
        String content=et_main_content.getText().toString();

        //发送广播
        //两种类型广播
        //有序广播  无序广播
        Intent intent=new Intent();
        //指定广播的名字
        intent.setAction("com.example.sendr.Hug");
        //指定广播内容
        intent.putExtra("content",content);
        //发送无序广播
        //sendBroadcast(intent);

        //发送无序黏性广播
        sendStickyBroadcast(intent);
    }
}
AdnroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sendr">
    <!--黏性广播权限-->
    <uses-permission android:name="android.permission.BROADCAST_STICKY"></uses-permission>
    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round"
        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>
    </application>

</manifest>



receiver01接受01Module
MainActivity.java
package com.example.receiver01;

import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    private MyReceiver01 myReceiver01;
    private IntentFilter intentFilter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myReceiver01 = new MyReceiver01();
        intentFilter = new IntentFilter();
        intentFilter.addAction("com.example.sendr.Hug");
    }
    @Override
    protected void onResume() {
        super.onResume();
        //注册广播
        registerReceiver(myReceiver01,intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //取消注册  注销
        unregisterReceiver(myReceiver01);
    }
}
MyReceiver01.java
package com.example.receiver01;

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

/**
 * Created by Administrator on 2017/7/11 0011.
 */

public class MyReceiver01 extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
       //获取广播的名字
        String action=intent.getAction();
        if("com.example.sendr.Hug".equals(action)){
            //获取广播内容
            String content=intent.getStringExtra("content");
            Log.i("test","广播接受者1号:"+content);
        }
    }
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.receiver01">

    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round"
        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=".MyReceiver01">
            <intent-filter>
                <action android:name="com.example.sendr.Hug"></action>
            </intent-filter>

        </receiver>
    </application>

</manifest>
receiver02接受02Module
MyReceiver02.java
package com.example.receiver02;

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

/**
 * Created by Administrator on 2017/7/11 0011.
 */

public class MyReceiver02 extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
       //获取广播的名字
        String action=intent.getAction();
        if("com.example.sendr.Hug".equals(action)){
            //获取广播内容
            String content=intent.getStringExtra("content");
            Log.i("test","广播接受者2号:"+content);
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.receiver02">

    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round"
        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>a
        </activity>
        <receiver android:name=".MyReceiver02">
            <intent-filter>
                <action android:name="com.example.sendr.Hug"></action>
            </intent-filter>
        </receiver>
    </application>

</manifest>

} }}






















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值