Android中BroadCast与Activity之间的通信

时间:2022-01-31 21:10:23

在看本文之前,假设你对于Android的广播机制不是非常了解。建议先行阅读我转载的一篇博文:图解
Android 广播机制

因为本案例比較简单,故直接在此贴出代码,不做过多的阐述。

先上效果截图:

Android中BroadCast与Activity之间的通信Android中BroadCast与Activity之间的通信

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYW5kcm9pZF9qaWFuZ2p1bg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" width="200" height="200" alt="">

MainActivity的代码例如以下:
package com.gc.testbroadcasedemo;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
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;
import android.widget.TextView;
/**
*
* @author Android将军
*
*/
public class MainActivity extends Activity {
private Button mButton;
private TextView mTextView;
public static String ACTION_INTENT_TEST = "com.gc.broadcase.test"; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.message_tv);
mButton = (Button) findViewById(R.id.send_btn);
mButton.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent mIntent = new Intent(ACTION_INTENT_TEST);
sendBroadcast(mIntent);
}
});
registerMessageReceiver();
}
//在销毁时要与广播解绑
@Override
protected void onDestroy() {
unregisterReceiver(mMessageReceiver);
super.onDestroy();
} public MessageReceiver mMessageReceiver;
public static String ACTION_INTENT_RECEIVER = "com.gc.broadcast.receiver"; /**
* 动态注冊广播
*/
public void registerMessageReceiver() {
mMessageReceiver = new MessageReceiver();
IntentFilter filter = new IntentFilter(); filter.addAction(ACTION_INTENT_RECEIVER);
registerReceiver(mMessageReceiver, filter);
} public class MessageReceiver extends BroadcastReceiver { @Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals(ACTION_INTENT_RECEIVER)) {
mTextView.setText(intent.getStringExtra("message"));
}
} }
}

MyBroadCast的代码例如以下:

package com.gc.testbroadcasedemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
/**
*
* @author Android将军
*
*/
public class MyBroadCast extends BroadcastReceiver{ public MyBroadCast()
{
Log.v("BROADCAST_TAG", "MyBroadCast");
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.v("BROADCAST_TAG", "onReceive");
Bundle bundle = intent.getExtras();
if(intent.getAction().equals(MainActivity.ACTION_INTENT_TEST))
{
processCustomMessage(context, bundle);
}
}
//send msg to MainActivity
private void processCustomMessage(Context context, Bundle bundle) { Intent mIntent=new Intent(MainActivity.ACTION_INTENT_RECEIVER);
mIntent.putExtra("message", "測试Broadcast与Activity之间的通信");
context.sendBroadcast(mIntent); } }

案例project文件夹例如以下:

Android中BroadCast与Activity之间的通信

在AndroidManifest中静态注冊MyBroadCast广播。代码例如以下:

 <receiver android:name=".MyBroadCast">
<intent-filter >
<action android:name="com.gc.broadcase.test"/>
</intent-filter>
</receiver>

转载请注明出处:http://blog.csdn.net/android_jiangjun/article/details/39928243