美文网首页
不同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之间发送广播

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