Android--广播

xiaoxiao2021-02-28  109

1. 安卓提供了一整套API,允许应用程序自由地发送和接受广播

2. 发送广播:Intent 接收广播:BroadcastReceiver

3. 标准广播:异步广播,同时接收 有序广播:同步广播,先后接收,可截获

4. 注册广播:动态注册(在代码中),静态注册(在AndroidManifest.xml中)

5. 创建一个广播接收器,新建一个类,继承自BroadcastReceiver,并重写onReceive()

6. 动态注册: 作用:监听网络变化

package com.example.zzz.broadcast; 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.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private IntentFilter intentFilter; private NetworkChangeReceiver networkChangeReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); intentFilter=new IntentFilter(); intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); networkChangeReceiver=new NetworkChangeReceiver(); registerReceiver(networkChangeReceiver,intentFilter); } protected void onDestroy(){ super.onDestroy(); unregisterReceiver(networkChangeReceiver); } class NetworkChangeReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isAvailable()) Toast.makeText(context, "network is available", Toast.LENGTH_SHORT).show(); else Toast.makeText(context, "network is unavailable", Toast.LENGTH_SHORT).show(); } } }

每当网络变化时,onReceive()就会执行 intentFilter需要添加一个实例(android.net.conn.CONNECTIVITY_CHANGE),我们广播接收器想要接受怎样的广播,直接addAction就可以了,然后将intentFilter和networkChangeReceiver注册。 记得一定要在onDestroy()取消注册

然后要在AndroidManifest.xml中加入

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

7.静态注册 新建一个类

public class BootCompleteReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context,"开机成功",Toast.LENGTH_LONG).show(); } }

然后在AndroidManifest.xml中加入

<application android:theme="@style/AppTheme"> <receiver android:name=".BootCompleteReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver> </application>

不要再onReceive()中加入过多逻辑,因为广播不允许开启线程

8. 发送标准广播 我们通过一个按钮来发送广播,广播接收器接收到广播后执行具体操作。

首先建立布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send Broadcast" /> </LinearLayout>  

然后创建一个自己的广播

public class MainActivity extends ActionBarActivity { @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() { public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent( "com.example.broadcasttest.MY_BROADCAST"); sendBroadcast(intent); } }); }

再新建一个广播接收器来接收我们发送的广播

public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "received", Toast.LENGTH_SHORT).show(); } }

最后注册广播接收器

<receiver android:name=".MyBroadcastReceiver" > <intent-filter> <action android:name="com.example.mybroadcast. MY_BROADCAST" /> </intent-filter> </receiver>

9. 发送有序广播 有序广播和标准广播基本上一样,只需要修改几个地方。

将发送广播的方式改为sendOrderedBroadcast(intent, null);设置广播接收器的权限intent-filter android:priority=”100”广播优先级高的权限可以终止广播abortBroadcast();

 

10. 本地广播机制 为了能够简单地解决广播的安全性问题, Android 引入了一套本地广播机制,使用这个机制发出的广播只能够在应用程序的内部进行传递,并且广播接收器也只能接收来自本应用程序发出的广播。

另外还有一点需要说明,本地广播是无法通过静态注册的方式来接收的。

转载请注明原文地址: https://www.6miu.com/read-68351.html

最新回复(0)