Android:发送广播

相关知识点:

安卓四大组件:activity 广播 服务Service 内容提供者 ——都需要注册

创建广播

1.创建一个类 继承BroadcastReceiver
2.想使用广播 注册
a.manifest 注册

2.发送广播
创建意图:

  Intent intent = new Intent(MainActivity.this,MyBroadcastOne.class);

发送:

 sendBroadcast(intent);

b.通过java代码去注册

//注册广播
    MyBroadcastOne myBroadcastOne = new MyBroadcastOne();
    //过滤电池电量低
    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    registerReceiver(myBroadcastOne,intentFilter);

通过java代码注册的广播不需要手动去发送

        取消广播:unregisterReceiver(myBroadcastOne);

发送一个广播,代码如下:

MyBroadcastOne 代码:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * Created by sq on 2020/2/11.
 */
public class MyBroadcastOne extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("msg","123");
        int level=intent.getIntExtra("level",0);
        Log.i("msg"," "+level);
    }
}

Main2Activity 代码:

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Main2Activity extends AppCompatActivity {
    private Button button;
    MyBroadcastOne myBroadcastOne;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        button= (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Main2Activity.this,MyBroadcastOne.class);
                //发送广播
                sendBroadcast(intent);
            }
        });
    }

    @Override
    protected void onResume() {
         myBroadcastOne=new MyBroadcastOne();
        //过滤电池电量低
        IntentFilter intentFilter=new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(myBroadcastOne,intentFilter);
       super.onResume();
    }

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

        unregisterReceiver(myBroadcastOne);
    }
}

3.同时发送多个广播
在xml中注册 给广播设置action name属性值一样
Android:发送广播_第1张图片
发送广播之前,new 空的意图,给意图设置action
Android:发送广播_第2张图片

发送多个广播,代码如下:

MyBroadcastTwo代码

 import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * Created by sq on 2020/2/11.
 */
public class MyBroadcastTwo extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("msg","2");
    }
}

MyBroadcastThree代码

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * Created by sq on 2020/2/11.
 */
public class MyBroadcastThree extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("msg","3");
    }
}

Main2Activity代码

public class Main2Activity extends AppCompatActivity {
    private Button button;
    MyBroadcastOne myBroadcastOne;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        button= (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                //发送广播
                intent.setAction("a");
                sendBroadcast(intent);
            }
        });
    }

    @Override
    protected void onResume() {
         myBroadcastOne=new MyBroadcastOne();
        //过滤电池电量低
        IntentFilter intentFilter=new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(myBroadcastOne,intentFilter);
       super.onResume();
    }

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

        unregisterReceiver(myBroadcastOne);
    }
}

你可能感兴趣的:(Andiord)