首页 技术 正文
技术 2022年11月20日
0 收藏 797 点赞 4,241 浏览 2610 个字

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SortedBroadcast extends Activity {
  Button send;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sorted_broadcast);
    //获取程序中的send按钮
    send = (Button) findViewById(R.id.send);
    send.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        // 创建Intent对象
        Intent intent = new Intent();
        intent.setAction(broadcasttest.action.CRAZY_BROADCAST”);
        intent.putExtra(“msg”, “简单的消息”);
        //发送有序广播
        sendOrderedBroadcast(intent, null);
      }
    });
  }

}

对于有序广播而言,它会按优先级依次触发每个BroadcastReceiver的onReceive()方法。

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class MyReceiver1 extends BroadcastReceiver{

  @Override
  public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, “接收到的Intent的Action为:”+
        intent.getAction()+”\n消息内容是:”+intent.getStringExtra(“msg”),
        5000).show();
    //创建一个Bundle对象,并存入数据
    Bundle bundle = new Bundle();
    bundle.putString(“first”, “第一个BroadcastReceiver存入的信息”);
    //将Bundle放入结果中
    setResultExtras(bundle);
    //取消Broadcast的继续传播
    abortBroadcast();
  }

}

上面的BroadcastReceiver不仅处理了它接收到的消息,而且向处理结果中存入了key为first的消息,这个消息将可以被第二个BroadcastReceiver解析出来。,不过最后一行代码用于取消广播,如果保持这条代码生效,那么优先级比MyReceiver1低的BroadcastReceiver都将不会被触发。

在AndroidManifest.xml文件中部署该BroadcastReceiver,并指定其优先级为20,配置片段如下:

<receiver android:name=”.MyReceiver1″>
  <intent-filter android:priority=”20″>
    <action android:name=”broadcasttest.action.CRAZY_BROADCAST”/>
  </intent-filter>
</receiver>

接下来再为程序提供第二个BroadcastReceiver,这个BroadcastReceiver将会解析前一个BroadcastReceiver所存入的key为first的消息,

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class MyReceiver2 extends BroadcastReceiver{

  @Override
  public void onReceive(Context context, Intent intent) {
    Bundle bundle = getResultExtras(true);
    //解析前一个BroadcastReceiver所存入的key为first的消息
    String first = bundle.getString(“first”);
    Toast.makeText(context, “第一个Broadcast存入的消息为:”+first,5000).show();

  }

}

在AndroidManifest.xml文件中部署该BroadcastReceiver,并指定其优先级为0,配置片段如下:

<receiver android:name=”.MyReceiver2″>
  <intent-filter android:priority=”0″>
    <action android:name=”broadcasttest.action.CRAZY_BROADCAST”/>
  </intent-filter>
</receiver>

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,906
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,430
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,247
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,058
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,690
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,727