首我们先来看看静态绑定的广播:
activity代码:
package com.example.broadcast_base;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button send = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send = (Button)findViewById(R.id.send);
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/**
* 静态方式,是先new 一个Intent对象,把这个对象的参数是一人上字符串,
* 这个字符串就是广播的内容。
* 然后就调用sendBroadcast()函数,就这样就完成了广播的发送
*/
Intent intent = new Intent("static_broadcast");
sendBroadcast(intent);
}
});
}
@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;
}
}
接收广播类:
package com.example.broadcast_base;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/**
* 广播的接收
* @author whh
*
* 广播的接要做两个地方,
* 1.编写接收类, 这个类一定要继承BroadcastReceiver, 然后重写这个类的onReceive函数。
* 2.编写androidManifest.xml配置文件
*/
public class Recevier extends BroadcastReceiver {
public Recevier()
{
System.out.println("静态 广播的构造");
}
@Override
public void onReceive(Context arg0, Intent arg1) {
//Log.v("BROADCAST_TAG", "onReceive");
System.out.println("接收到静态广播");
}
}
androidManifest.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcast_base"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.broadcast_base.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="Recevier"> <!-- 这个标签一定要写在application标签内, 这个name 就是接收广播的类名 -->
<intent-filter>
<action android:name="static_broadcast"></action> <!-- 这里的name就是接收的广播,也是构造Intent所用的字符串 -->
</intent-filter>
</receiver>
</application>
</manifest>
从结果中可以看到,这里的每接收到一条广播,那么就重新构造一个Receiver对象
但是我们的代码中并没有new那个类的对象,只是定义了一个继承了BroadcastReceiver的子类。
个人觉得是在那个androidManifest.xml文件中有文章,因这里有一个<receiver> 标签的名称是这个类名,
所以,这个自动生成的对象就应该是根据这个文件来操作的。
接下来让我们看看动态绑定的:
activity类:
package com.example.broadcastdynamic;
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.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button send = null;
DyReceiver receiver = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send = (Button)findViewById(R.id.send);
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("sb");
sendBroadcast(intent);
}
});
/**
*
*/
receiver = new DyReceiver();
/**
* 这里就类似于静态邦定的<receiver>标签, 只不过这里是用了代码来操作.
* 这里用到了一个intent过滤器
*/
IntentFilter filter = new IntentFilter("sb");
registerReceiver(receiver, filter);
}
@Override
protected void onStop() {
unregisterReceiver(receiver);
super.onStop();
}
@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;
}
}
接收类:
package com.example.broadcastdynamic;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class DyReceiver extends BroadcastReceiver{
public DyReceiver()
{
System.out.println("DyReceiver 构造");
}
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("动态 收到广播");
}
}
接收类的构造,
接收广播:
可见,动态并没有在接收广播时重新构造一个对象。
结论:我个人觉得动态绑定要比静态绑定要高效。