android studio 自定义广播

这次记录的主要是我对广播的一些理解

自定义广播的静态注册与使用

一.静态广播的注册
到AndroidMainfest.xml文件当中进行注册(注意必须得要将receiver写在application的里面)
Android:name=“继承broadcastReceiver的类的名称”
intent-filter是一个过滤器(用来筛选出你所需要找到的广播)
action android:name则是自定义广播的名称
第一步对在androidmainfest.xml里广播进行注册
第二步在mainactivity里通过sendBroadCast发送广播
第三步骤建立一个继承BroadCastReceiver的类来接收该广播
AndroidMainfest.xml

        <receiver android:name=".MyGuangBo">
            <intent-filter>
                <action android:name="nanchang" />
            </intent-filter>
        </receiver>

MainActivity.java
注意这里是开始写发送广播的程序(定义了广播名称,定义了通过广播发送的内容)
值得注意的是android8.0以上的版本静态注册会出现无法使用的情况,所以我这有两种解决办法;
第一种:

intent.setComponent(new ComponentName(getPackageName(),"广播的类路径名"));

第二种:

intent.setComponent(new ComponentName("广播的类的报名","广播的类路径名"));

以上两种都需添加在intent声明之后

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = findViewById(R.id.bt1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("nanchang");
                intent.putExtra("西瓜","4.5");4.5通过西瓜来传送数据给广播
                intent.setComponent(new ComponentName(getPackageName(),"com.example.zidingyiguangbo.MyGuangBo"));
                //Toast.makeText(MainActivity.this,"aaaa",Toast.LENGTH_LONG).show();
                sendBroadcast(intent);
            }
        });
    }
}

MainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
		这里开始设置按钮 id对应 上面的获取按钮的id
    <Button
        android:id="@+id/bt1"
        
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送广播"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

写接受广播的程序
注意必须得要继承BroadcastReceiver并且要重写onReceive的方法
onReceive:就是写接收广播的内容并影响界面

public class MyGuangBo extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String STR=intent.getStringExtra("西瓜");
        接收
        Log.v("接收到了","123");
        log.v来测试广播是否有接收到
        Toast.makeText(context,STR,Toast.LENGTH_SHORT).show();
        通过提示的方法来影响界面
    }
}

自定义广播的动态注册与使用

动态注册不需要在AndroidMainfest.xml文件里面进行注册,而是需要在MainActivity.java里
同样的注册需要在onCreat方法里面实现
实现功能:点击按钮能够出现提示
第一步对在mainactivity里广播进行注册
第二步在mainactivity里通过sendBroadCast发送广播
第三步建立一个继承BroadCastReceiver的类来接收该广播

private DongTaiZhuCe guangbo;
		guangbo=new DongTaiZhuCe();
		直接将guangbo对象升级为成员变量 
		
        IntentFilter intentFilter=new IntentFilter("nanchang");
        registerReceiver(guangbo,intentFilter);

将guangbo对象升级为成员变量 的目的是为了让onDestory方法实现

public class MainActivity extends AppCompatActivity implements DongTaiZhuCe.DaiLi {
        private DongTaiZhuCe guangbo;
        private TextView tv1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         guangbo=new DongTaiZhuCe();
         设置广播对象
        IntentFilter intentFilter=new IntentFilter("nanchang");
        过滤器
        registerReceiver(guangbo,intentFilter);
        		注册		广播    过滤器
        /*第四部 将MainAcitity对象传到广播中 注册之后 this表示 mainactivity对象*/
        guangbo.chuansongzhizhen(this);
         tv1=findViewById(R.id.tv1);
        Button button1=findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent("nanchang");
                intent.putExtra("xigua","6.6");

                sendBroadcast(intent);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(guangbo);
    }
    /*第六部 写写入的方法*/
    @Override
    public void xieru(String s) {
        tv1.setText(s);
    }
}

MainActivity.xml文件
第一步对在mainactivity里广播进行注册
第二步在mainactivity里通过sendBroadCast发送广播
第三步建立一个继承BroadCastReceiver的类来接收该广播

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

广播的接收类

public class DongTaiZhuCe extends BroadcastReceiver {
   /*第三部 为daili写上成员数据 prvivate镜像包私用*/
    private DaiLi daili;

    @Override
    public void onReceive(Context context, Intent intent) {
        /*写入界面tv之中*/
        String str=intent.getStringExtra("xigua");
       /* 第五步 写写入的方法*/
        daili.xieru(str);
        Toast.makeText(context,str,Toast.LENGTH_LONG).show();
    }
//this 传给d 给了main activity
    public void chuansongzhizhen(DaiLi d) {
        daili=d;
    }

    /*在广播设定接口*/
    interface DaiLi {
        void xieru(String s);
    }
}

自定义广播的跨应用广播

接下来要实现的是跨应用广播 步骤是在一个工程上建一个发送广播另一个工程负责接收该广播只需要新建一个发送广播的项目就能完成 。
原理:在第一个项目里注册该广播,在第二个项目里面进行调用。
发送端:
MainActivity.java文件

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button=findViewById(R.id.bt1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent("nanchang");
                intent.putExtra("xigua","7.5");

                sendBroadcast(intent);
            }
        });
    }
}

MainActivity.xml文件

    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

在注册项目的时候我遇到了一些问题在这里做出总结:
之前出现apk覆盖的情况导致跨程序运行的失败
android不想覆盖原来apk的方法
因为程序覆盖安装主要检查两点:
1)两个程序的入口Activity是否相同。两个程序如果包名不一样,即使其它所有代码完全一样,也不会被视为同一个程序的不同版本;
2)两个程序所采用的签名是否相同。如果两个程序所采用的签名不同,即使包名相同,也不会被视为同一个程序的不同版本,不能覆盖安装。
android不想覆盖原来apk的方法
{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=1119637389&plateform=mobileqq&url=https%253A%252F%252Fblog.csdn.net%252Fchenhao0568%252Farticle%252Fdetails%252F39429909&src_uin=1119637389&src_scene=311&cli_scene=getDetail,text:网页链接}
1.更改包名 :在项目上右击,选择Android Tools------>Rename Application Package------>改包名,一路回车。
Android studio 如何修改工程的包名
{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=1119637389&plateform=mobileqq&url=http%253A%252F%252Furl.cn%252F5PeGoMR&src_uin=1119637389&src_scene=311&cli_scene=getDetail,text:网页链接}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐