• intent广播


    1.创建一个intent,调用sendBroadcast()函数,把intent携带的信息广播出去,如果要在intent传递额外数据,可以用intent的putExtra()方法

     1 import android.app.Activity;
     2 import android.content.Intent;
     3 import android.os.Bundle;
     4 import android.view.View;
     5 import android.widget.Button;
     6 
     7 public class FirstActivity extends Activity {
     8 
     9     @Override
    10     protected void onCreate(Bundle savedInstanceState) {
    11         super.onCreate(savedInstanceState);
    12         setContentView(R.layout.firstactivity_layout);// 设置页面布局
    13         Button button = (Button) findViewById(R.id.button);// 通过ID值获得按钮对象
    14         button.setOnClickListener(new View.OnClickListener() {// 为按钮增加单击事件监听器
    15             public void onClick(View v) {
    16                 String str = "this.is.the.first.broadcast";//将此字符串作为识别广播的标识符
    17                 Intent intent = new Intent(str);// 创建Intent对象
    18                 intent.putExtra("message","this is my first message with broadcast");
    19                 sendBroadcast(intent);
    20             }
    21         });
    22     }
    23 }

    2.广播消息发送后,利用broadcastReceiver监听广播消息,并且在AndroidManifest.xml文件中注册receiver标签。在broadcastReceiver接收到与之匹配的广播消息后,onReceive()方法会被调用;

     1 import android.app.Activity;
     2 import android.content.BroadcastReceiver;
     3 import android.content.Context;
     4 import android.content.Intent;
     5 import android.os.Bundle;
     6 import android.widget.Toast;
     7 
     8 public class SecondActivity extends BroadcastReceiver {//接收广播要继承BroadcastReceiver
     9 
    10     @Override
    11     public void onReceive(Context context, Intent intent) {//重写onReceive方法
    12         // TODO Auto-generated method stub
    13         String msg = intent.getStringExtra("message");//获取key为message里面的字符串的值
    14         Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    15     }
    16 }

    3.在AndroidManifest.xml中进行广播过滤

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     3     package="com.mingrisoft"
     4     android:versionCode="1"
     5     android:versionName="1.0" >
     6 
     7     <uses-sdk android:minSdkVersion="15" />
     8 
     9     <application
    10         android:icon="@drawable/ic_launcher"
    11         android:label="@string/app_name" >
    12         <activity android:name=".FirstActivity" > <!--发送广播方的类名-->
    13             <intent-filter >
    14                 <action android:name="android.intent.action.MAIN" />
    15                 <category android:name="android.intent.category.LAUNCHER" />
    16             </intent-filter>
    17         </activity> 
    18              
    19             <receiver android:name=".SecondActivity"><!--接收广播方的类名 -->
    20                 <intent-filter>
    21                     <action android:name="this.is.the.first.broadcast"/><!--只接受带该字符串的intent广播-->        
    22                             <!--这与发送方(最上面的第一段代码)在初始化intent时作为参数的字符串是一致的-->
    23                 </intent-filter>
    24              </receiver>
    25              
    26      </application>
    27 </manifest>

    通过在AndroidManifest.xml中,第21行代码,将第一段代码的发送方和第二段接收方的代码进行连接起来了,它就像一座桥梁。

  • 相关阅读:
    python flsak 框架
    postman
    压力测试和负载测试
    软件测试相关内容
    Linux常用基本命令
    leetcode刷题——反转字符串
    leetcode——搜索插入位置
    leetcode刷题——移除元素
    leetcode 刷题 ——删除排序数组中的重复项
    json.decoder.JSONDecodeError: Expecting value 错误的处理
  • 原文地址:https://www.cnblogs.com/hxjbc/p/5178471.html
Copyright © 2020-2023  润新知