android_无序广播
来源:互联网 发布:亚马逊云计算培训 编辑:程序博客网 时间:2024/05/19 12:23
新建三个Module分别是send发送,receiver01接受1,receiver02接受2,一个发送多个接受send发送Moduleactivitymain.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.javapackage 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接受01ModuleMainActivity.javapackage 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.javapackage 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.javapackage 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
- android_无序广播
- 无序广播
- 无序广播
- Android_广播
- android_广播
- 广播的无序广播
- 有序广播、无序广播
- 有序广播和无序广播
- 顺序广播和无序广播
- 有序广播与无序广播
- 有序广播和无序广播
- 有序广播和无序广播
- 有序广播和无序广播
- 有序广播和无序广播
- 有序广播和无序广播
- 有序广播和无序广播
- 有序广播和无序广播
- 有序广播与无序广播
- 软件测试学习笔记(2):软件测试的分类
- 【机器学习读书笔记】决策树
- simple_graph_execution_state
- jQuery基本选择器
- 如何排除jar包冲突
- android_无序广播
- 软件缺陷度量
- 慕课的Spring课程
- IDEA如何解决WARN No appenders could be found for logger
- ubuntu opencv3.2安装测试
- 1023. Have Fun with Numbers (20)
- 【机器学习读书笔记】朴素贝叶斯分类
- mysqld 启动报错:Address already in use
- 简单的sql查询