編輯:關於Android編程
MainActivity如下:
package cc.testhome; import cc.testhome.HomeKeyObserver.OnHomeKeyListener; import cc.testhome.PowerKeyObserver.OnPowerKeyListener; import android.os.Bundle; import android.app.Activity; /** * Demo描述: * 利用廣播監聽Home鍵的按下和長按Home鍵 * 利用廣播監聽電源鍵的按下(關閉屏幕) * * 參考資料: * 1 http://blog.csdn.net/q445697127/article/details/8432513 * 2 http://blog.csdn.net/watt520/article/details/18959897 * 3 http://blog.csdn.net/lfdfhl/article/details/9903693 * Thank you very much */ public class MainActivity extends Activity { private HomeKeyObserver mHomeKeyObserver; private PowerKeyObserver mPowerKeyObserver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init() { mHomeKeyObserver = new HomeKeyObserver(this); mHomeKeyObserver.setHomeKeyListener(new OnHomeKeyListener() { @Override public void onHomeKeyPressed() { System.out.println("----> 按下Home鍵"); } @Override public void onHomeKeyLongPressed() { System.out.println("----> 長按Home鍵"); } }); mHomeKeyObserver.startListen(); ////////////////////////////////////////// mPowerKeyObserver = new PowerKeyObserver(this); mPowerKeyObserver.setHomeKeyListener(new OnPowerKeyListener() { @Override public void onPowerKeyPressed() { System.out.println("----> 按下電源鍵"); } }); mPowerKeyObserver.startListen(); } @Override protected void onDestroy() { super.onDestroy(); mHomeKeyObserver.stopListen(); ////////////////////////////////////////// mPowerKeyObserver.stopListen(); } }
HomeKeyObserver如下:
package cc.testhome; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; public class HomeKeyObserver { private Context mContext; private IntentFilter mIntentFilter; private OnHomeKeyListener mOnHomeKeyListener; private HomeKeyBroadcastReceiver mHomeKeyBroadcastReceiver; public HomeKeyObserver(Context context) { this.mContext = context; } //注冊廣播接收者 public void startListen(){ mIntentFilter=new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); mHomeKeyBroadcastReceiver=new HomeKeyBroadcastReceiver(); mContext.registerReceiver(mHomeKeyBroadcastReceiver, mIntentFilter); System.out.println("----> 開始監聽"); } //取消廣播接收者 public void stopListen(){ if (mHomeKeyBroadcastReceiver!=null) { mContext.unregisterReceiver(mHomeKeyBroadcastReceiver); System.out.println("----> 停止監聽"); } } // 對外暴露接口 public void setHomeKeyListener(OnHomeKeyListener homeKeyListener) { mOnHomeKeyListener = homeKeyListener; } // 回調接口 public interface OnHomeKeyListener { public void onHomeKeyPressed(); public void onHomeKeyLongPressed(); } //廣播接收者 class HomeKeyBroadcastReceiver extends BroadcastReceiver{ final String SYSTEM_DIALOG_REASON_KEY = "reason"; //按下Home鍵 final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey"; //長按Home鍵 final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps"; @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) { String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY); if (reason != null && mOnHomeKeyListener != null) { if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) { mOnHomeKeyListener.onHomeKeyPressed(); } else if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) { mOnHomeKeyListener.onHomeKeyLongPressed(); } } } } } }
PowerKeyObserver如下:
package cc.testhome; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; public class PowerKeyObserver { private Context mContext; private IntentFilter mIntentFilter; private OnPowerKeyListener mOnPowerKeyListener; private PowerKeyBroadcastReceiver mPowerKeyBroadcastReceiver; public PowerKeyObserver(Context context) { this.mContext = context; } //注冊廣播接收者 public void startListen(){ mIntentFilter=new IntentFilter(Intent.ACTION_SCREEN_OFF); mPowerKeyBroadcastReceiver=new PowerKeyBroadcastReceiver(); mContext.registerReceiver(mPowerKeyBroadcastReceiver, mIntentFilter); System.out.println("----> 開始監聽"); } //取消廣播接收者 public void stopListen(){ if (mPowerKeyBroadcastReceiver!=null) { mContext.unregisterReceiver(mPowerKeyBroadcastReceiver); System.out.println("----> 停止監聽"); } } // 對外暴露接口 public void setHomeKeyListener(OnPowerKeyListener powerKeyListener) { mOnPowerKeyListener = powerKeyListener; } // 回調接口 public interface OnPowerKeyListener { public void onPowerKeyPressed(); } //廣播接收者 class PowerKeyBroadcastReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(Intent.ACTION_SCREEN_OFF)) { mOnPowerKeyListener.onPowerKeyPressed(); } } } }
main.xml如下:
(一)概述本節給大家帶來的是Android四大組件中的最後一個——ContentProvider(內容提供者),可能部分讀者 有疑問了,&rdqu
在這裡簡單的介紹下怎麼統計自己研發的APP 的用戶活躍度,和使用量,以此來展示自己APP的用戶使用量!我們的APP都需要注入數據分析,以供我們實時的了解APP的下載和使用
首先看一張國內Top500 Android應用中它們用到的第三方推送以及所占數量:現在總結下Android平台下幾種推送方案的基本情況以及優缺點:一、使用GCM(Goog
安裝Android Support Repository最低版本 minSdkVersion 18添加Uiautomator在build.gradle添加對Uiautom