Nina_HY

只有尝到了坏果子,才知道好果子有多好吃,才会珍惜

导航

Android 的Broadcast与BroadcastReceiver的学习

查了很多资料

了解的流程如下

它是用来做什么的:是程序之间的通信,我们可以用来插件之间的通信

它的工作流程:有广播Broadcast 就要有接收者BroadcastReceiver。

OK ,其实看归看,实际操作一下就什么都明白了。具体的概念最好找一个简短的ppt看一下,然后运行一下就了解了。

我把他们的代码贴给大家共享一下,采用的是静态注册的方式(因为有两种方式可以采用),定义发送广播的方式为无序(这个也有两种方式)。

这个Activity是调用sendBroadcast(it)来说明要发一个广播的

package com.BroadcastTest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class BroadcastTestActivity extends Activity {
public static final String NEW_LIFEFORM_DETECTED =
"com.broadcasttest.NEW_LIFEFORM";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button btn0 = (Button)findViewById(R.id.btn0);
btn0.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent it = new Intent(NEW_LIFEFORM_DETECTED);
sendBroadcast(it);
}
});
}
}


Activity的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />

<Button
android:id="@+id/btn0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />

</LinearLayout>

那么接下来就是要接收广播的代码了:

package com.BroadcastTest;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MyBroadcastReceiver extends BroadcastReceiver {

public MyBroadcastReceiver () {
Log.v("BROADCAST_TAG", "myBroadCast");
}

 


@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "成功接收广播:", Toast.LENGTH_LONG).show();
}

}

下面就是配置文件中的静态注册的:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package
="com.BroadcastTest"
android:versionCode
="1"
android:versionName
="1.0" >

<uses-sdk android:minSdkVersion="7" />

<application
android:icon="@drawable/ic_launcher"
android:label
="@string/app_name" >
<activity
android:name=".BroadcastTestActivity"
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=".MyBroadcastReceiver">
<intent-filter>
<action android:name="com.broadcasttest.NEW_LIFEFORM" />
</intent-filter>
</receiver>
</application>

</manifest>



运行的结果如下:

时间仓促,理解的不够深刻,欢迎多多指正!

posted on 2012-04-03 20:39  Nina_HY  阅读(274)  评论(0编辑  收藏  举报