标签:
broadcastReceiver开发步骤:
1 创建需要启动的BroadcastReceiver的Intent
2 调用context中的sendBroadcast或者sendOrderedBroadcast()方法来启动指定的广播接受者(BroadcastReceiver)
需要注意的是:当应用程序发出一个boradcastReceiver Intent时,所有匹配了该Intent的广播接受者都有可能被启动,具体的启动方式和顺序可以由我们设定。
创建andriod项目
创建layout布局代码如下
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/main_bt1"
android:text="启动无序广播" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/main_bt2"
android:text="启动有序广播" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/main_bt3"
android:text="动态注册广播" />
新建java类activity活动代码
package com.example.android_lession9_1;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button bt1,bt2,bt3;
private MyBroadcast my; //声明内部广播
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
my=new MyBroadcast();//实例化内部广播
//动态注册广播
MainActivity.this.registerReceiver(my, new IntentFilter("com.zyy.lession9_1_mydongbroadcast"));
bt1=(Button) findViewById(R.id.main_bt1);
bt3=(Button) findViewById(R.id.main_bt3);
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//点击发送无序广播
Intent t=new Intent("com.zyy.lession9_1_mybroadcast");
t.putExtra("key1", "abc");
MainActivity.this.sendBroadcast(t);
}
});
bt2=(Button) findViewById(R.id.main_bt2);
bt2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//点击发送有序广播
Intent i=new Intent("com.zyy.lession9_1_myorderbroadcast");
i.putExtra("key2", "隔壁老王");
//发送有序广播
MainActivity.this.sendOrderedBroadcast(i,null);
}
});
bt3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//发送动态广播
Intent i=new Intent("com.zyy.lession9_1_mydongbroadcast");
MainActivity.this.sendBroadcast(i);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//内部类广播不能注册在清单文件中,只能采用动态注册方式
public class MyBroadcast extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Log.d("zyy", "进入了内部广播");
}
}
}
新建三个广播代码
1.
package com.example.android_lession9_1;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadCast extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Log.d("zyy", context.toString());
Log.d("zyy", intent.getStringExtra("key1"));
}
}
2。
package com.example.android_lession9_1;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class MyOrderBroadCast extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String str=intent.getStringExtra("key2");
Log.d("zyy", "从main传递过来的值是"+str);
str="隔壁老y";
Bundle b=new Bundle();
b.putString("key3", str);
setResultExtras(b);
//中止传播广播
//abortBroadcast();
}
}
3.
package com.example.android_lession9_1;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyOrderBroadCast2 extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String str=intent.getStringExtra("key2");
String str2=getResultExtras(true).getString("key3");
Log.d("zyy", "从main获取的值是"+str);
Log.d("zyy", "从上一个广播发送过来的值获取的值是"+str2);
}
}
四配置清单文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android_lession9_1"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="17" />
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name="com.example.android_lession9_1.MainActivity"
android:label="@string/app_name" >
标签:
原文地址:http://www.cnblogs.com/andriod2016/p/5662304.html