編輯:關於Android編程
如果用戶開啟了設置裡的屏幕旋轉,Android中處理橫豎屏切換,通常的做法是在AndroidManifest.xml中定義android:configChanges=orientation|keyboardHidden,然後在重寫onOrientationChanged方法,如下:
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
Log.i(info, landscape); // 橫屏
} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
Log.i(info, portrait); // 豎屏
}
如果用戶在設置裡禁止了屏幕旋轉,怎麼在應用中獲取手機的方向呢?我們可以用OrientationEventListener(方向事件監聽器),是一個當方向發生變化時,從SensorManager(傳感器管理程序)接收通知的輔助類。
用法如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
mAlbumOrientationEventListener = new AlbumOrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL);
if (mAlbumOrientationEventListener.canDetectOrientation()) {
mAlbumOrientationEventListener.enable();
} else {
Log.d(chengcj1, Can't Detect Orientation);
}
}
@Override
protected void onDestroy() {
mAlbumOrientationEventListener.disable();
super.onDestroy();
}
private class AlbumOrientationEventListener extends OrientationEventListener {
public AlbumOrientationEventListener(Context context) {
super(context);
}
public AlbumOrientationEventListener(Context context, int rate) {
super(context, rate);
}
@Override
public void onOrientationChanged(int orientation) {
if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
return;
}
//保證只返回四個方向
int newOrientation = ((orientation + 45) / 90 * 90) % 360
if (newOrientation != mOrientation) {
mOrientation = newOrientation;
//返回的mOrientation就是手機方向,為0°、90°、180°和270°中的一個
}
}
}
說明:只要手機偏移一點,方向發生變化時,onOrientationChanged會一直執行,但是在實際開發中,我們只關心4個方向(0°,90°,180°,270°),因此用上面的方法轉化一下,轉化原理為:當偏移角度小於45°,都當成0°來處理;大於45°,小於90°,都當成90°來處理;同理,大於90°,小於135°,當成90°來處理;大於135°,小於180°,都當成180°來處理,依此類推......
Android Service的綁定過程
通常我們使用Service都要和它通信,當想要與Service通信的時候,那麼Service要處於綁定狀態的。然後客戶端可以拿到一個Binder與服務端進行通信,這個過程
Android中:Activity、Content Provider、Broadcast和AIDLService4種跨歷程通訊的方式
由於android系統中應用程序之間不能共享內存。因此,在不同應用程序之間交互數據(跨進程通訊)就稍微麻煩一些。在android SDK中提供了4種用於跨進程通訊的方式。
Activity的聲明周期和啟動模式
正常情況下Activity的聲明周期先來看看官方給出的聲明周期圖:這裡需要說明如下幾點:針對特定Activity,第一次啟動,回調如下:onCreate->onSt
移動一卡多號業務開通/價錢/APP使用指南
中國移動推出的一卡多號業務可以在已有移動手機號上增加1-3個副號,不用換機、換卡。特別適合想擁有多個手機號碼的用戶或需要保護隱私的用戶服務。在不增加手機、不