zoukankan      html  css  js  c++  java
  • Android连载22-自定义广播之标准广播发送

    一、发送自定义广播

    1.广播主要分为两种:

    • 标准广播和有序广播

    2.发送标准广播

    • 先定义一个广播接收器来接收广播
    package com.example.broadcasttest2;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.widget.Toast;
    
    public class MyBroadcastReceiver extends BroadcastReceiver {
     
     @Override
     public void onReceive(Context context,Intent intent) {
      Toast.makeText(context, "received in MyBroadcastReceiver", Toast.LENGTH_SHORT).show();
     }
     
    }
     
    • 上面的代码意义在于接收到自定义广播的时候,就会弹出"received in MyBroadcastReceiver"这句话,然后在AndroidManifest.xml中注册这个类
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
    
          ..........................................
            
            <receiver android:name=".MyBroadcastReceiver">
                <intent-filter>
                    <action android:name="com.example.broadcasttest.MY_BROADCAST"/>
                </intent-filter>
            </receiver>
        </application>
    • 上面的XML就是当定义的接收器接收到值com.example.broadcasttest.MY_BROADCAST的时候才会触发那个定义好的广播器
    • 接下来我们可以猜到就是触发这条广播呗,我们用个按钮来触发这个广播吧
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     ..........................................
            android:id="@+id/button"
            android:layout_width = "match_parent"
            android:layout_height = "wrap_content"
            android:text = "Send Broadcast"
            />
    </LinearLayout>
    package com.example.broadcasttest2;
    
    import android.app.Activity;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
     
     private IntentFilter intentFilter;
     
     private NetworkChangeReceiver networkChangeReceiver;
     
     @Override
     protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
        
      Button button = (Button) findViewById(R.id.button);
      button.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) {
        Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
        sendBroadcast(intent);
       }
      });
     } 
    }

    运行打包安装:
    22.1

    • 我们点击一下按钮
      22.2
    • 首先构建出了一个Intent对象,并把要发送的广播的值传入,然后调用了Context的sendBroadcast()方法将广播发送出去,这样所有监听com.example.broadcasttest.MY_BROADDCAST这条广播的广播接收器就会收到消息。此时发出去的广播就是一条标准广播。

    二、源码:

  • 相关阅读:
    【BZOJ4826】【HNOI2017】影魔(扫描线,单调栈)
    【BZOJ4540】【HNOI2016】序列(莫队)
    【NOIP2017】列队(Splay)
    ZJOI2018酱油记
    【BZOJ4828】【HNOI2017】大佬(动态规划)
    【NOIP2017】宝藏(状态压缩,动态规划)
    【HDU4336】Card Collector (动态规划,数学期望)
    【HDU4652】Dice(数学期望,动态规划)
    【BZOJ4945】【NOI2017】游戏(搜索,2-sat)
    【BZOJ3714】Kuglarz(最小生成树)
  • 原文地址:https://www.cnblogs.com/ruigege0000/p/13228003.html
Copyright © 2011-2022 走看看