跨进程通信的广播BroadcastReceiver

一发送端

1.自定义权限

    
    

    

2.Activity中动态注册广播

package com.ytlz.myjavatest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;

import java.util.zip.Inflater;

public class MainActivity extends AppCompatActivity {
    private ProgressReceiver progressReceiver;
    private final static String PROGRESS_MESSAGE_TEST1 = "progress_message_test1";
    private Button sendBrocast;
    private String premission = "com.ytlz.myjavatest.brocastreceiver.progress";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sendBrocast = findViewById(R.id.sendBrocast);
        sendBrocast.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setAction(PROGRESS_MESSAGE_TEST1);
            sendBroadcast(intent, premission);
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        initReceiver();
    }

    void initReceiver() {
        progressReceiver = new ProgressReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(PROGRESS_MESSAGE_TEST1);
        registerReceiver(progressReceiver, intentFilter, premission, null);

    }

    @Override
    protected void onPause() {
        super.onPause();
        recycleReceiver();
    }

    void recycleReceiver() {
        if (progressReceiver != null) {
            unregisterReceiver(progressReceiver);
        }
    }

    private class ProgressReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (PROGRESS_MESSAGE_TEST1.equalsIgnoreCase(action))
                Toast.makeText(MainActivity.this, "我接收到广播了", Toast.LENGTH_SHORT).show();
        }
    }
}

二,接收端代码

1.动态广播测试

清单文件设置权限代码:

 

Activity代码:

package com.ytlz.receivetest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    TestReceiver testReceiver;
    private final static String PROGRESS_MESSAGE_TEST1 = "progress_message_test1";
    private String premission = "com.ytlz.myjavatest.brocastreceiver.progress";
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        testReceiver = new TestReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(PROGRESS_MESSAGE_TEST1);
        registerReceiver(testReceiver, filter, premission, null);

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (testReceiver != null) {
            unregisterReceiver(testReceiver);
        }

    }

    private class TestReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (PROGRESS_MESSAGE_TEST1.equalsIgnoreCase(action)) {
                Log.d(TAG, "onReceive: -------------------------我也接收到了-------------------------");
            }
        }
    }
}

然后点击第一个app的发送按钮,第二个app就能接收到这个广播

2.静态广播测试

清单文件代码

//权限必不可少
 
 
        
            
                
            
        

广播文件

public class MyReceiver extends BroadcastReceiver {

    private final static String PROGRESS_MESSAGE_TEST1 = "progress_message_test1";

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        if (intent.getAction().equalsIgnoreCase(PROGRESS_MESSAGE_TEST1)) {
            Log.d("MyReceiver", "我接手的的是廣播嗎");
        }
    }
}

点击发送广播按钮,测试静态广播的接收

你可能感兴趣的:(Android,学习笔记)