2012-04-18 309 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"

不幸的是,我没有适配器将键盘插入我的设备,并检查我的理论。所以试试吧!