`
119568242
  • 浏览: 420221 次
  • 性别: Icon_minigender_1
  • 来自: 深圳/湛江
社区版块
存档分类
最新评论

[转载]Activity中ConfigChanges属性的用法

 
阅读更多

 

[转载]Activity中ConfigChanges属性的用法

 通过设置这个属性可以使Activity捕捉设备状态变化,以下是可以被识别的内容:  
CONFIG_FONT_SCALE
CONFIG_MCC
CONFIG_MNC
CONFIG_LOCALE
CONFIG_TOUCHSCREEN
CONFIG_KEYBOARD
CONFIG_NAVIGATION
CONFIG_ORIENTATION

设置方法:将下列字段用“|”符号分隔开,例如:“locale|navigation|orientation

Value Description
mcc The IMSI mobile country code (MCC) has changed — that is, a SIM hasbeen detected and updated the MCC.移动国家号码,由三位数字组成,每个国家都有自己独立的MCC,可以识别手机用户所属国家。
mnc The IMSI mobile network code (MNC) has changed — that is, a SIM hasbeen detected and updated the MNC.移动网号,在一个国家或者地区中,用于区分手机用户的服务商。
locale The locale has changed — for example, the user has selected a new language that text should be displayed in.用户所在地区发生变化。
touchscreen The touchscreen has changed. (This should never normally happen.)
keyboard The keyboard type has changed — for example, the user has plugged in an external keyboard.键盘模式发生变化,例如:用户接入外部键盘输入
keyboardHidden The keyboard accessibility has changed — for example, the user has slid the keyboard out to expose it.用户打开手机硬件键盘
navigation The navigation type has changed. (This should never normally happen.)
orientation The screen orientation has changed — that is, the user has rotated the device.设备旋转,横向显示和竖向显示模式切换。
fontScale The font scaling factor has changed — that is, the user has selected a new global font size.全局字体大小缩放发生改变
通过一个例子介绍这个属性的用法: 首先需要修改项目的manifest:
?View Code XML
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.androidres.ConfigChangedTesting"
      android:versionCode="1"
      android:versionName="1.0.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ConfigChangedTesting"
                  android:label="@string/app_name"
                  android:configChanges="keyboardHidden|orientation">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

在Activity中添加了 android:configChanges属性,目的是当所指定属性(Configuration Changes)发生改变时,通知程序调用 onConfigurationChanged()函数。 创建一个Layout UI: 
?View Code XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button
        android:id="@+id/pick"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Pick"
    />
<Button
        android:id="@+id/view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="View"
    />
</LinearLayout>

这个简单的UI包含两个按钮,其中一个是通过Contact列表选择一个联系人,另外一个是查看当前选择联系人的详细内容。
 

项目的Java源代码:
01.import android.app.Activity;
02.import android.content.Intent;
03.import android.content.res.Configuration;
04.import android.net.Uri;
05.import android.os.Bundle;
06.import android.provider.Contacts.People;
07.import android.view.View;
08.import android.widget.Button;
09.

10.public class ConfigChangedTesting extends Activity {
11.    /** Called when the activity is first created. */
12.    static final int PICK_REQUEST = 1337;
13.    Button viewButton=null;
14.    Uri contact = null;
15.    @Override
16.    public void onCreate(Bundle savedInstanceState) {
17.        super.onCreate(savedInstanceState);
18.        //setContentView(R.layout.main);
19.

20.        setupViews();
21.    }
22.

23.    public void onConfigurationChanged(Configuration newConfig) {
24.                 super.onConfigurationChanged(newConfig);  
25.

26.                 setupViews();
27.    }  
28.

29.    /* (non-Javadoc)
30.     * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
31.     */
32.    @Override
33.    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
34.        // TODO Auto-generated method stub
35.        //super.onActivityResult(requestCode, resultCode, data);
36.

37.        if(requestCode == PICK_REQUEST){
38.

39.            if(resultCode==RESULT_OK){
40.

41.                contact = data.getData();
42.                viewButton.setEnabled(true);
43.            }
44.

45.        }
46.

47.    }
48.

49.    private void setupViews(){
50.

51.        setContentView(R.layout.main);
52.

53.        Button pickBtn = (Button)findViewById(R.id.pick);
54.

55.        pickBtn.setOnClickListener(new View.OnClickListener(){
56.

57.            public void onClick(View v) {
58.                // TODO Auto-generated method stub
59.

60.                Intent i=new Intent(Intent.ACTION_PICK,People.CONTENT_URI);
61.                startActivityForResult(i,PICK_REQUEST);
62.            }
63.        });
64.

65.        viewButton =(Button)findViewById(R.id.view);  
66.

67.        viewButton.setOnClickListener(new View.OnClickListener() {
68.                    public void onClick(View view) {
69.                        startActivity(new Intent(Intent.ACTION_VIEW, contact));
70.                    }
71.        });  
72.

73.        viewButton.setEnabled(contact!=null);
74.    }
75.}

 

分享到:
评论

相关推荐

    Android Activity横竖屏切换生命周期详解

    一般的我们去切换屏幕方向都是不希望Activity被重新创建,这时就需要对一些属性进行设置,或者使用代码设置。文章通过以上方面解析Activity在横竖屏切换时,生命周期方法执行过程。 1.Activity生命周期 2....

    Activity代理框架AyoActivityNoManifest.zip

    ActivityAttacher就是附着在上面4个模板Activity里的Activity代理ActivityAttacher中持有一个Activity实例对象,是在onCreate时赋值的ActivityAttacher可以处理Activity中的所有配置和生命周期ActivityAttacher提供...

    Android设置Activity背景为透明style的简单方法(必看)

    方法一: 通过Theme.Translucent ...只需要在Manifest中需要透明的Activity内设置theme为以上任意一个就可以了 &lt;activity android:name=com.vixtel.simulate.MainApp android:configChanges=keyboardHidden|

    android edittext不弹出软键盘三种方法

    在AndroidMainfest.xml中选择哪个activity,设置windowSoftInputMode属性为adjustUnspecified|stateHidden 例如: &lt;activity android:name=".Main" android:label="@string/app_name" android:...

    解决Android手机屏幕横竖屏切换

    1. 在AndroidManifest.xml中为Activity设置configChanges属性, application android:icon=@drawable/icon android:label=@string/app_name&gt; &lt;activity android:name=.MainActivity android:label=@string/app_...

    android:configChanges="orientation|keyboardHidden"的使用-附件资源

    android:configChanges="orientation|keyboardHidden"的使用-附件资源

    Android编程实现横竖屏切换时不销毁当前activity和锁定屏幕的方法

    首先在Mainifest.xml的Activity元素中加入android:configChanges=”orientation|keyboardHidden”属性 &lt;activityandroid name=.FileBrowserandroid:label app_nameandroid:configChanges=orientation|...

    Android相机 解决三星bug版本

    最近在Android项目中使用拍照功能 , 其它型号的手机运行成功了 唯独在三星的相机上遇到了bug . BUG具体体现为 : (1) 摄像头拍照后图片数据不一定能返回 ; onActivityResult的data为空 (2) 三星的camera强制切换到...

    BiliBili-基于ijkplayer rxjava retrofit,组件化思想,实现一个仿B站的Android客户端.zip

     android:name="android.permission.READ_EXTERNAL_STORAGE"/&gt;注意,如果横竖屏切换不想重新走一遍生命周期,还需要在表单中设置Activity的configChanges属性:&lt;activity android:name=".PlayActivity" ...

    android 横竖屏切换生命周期

    为了避免activity 重新加载过程中有闪屏的现象,则需要再清单文件中配置该activity 的属性 android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation|screenSize",详细信息请参阅...

    Android 4高级编程第三版_2

    这个是Android 4高级编程第三版的完整pdf文档,受最大上传大小的限制,切分成了十份,每份就最好的数字不一样,比如说第二份就是Android 4 高级编程第三版_2,代码可以在清华出版社的网站上下载

    android横竖屏切换不重启activity解决方案

    部分网友会发现Activity在切换到后台或布局从横屏LANDSCAPE切换到PORTRAIT,会重新切换Activity会触发一次onCreate方法,我们可以在androidmanifest.xml中的activit元素加入这个属性Android:configChanges=”...

    FragmentDemo

    FragmentDemo

    Android分屏多窗口的实践代码

    解决方法:在manifest文件里面给activity加上下面一行属性 android:configChanges=screenSize|smallestScreenSize|screenLayout|orientation 2.当启用多窗口模式时,可能有改变原UI的需求,这时可以在代码里面修改...

    android实现在横竖屏切换时页面信息不被重置的示例分享

    在 AnroidMainifest.xml 的 activity 元素中加入: 代码如下:android:configChanges=”orientation|keyboardHidden”或 代码如下:android:configChanges=”orientation|keyboard|keyboardHidden” 表示在改变屏幕...

    FragmentDemo修改了切屏重叠问题

    guolin的fragmentdemo, 修改了切屏时重叠问题, AndroidManifest &lt;activity加入 android:configChanges="keyboardHidden|orientation|screenSize" 即可

    androidfaqs:例程 TrickConfigurationSnippets 的总结

    在您的AndroidManifest文件查找中包含相应的活动标签,并将android:configChanges="orientation|screenSize"到其中。 现在您的活动标签将类似于&lt;activity name= ".YourActivity" android:configChanges=...

    Android EditText默认不弹出输入法的实现方法

    1. 在AndroidManifest.xml中将需要默认隐藏键盘的Activity中添加属性即可(常用此方法) android:windowSoftInputMode=adjustUnspecified|stateHidden android:configChanges=orientation|keyboardHidden 例如: &lt...

    Android RetainFragment状态保存的方法

    这种情况适用于屏幕旋转和配置变化,只要作用是阻止Activity重建,因此对于【语言】【时区】的调整可能需要重新启动Activity才能更新。 注意: 语言的变化需要配置为 android:configChanges=locale|layoutDirection...

Global site tag (gtag.js) - Google Analytics