美文网首页
不同apk之间发送广播

不同apk之间发送广播

作者: 我叫杨毅 | 来源:发表于2019-03-26 10:38 被阅读0次

A.apk

1.在MainActivity

private  Button  but;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        but=findViewById(R.id.bu_t);

        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击的时候发送一条广播出去
                Intent intent = new Intent("com.example.test2");//括号里为包名
                intent.putExtra("123", "我被点了");
                intent.setComponent(new ComponentName("com.rfid.RFID","com.rfid.RFID.MyReceiver"));//括号里为包名+广播类
                sendBroadcast(intent);
                Log.e(TAG, "onClick: 111111");

            }
        });

    }

2.XML

<Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="发送广播"
        android:id="@+id/bu_t"
        android:layout_centerInParent="true"
        />

B.apk

3.mainfest中隐世广播

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rfid.RFID">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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>

<!--隐世广播-->
        <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.rfid.RFID"/>
            </intent-filter>
        </receiver>

    </application>

</manifest>

4.新建MyReceiver类继承BroadcastReceiver 广播

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"接收到广播",Toast.LENGTH_SHORT).show();
        String str = intent.getStringExtra("123");
        Toast.makeText(context,str,Toast.LENGTH_SHORT).show();
    }
}

如果是定义内部类在MainActivity,必须要静态static ,否则会崩,调不到textview只需要加上静态static

private static TextView tex;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tex = (TextView) findViewById(R.id.tex);
    }



    public static class MyReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context,"接收到广播",Toast.LENGTH_SHORT).show();
            String str = intent.getStringExtra("123");
            Toast.makeText(context,str,Toast.LENGTH_SHORT).show();
            tex.setText(str);
        }
    }

相关文章

  • 不同apk之间发送广播

    A.apk 1.在MainActivity 2.XML B.apk 3.mainfest中隐世广播 4.新建MyR...

  • android 9.0通过广播实现静默安装

    一、实现思路第三方APP通过发送特定广播,把apk文件路径发送给系统,由android系统来安装apk二、实现步奏...

  • BroadCast

    网络状态监听 开机广播监听 发送广播 发送标准广播

  • 发送广播

    发送广播只是需要发送自己定义的广播,那种类似于系统广播等已有的广播是不需要自己手动的发送的。无论广播注册的形式是什...

  • 《第一行代码:Android》读书笔记——第5章 Broadca

    广播机制简介 Android广播的分类: 如图所示: 发送和接收广播 发送广播:使用Intent 接收广播:Bro...

  • BroadcastReceiver基础知识

    什么是广播 在Android中,广播是一种广泛运用的在应用程序之间传输信息的机制,Android中我们要发送的广播...

  • android的广播

    Android的广播方式分为有序广播和标准广播。 发送广播 在activity中发送标准广播,调用的方法是send...

  • Android中广播的基本使用及说明

    1、Android广播发送及广播类型 广播发送的基本代码: Intent intent = new Intent(...

  • Broadcast 学习

    广播类别:标准广播,有序广播,本地广播 标准广播异步发送,所有人能够接收只要注册接收器就能监听 有序广播顺序发送,...

  • swift 广播

    步骤注册广播 注册解析广播的方法 发送广播

网友评论

      本文标题:不同apk之间发送广播

      本文链接:https://www.haomeiwen.com/subject/ndnwvqtx.html