2012-04-18 307 views
0

我試圖構建一個測試應用程序,只是在發生配置更改時中繼Toast消息。 (以及它應該做什麼,它不起作用)最終目的是檢測用戶是否將平板電腦放入鍵盤底座或將其從其中移除。我的清單和主要活動在下面..我認爲這個代碼會觸發烤麪包,當平板電腦的配置更改爲uiMode或外部鍵盤時..但是當我停靠/取消停放時,沒有任何事情發生..請幫助檢測uiMode或鍵盤變化android

我的清單:

<?xml version="1.0" encoding="utf-8"?> 

<uses-sdk android:minSdkVersion="12" /> 

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <activity 
     android:name=".UiModeTestActivity" 
     android:configChanges="keyboard|uiMode" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

我的Java:

package com.eliddell; 

import android.app.Activity; 
import android.content.res.Configuration; 
import android.os.Bundle; 
import android.widget.Toast; 

public class UiModeTestActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 
    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     Toast.makeText(getApplicationContext(), "new config:"+newConfig, Toast.LENGTH_LONG).show(); 
    } 
} 

回答

0

您的代碼看起來不錯,但...

正如消息人士說:

/** 
* The kind of keyboard attached to the device. 
* One of: {@link #KEYBOARD_NOKEYS}, {@link #KEYBOARD_QWERTY}, 
* {@link #KEYBOARD_12KEY}. 
*/ 
public int keyboard; 

所以,我覺得如果KEYBOARD_QWERTY變化KEYBOARD_12KEY

源的另一部分表示鍵盤將改變:

/** 
    * A flag indicating whether any keyboard is available. Unlike 
    * {@link #hardKeyboardHidden}, this also takes into account a soft 
    * keyboard, so if the hard keyboard is hidden but there is soft 
    * keyboard available, it will be set to NO. Value is one of: 
    * {@link #KEYBOARDHIDDEN_NO}, {@link #KEYBOARDHIDDEN_YES}. 
    */ 
    public int keyboardHidden; 

因此,也許而不是android:configChanges="keyboard|uiMode"你會嘗試android:configChanges="keyboardHidden|uiMode"

不幸的是,我沒有適配器將鍵盤插入我的設備,並檢查我的理論。所以試試吧!