Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android中BroadCast與Activity之間的通信

Android中BroadCast與Activity之間的通信

編輯:關於Android編程

在看本文之前,如果你對於Android的廣播機制不是很了解,建議先行閱讀我轉載的一篇博文:圖解 Android 廣播機制。

由於本案例比較簡單,故直接在此貼出代碼,不做過多的闡述。

先上效果截圖:

\\ 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);
				
		}

}
案例工程目錄如下:
\
在AndroidManifest中靜態注冊MyBroadCast廣播,代碼如下:
 
            
                
            
        
轉載請注明出處:http://blog.csdn.net/android_jiangjun/article/details/39928243

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved