我是靠谱客的博主 俊秀帆布鞋,这篇文章主要介绍Android BroadcastReceiver实例Demo(有序广播的发送),现在分享给大家,希望可以做个参考。

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

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

发送顺序:

先发送第二条广播;

再发送第一条广播;

最后发送第三条广播。

代码例如以下:



布局文件:

activity_main(一个Button):

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<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:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
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:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
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(非常关键):

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?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>


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

最后

以上就是俊秀帆布鞋最近收集整理的关于Android BroadcastReceiver实例Demo(有序广播的发送)的全部内容,更多相关Android内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部