出门莫恨无人随,书中车马多如簇。这篇文章主要讲述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>
文章图片
文章图片
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接收器
文章图片
文章图片
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);
推荐阅读
- android 学习随笔八(网络)
- Java4Android基础学习之包与访问权限
- Android Studio第十二期 - Activity+Fragment模块化管理方式
- Skipping 'Android SDK Tools, revision 24.0.2'; it depends on 'Android SDK Platform-tools
- Android解决软键盘弹出将布局顶到上面
- Android -- 案例学习积累
- 安装android studio
- 手机钉钉怎样退出企业?_其它办公
- excel如何保护公式?excel保护公式设置办法_Excel专区