• Android BroadcastReceiver实例Demo(有序广播的发送)


    上一篇简介了广播的发送,这篇主要介绍下,有序广播的发送。

    设置完相关属性的时候,广播就会依照有序的方式进行发送:

    发送顺序:

    先发送第二条广播;

    再发送第一条广播;

    最后发送第三条广播。

    代码例如以下:



    布局文件:

    activity_main(一个Button):

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:text="点击发送有序广播" />
    
    </RelativeLayout>
    

    MainActivity:

    package com.android_broadcasereceiverdemo2;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends Activity {
    	private Button button;
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		
    		button = (Button) findViewById(R.id.button1);
    		button.setOnClickListener(new View.OnClickListener() {
    			
    			@Override
    			public void onClick(View v) {
    				Intent intent = new Intent();
    				intent.setAction("myaction");
    				intent.putExtra("name", "梅西");
    				sendOrderedBroadcast(intent, null);
    			}
    		});
    	}
    }
    


    FirstBroadcast:

    package com.android_broadcasereceiverdemo2;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.widget.Toast;
    
    public class FirstBroadcast extends BroadcastReceiver {
    
    	@Override
    	public void onReceive(Context context, Intent intent) {
    		
    		String name = intent.getStringExtra("name");
    		Toast.makeText(context, "第一条广播已发送..."+name, Toast.LENGTH_SHORT).show();
    //		abortBroadcast();//当加上这条代码的时候,广播发送到此结束,即第三条广播不会再收到。
    	}
    
    }
    

    SecondBroadcast:

    package com.android_broadcasereceiverdemo2;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.widget.Toast;
    
    public class SecondBroadcast extends BroadcastReceiver {
    
    	@Override
    	public void onReceive(Context context, Intent intent) {
    		
    		String name = intent.getStringExtra("name");
    		Toast.makeText(context, "第二条广播已发送..."+name, Toast.LENGTH_SHORT).show();
    	}
    
    }
    


    ThirdBroadcast:

    package com.android_broadcasereceiverdemo2;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.widget.Toast;
    
    public class ThirdBroadcast extends BroadcastReceiver {
    
    	@Override
    	public void onReceive(Context context, Intent intent) {
    		
    		String name = intent.getStringExtra("name");
    		Toast.makeText(context, "第三条广播已发送..."+name, Toast.LENGTH_SHORT).show();
    	}
    
    }
    

    AndroidManifest.xml(非常关键):

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.android_broadcasereceiverdemo2"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="11"
            android:targetSdkVersion="18" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.android_broadcasereceiverdemo2.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <receiver android:name=".FirstBroadcast" >
                <!-- 有序广播中,priority数值越大,优先级越大,也就越先发送!! -->
                <intent-filter android:priority="99">
                    <action android:name="myaction" />
                </intent-filter>
            </receiver>
            
            <receiver android:name=".SecondBroadcast" >
                <intent-filter android:priority="100">
                    <action android:name="myaction" />
                </intent-filter>
            </receiver>
            
            <receiver android:name=".ThirdBroadcast" >
                <intent-filter android:priority="98">
                    <action android:name="myaction" />
                </intent-filter>
            </receiver>
        </application>
    
    </manifest>


    须要源代码的读者能够到我的资源中下载。

  • 相关阅读:
    windows C++ 获得一个进程的线程数目
    乌龟 SVN 在update后,如何知道 update前的版本号?
    C++ 类中的static对象貌似不能是类对象
    WinForms C#:html编辑器工程源码,含直接写WebBrowser的文件流、IPersistStreamInit接口的声明和一些相关的小方法
    用Remoting 实现一个文件传输组件
    [转载]Extreme Game Programming
    C#:10进制转2进制函数
    bindows 源码格式化工具(测试中的测试)C#源码
    明天又是周末,可以好好休息了!
    将文件加入到图形文件里;
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4033530.html
Copyright © 2020-2023  润新知