android入门——BroadCast

出门莫恨无人随,书中车马多如簇。这篇文章主要讲述android入门——BroadCast相关的知识,希望能为你提供帮助。
自定义广播

android入门——BroadCast

文章图片
android入门——BroadCast

文章图片
< ?xml version="1.0" encoding="utf-8"?> < manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.wkp.broadcast"> < !-- 声明自定义权力 --> < permission android:name="com.example.wkp.broadcast.MY_PEMISSION"/> < application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" 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> < activity android:name=".SecondActivity"> < intent-filter> < action android:name="android.intent.action.MAIN" /> < category android:name="android.intent.category.LAUNCHER" /> < /intent-filter> < /activity> < receiver android:name=".MyReceiver2"> < intent-filter> < action android:name="com.example.wkp.broadcast.MY_ACTION"/> < /intent-filter> < /receiver> < !--静态注册 --> < receiver android:name=".MyReceiver"> < intent-filter> < !-- 动作打开飞行模式时触发广播 --> < !--< action android:name="android.intent.action.AIRPLANE_MODE"/> --> < !-- 收到短信时暂时无法实现 --> < !--< action android:name="android.provider.Telephony.SMS_RECEIVED"/> --> < !--< action android:name="android.provider.Telephony.SMS_DELIVER"/> --> < /intent-filter> < /receiver> < /application> < !-- 开启短信权限 --> < uses-permission android:name="android.permission.SEND_SMS"> < /uses-permission> < uses-permission android:name="android.permission.RECEIVE_SMS"> < /uses-permission> < uses-permission android:name="android.permission.READ_SMS"> < /uses-permission> < !-- 自定义权力 --> < uses-permission android:name="com.example.wkp.broadcast.MY_PEMISSION"> < /uses-permission> < /manifest>

AndroidMainfest.xml【android入门——BroadCast】注意声明自定义权力 
< permission android:name="com.example.wkp.broadcast.MY_PEMISSION"/>

开启权力
< uses-permission android:name="com.example.wkp.broadcast.MY_PEMISSION"> < /uses-permission>

静态注册
< receiver android:name=".MyReceiver2">
< intent-filter>
< action android:name="com.example.wkp.broadcast.MY_ACTION"/>
< /intent-filter>
< /receiver>

android入门——BroadCast

文章图片
android入门——BroadCast

文章图片
package com.example.wkp.broadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; /** * Created by wkp on 2016/9/21. */ public class MyReceiver2 extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Log.v("hh","get broadcast"); } }

MyReceiver2.java接收器
android入门——BroadCast

文章图片
android入门——BroadCast

文章图片
package com.example.wkp.broadcast; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; /** * Created by wkp on 2016/9/21. */ public class SecondActivity extends Activity { private Button btn=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); btn= (Button) findViewById(R.id.send); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(); intent.setAction("com.example.wkp.broadcast.MY_ACTION"); sendBroadcast(intent,"com.example.wkp.broadcast.MY_PEMISSION"); Log.v("hehe","already send"); } }); } }

SecondActivity.java点击按钮发送广播
 
 
有序广播先定义先接收
abortBroadcast(); 忽略广播  
可以通过android:priority制定优先级   不写为0
可以设置是否接收其他app的广播
可以设置是否被其他app接收广播 本地广播LocalBroadcastManager
private LocalBroadcastManager manager=  LocalBroadcastManager.getInstance(this);
LocalReceiver receiver=new LocalReceiver();
IntentFilter filter=new IntentFilter();
filter.addAction("com.example.wkp.broadcast.MY_ACTION");
manager.registerReceiver(receiver,filter);
 

    推荐阅读