博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android入门——BroadCast(2)
阅读量:4698 次
发布时间:2019-06-09

本文共 2556 字,大约阅读时间需要 8 分钟。

自定义广播

AndroidMainfest.xml

注意声明自定义权力 

开启权力

静态注册

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);

 

转载于:https://www.cnblogs.com/wangkaipeng/p/5894741.html

你可能感兴趣的文章
PeekMessage、GetMessage的区别
查看>>
磁盘使用率达到100%
查看>>
linux跳过root密码登陆
查看>>
mini2440 U-boot 编译
查看>>
学习ThreadLocal
查看>>
在 Visual Studio 调试器中指定符号 (.pdb) 和源文件
查看>>
直接量
查看>>
leetcode 115. 不同的子序列(Distinct Subsequences)
查看>>
三元表达式
查看>>
Oauth支持的5类 grant_type 及说明
查看>>
客户端第一天学习的相关知识
查看>>
LeetCode - Same Tree
查看>>
Python dict get items pop update
查看>>
[置顶] 程序员必知(二):位图(bitmap)
查看>>
130242014036-(2)-体验敏捷开发
查看>>
constexpr
查看>>
Nginx 流量和连接数限制
查看>>
课堂作业1
查看>>
IE8/9 本地预览上传图片
查看>>
Summary of CRM 2011 plug-in
查看>>