您的位置:首页 > 其它

广播接收者---发送有序广播

2017-06-05 08:54 375 查看

广播接收者----发送有序广播

1.功能描述:实现拦截一条有序广播。

2.技术要点:通过sendOrderedBroadcast()方法实现发送一条有序广播。

(1).用户交互界面的设计与实现

(2).界面交互代码的设计与实现

(3).创建3个广播接收者

  MyReceiverOne.java

 MyReceiverTwo.java

MyReceiverThree.java

3.实现步骤:设置广播接收者的优先级

4.

.


5.具体实现代码:

MainActivity

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void send(View view){
Intent intent=new Intent();
intent.setAction("intercept_stitch");
sendBroadcast(intent,null);
}
}
6.MyReceiverOne.java

public class MyReceiverOne extends BroadcastReceiver {
public MyReceiverOne() {
}

@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyReceiverOne","自定义的广播接收者one,接收到了广播事件");

}
}
7.MyReceiverTwo.jsva

public class MyReceiverTwo extends BroadcastReceiver {
public MyReceiverTwo() {
}

@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyReceiverTwo","自定义广播接收者Two,已接受到了广播事件");

}
}
8.MyReceiverThree.java
public class MyReceiverThree extends BroadcastReceiver {
public MyReceiverThree() {
}

@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyreceiverThree","自定义广播接收者Three,已接收到了广播事件");
}
}
9.清单文件:AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.edu.bzu.myapplication">

<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name"
android:supportsRtl="true" android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver android:name=".MyReceiverOne" >
<intent-filter android:priority="1000">
<action android:name="intercept_stitch"></action>
</intent-filter>
</receiver>
<receiver android:name="cn.edu.bzu.myapplication.MyReceiverTwo"
>
<intent-filter android:priority="200">
<action android:name="intercept_stitch" ></action>
</intent-filter>

</receiver>
<receiver android:name="cn.edu.bzu.myapplication.MyReceiverThree"
>
<intent-filter android:priority="600">
<action android:name="intercept_stitch" ></action>
</intent-filter>
</receiver>
</application>

</manifest>
10.运行界面:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: