zoukankan      html  css  js  c++  java
  • 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>


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

  • 相关阅读:
    课下作业(ucosii、catuserlist、ls实现、stat实现等)-20175204张湲祯
    2019-2020-2 20175235 泽仁拉措《网络对抗技术》 Exp9 web安全基础
    2019-2020
    2019-2020
    2019-2020
    2019-2020
    2019-2020
    2019-2020-2 网络对抗技术 exp3 免杀原理与实践
    2019-2020-2《网络对抗技术》20175235 Exp2 后门原理与实践
    2019-2020-2 2 20175235 泽仁拉措 《网络对抗技术》实验一 PC平台逆向破解
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4033530.html
Copyright © 2011-2022 走看看