发送有序广播
1 单个广播 触发
AndroidManifest.xml
<!-- 有序广播接收器使用的权限 -->
<uses-permission android:name="my.permission.receiver"></uses-permission>
<receiver android:name=".MyReceiver">
<intent-filter android:priority="-1000">
<action android:name="com.example.androd_8_onderedreceiver_Action_receiver"></action>
</intent-filter>
</receiver>
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.btn1);
// 发送有序广播---静态注册
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setAction("xyz.lgch.onderedreceiver_Action_receiver");
intent.putExtra("message", "发送有序广播 www.lgch.xyz");
sendOrderedBroadcast(intent, "my.permission.receiver");
}
});
}
MyReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.widget.Toast;
// 创建 接收广播
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
// TODO Auto-generated method stub
String message = "广播信息" + arg1.getStringExtra("message");
Toast toast = Toast.makeText(context, message, 3000);
toast.setGravity(Gravity.TOP, 0, 0);
toast.show();
}
}
2 广播 传递 (暂时这么说)
触发广播2后,动态广播接收到参数后 触发
MainActivity.java
<!-- 发送广播的自定义权限 -->
<permission android:name="my.permission.receiver"></permission>
<!-- 有序广播接收器使用的权限 -->
<uses-permission android:name="my.permission.receiver"></uses-permission>
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn1);
// 动态注册
IntentFilter intentFilter = new IntentFilter();
intentFilter
.addAction("xyz.lgch.onderedreceiver_Action_receiver");
intentFilter.setPriority(0);
registerReceiver(new MyReceiver5(), intentFilter);
// 发送有序广播---静态注册
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setAction("xyz.lgch.onderedreceiver_Action_receiver");
intent.putExtra("message", "发送有序广播 www.lgch.xyz");
sendOrderedBroadcast(intent, "my.permission.receiver");
}
});
}
}
MyReceiver2.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Toast;
public class MyReceiver2 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
// TODO Auto-generated method stub
String message = "广播信息:" + arg1.getStringExtra("message");
Toast toast = Toast.makeText(context, message, 3000);
toast.setGravity(Gravity.TOP, 0, 0);
toast.show();
Bundle bundle = new Bundle();
bundle.putString("aa","666");
setResultExtras(bundle);
}
}
MyReceiver.java
package com.example.testapp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Toast;
public class MyReceiver5 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
Bundle bundle = getResultExtras(true);
String data = bundle.getString("aa");
String message = "广播信息" + arg1.getStringExtra("message");
Toast toast = Toast.makeText(context, message + "---" + data, 3000);
toast.setGravity(Gravity.TOP, 0, 0);
toast.show();
}
}