Android中广播的使用

一.什么是广播?

在Android中,Broadcast是一种广泛运用的在应用程序之间传输信息的机制。我们拿广播电台来做个比方。我们平常使用收音机收音是这样的:许许多多不同的广播电台通过特定的频率来发送他们的内容,而我们用户只需要将频率调成和广播电台的一样就可以收听他们的内容了。Android中的广播机制就和这个差不多的道理。
电台发送的内容是语音,而在Android中我们要发送的广播内容是一个Intent。这个Intent中可以携带我们要传送的数据。
电台通过大功率的发射器发送内容,而在Android中则是通过sendBroadcast这个方法来发送(很形象的名字吧)。
用户通过调整到具体的电台频率接受电台的内容。而在Android中要接受广播中的内容则是通过注册一个BroadCastReceiver来接收的。只有发送广播的action和接收广播的action相同,接受者才能接受这个广播。

二.静态注册广播示例:
在你需要传入的Activity的AndroidMainifest中加入:

 <receiver android:name=".Myresive">
          <intent-filter >
              <action android:name="hello"/>
          </intent-filter>
      </receiver>

在你要传出广播的Activity中代码如下:

package com.example.administrator.myapplicat326;

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

public class MainActivity extends AppCompatActivity {
    Button button;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.qw);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent("hello");
                sendBroadcast(intent);//静态注册无序列表
//                sendOrderedBroadcast(intent, null);
            }
        });
    }
}

三.动态注册无序列表:
动态注册就是把原来静态注册写在AndroidMainifest中的以代码形式写出来:

package com.example.administrator.myapplica3266;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
private  Myresive  myresive;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         myresive= new Myresive();
        IntentFilter filter = new IntentFilter();

        filter.addAction("hello");

       registerReceiver(myresive, filter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myresive);
    }
}

四,静态注册有序列表:
发送广播的不变,Activity的AndroidMainifest中加入:

<receiver android:name=".Myresive">
          <intent-filter android:priority="1000" >//优先级
              <action android:name="hello"/>
          </intent-filter>
      </receiver>

在新建一个类继承BroadcastReceiver :


public class Myresive extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "叫我干嘛", Toast.LENGTH_SHORT).show();
        Log.e("1111111111111111","叫我干嘛");



    }
}

五。动态注册有序广播:

动态注册就是把原来静态注册写在AndroidMainifest中的以代码形式写出来:

package com.example.administrator.myapplica3266;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
private  Myresive  myresive;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         myresive= new Myresive();
        IntentFilter filter = new IntentFilter();
        filter.setPriority(1000);
        filter.addAction("hello");

       registerReceiver(myresive, filter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myresive);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值