`
119568242
  • 浏览: 430973 次
  • 性别: 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属性

    当设置为`true`时,此`Activity`将不会出现在“最近使用的应用”列表中。这对于敏感操作或隐私相关的`Activity`很有用,可以防止用户意外访问。 #### android:exported 决定`Activity`是否可以被其他应用组件调用。...

    Android 销毁当前的Activity

    让 Android 横竖屏切换时不销毁当前的 Activity 需要使用 `android:configChanges` 属性和 `onConfigurationChanged` 方法。同时,我们也需要正确处理屏幕方向的改变,以确保应用程序的正确运行。

    Android activity属性设置大全.doc

    如果设置为"true",activity不会出现在最近使用的应用列表中,用户无法通过此列表快速回到该activity。 7. android:exported=["true" | "false"] 表示activity是否允许其他应用的组件(如意图)来调用。"true...

    Android基础系列的Activity

    为了避免不必要的重建,可以通过在AndroidManifest.xml中指定Activity的android:configChanges属性,或重写onConfigurationChanged()方法来手动处理配置变化。 总结起来,Android的Activity是构建用户界面和实现...

    随意调节你的activity的大小,activity 自定义窗口大小

    默认情况下,Activity会全屏显示,但我们可以通过修改Activity的属性和使用自定义布局来改变这一行为。 1. 修改Activity的属性: 在AndroidManifest.xml中,可以为特定Activity添加`android:resizeableActivity=...

    LauncherActivity 开发启动Activity的界面

    7. **处理配置变更**:如果`LauncherActivity`的布局可能因屏幕方向改变或其他配置变更而变化,需要适当地处理这些情况,例如使用`android:configChanges`属性或重写`onConfigurationChanged()`方法。 8. **测试与...

    android Activity与Fragment

    Fragment可以嵌入到Activity中,也可以独立存在。当Activity经历生命周期变化时,与其关联的Fragment也会相应地经历生命周期状态。 在Activity的屏幕旋转等配置更改时,Android默认会重新创建Activity及其包含的...

    Activity 的生命周期 以及 横屏竖屏切换时 Activity 的状态变化

    为了优化用户体验,避免每次屏幕方向改变时都重新创建`Activity`,可以在`AndroidManifest.xml`文件中为特定的`Activity`添加`android:configChanges`属性。例如: 1. **android:configChanges="orientation"**:...

    Android模拟Activity进出栈.zip

    开发者可以通过重写onSaveInstanceState()保存状态,onRestoreInstanceState()恢复状态,或者使用configChanges属性避免重启。 8. **TaskAffinity**:每个Activity都关联了一个任务亲和力,决定它应该属于哪个任务...

    android Activity 详述 demo

    开发者可以使用`android:configChanges`属性来手动处理配置变更,避免不必要的重建。 六、Activity的保存与恢复状态 当Activity被系统销毁(如屏幕旋转)时,可以使用`onSaveInstanceState(Bundle)`保存当前状态,...

    Android开发中Activity属性设置小结

    4. **android:configChanges**:指定哪些配置变化会导致Activity的`onConfigurationChanged()`方法被调用,如屏幕方向、导航方式或语言更改等。如果不想因为配置改变而重新创建Activity,可以手动处理这些变化。 5....

    深入理解Activity生命周期

    使用`android:configChanges`属性可以在XML清单文件中指定哪些配置更改应由Activity自己处理,而不是默认销毁并重建Activity。`onConfigurationChanged(Configuration newConfig)`方法可用于处理这些特定配置变化。 ...

    Activity_one

    通过修改`AndroidManifest.xml`中的`&lt;activity&gt;`标签,可以设置`android:theme`属性,使用预定义的主题或者自定义主题样式。 6. **多窗口支持**:从Android 7.0开始,`Activity`支持在分屏模式下运行。开发者需要...

    设备旋转与Activity生命周期

    通过在AndroidManifest.xml中对应的Activity标签中添加`android:configChanges="orientation|screenSize"`属性,指示系统不要销毁Activity,而是调用`onConfigurationChanged(Configuration newConfig)`方法来处理...

    Android Activity嵌套Fragmnet实现横竖屏切换

    1. 使用`android:configChanges="orientation|screenSize"`属性,在AndroidManifest.xml中指定Activity对横竖屏变化的处理方式,让系统不再默认销毁Activity。 2. 在Activity中重写`onConfigurationChanged...

    Android Activity内嵌Fragment,当Activity recreate时Fragment被添加多次,造成界面重叠

    此外,如果你使用的是静态Fragment(即在布局文件中定义的Fragment),可能需要在Activity的`onCreate(Bundle savedInstanceState)`方法中移除并重新添加Fragment,以确保配置改变后能正确更新。 总之,处理...

    Activity生命周期

    默认情况下,配置变更会导致Activity重建,但可以通过在AndroidManifest.xml中设置`android:configChanges`属性或重写`onConfigurationChanged(Configuration newConfig)`来控制如何处理配置变更。 理解并熟练掌握...

    activity

    开发者可以通过重写onSaveInstanceState()保存状态,并在onRestoreInstanceState()中恢复,或者使用configChanges属性避免不必要的重建。 7. **Fragment与Activity**:Fragment是Android 3.0引入的组件,可以嵌入到...

    Android的Activity周期测试

    在名为"TestAndroid"的项目中,我们可以创建一个简单的Activity,覆盖上述提到的生命周期方法,并在每个方法内打印日志。通过改变设备状态,比如旋转屏幕,可以看到onSaveInstanceState和onConfigurationChanged...

    安卓Android源码——(Activity跳转与操作).zip

    12. 可以在Activity中覆写onSaveInstanceState(Bundle outState)和onRestoreInstanceState(Bundle savedInstanceState)方法,以保存和恢复Activity的状态,防止因系统原因导致的Activity销毁。 通过学习这个压缩包...

Global site tag (gtag.js) - Google Analytics