編輯:關於Android編程
在看本文之前,如果你對於Android的廣播機制不是很了解,建議先行閱讀我轉載的一篇博文:圖解 Android 廣播機制。
由於本案例比較簡單,故直接在此貼出代碼,不做過多的闡述。
先上效果截圖:
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")); } } } }
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); } }案例工程目錄如下:
轉載請注明出處:http://blog.csdn.net/android_jiangjun/article/details/39928243
在前不久的谷歌2015 I/O大會上,發布了Android新版本M,貌似從這個版本開始Android不在以數字命名版本了。在這次的I/O大會上谷歌對Andro
Android屏幕適配出現的原因在我們學習如何進行屏幕適配之前,我們需要先了解下為什麼Android需要進行屏幕適配。由於Android系統的開放性,任何用戶、開發者、O
屏幕亮度自動調節:主要是從Sensor分析之中分離出來分析LIGHT 光線感應器,因此就分析一下自動調節屏幕亮度(手機隨著光線的強度自我調節,也就是在亮的光線下屏幕自動調
效果如下:布局文件activity_main.xml如下: MainActivity.java代碼如下:import android.app.Act