静态广播与动态广播

静态广播
1.在AndroidManifest.xml的标签里加入


        <receiver android:name=".Receiver">
           <intent-filter>
        <action android:name="cn.text"/>
           intent-filter>
        receiver>

2.手动发送广播。

package com.example.brocastservers;

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;
import android.widget.Toast;


/*
    发送广播
 */
public class MainActivity extends AppCompatActivity {
    private Button button;

    private View.OnClickListener listener = new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            //发送地址  给接受者注册一个地址
            intent.setAction("cn.text");
            intent.putExtra("name", "发送的消息");
            MainActivity.this.sendBroadcast(intent);
            Toast.makeText(getApplicationContext(), "发送广播成功", Toast.LENGTH_SHORT).show();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        button = (Button)this.findViewById(R.id.button);
        button.setOnClickListener(listener);
    }


}

3.接收广播

package com.example.brocastservers;

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

/**
 * Created by Administrator on 2018/6/6.
 */
//接受广播

public class Receiver extends BroadcastReceiver {
    @Override
    // 复写onReceive()方法
  // 接收到广播后,则自动调用该方法
    public void onReceive(Context context, Intent intent) {
    //接收之后的操作
        String name = intent.getExtras().getString("name");
        Log.d("ser", "接收的内容"+name);

    }
}

这里写图片描述

动态广播
1.因为不用再清单文件中配置

package com.example.brocastservers;

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;
import android.widget.Toast;


/*
    发送广播
 */
public class MainActivity extends AppCompatActivity {
    private Button button;

    private View.OnClickListener listener = new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            //发送地址  给接受者注册一个地址
            intent.setAction("android.intent.action.MY_BROADCAST");
            intent.putExtra("name", "发送的消息");
            MainActivity.this.sendBroadcast(intent);
            Toast.makeText(getApplicationContext(), "发送广播成功", Toast.LENGTH_SHORT).show();
        }
    };
    private Receiver receiver;

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

        //实例化接受者
        receiver = new Receiver();

        IntentFilter filter = new IntentFilter();
        //注册地址 
        //因为不用配置,所以我们的地址也要一样
        filter.addAction("android.intent.action.MY_BROADCAST");

        //注册
        registerReceiver(receiver, filter);


        button = this.findViewById(R.id.button);
        button.setOnClickListener(listener);
    }

    //解除注册
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }
}

2.接受者

package com.example.brocastservers;

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

/**
 * Created by Administrator on 2018/6/6.
 */
//接受广播

public class Receiver extends BroadcastReceiver {
    @Override
    // 复写onReceive()方法
  // 接收到广播后,则自动调用该方法
    public void onReceive(Context context, Intent intent) {
    //接收之后的操作
        String name = intent.getExtras().getString("name");
        Log.d("ser", "接收的内容"+name);
    }
}

你可能感兴趣的:(静态广播与动态广播)