Android ApiDemo分析(七)

来源:互联网 发布:深圳网络股东大会投票 编辑:程序博客网 时间:2024/06/01 23:31
app/Dialog

这个Demo演示了各种Dialog类型的用法;

功能描述:按下不同的按钮将显示不同类型的Dialog;

相关内容:
1、onCreateDialog(int id) 
该方法是Activity的一个方法,用于创建Dialog,使用时需要首先调用showDialog (int id)方法,之后系统会根据id在onCreateDialog方法体内寻找对应id的Dialog进行创建。而在创建这个对话框实例后,在每次showDialog之前,如果需要对这个对话框做些修改可以重载onPrepareDialog方法来实现。
2、Dialog
Android支持以下几种类型的对话框:
       1)警告对话框 AlertDialog:  一个可以有0到3个按钮, 一个单选框或复选框的列表的对话框. 警告对话框可以创建大多数的交互界面, 是推荐的类型.
  2)进度对话框 ProgressDialog:  显示一个进度环或者一个进度条. 由于它是AlertDialog的扩展, 所以它也支持按钮.
  3)日期选择对话框 DatePickerDialog:  让用户选择一个日期.
  4)时间选择对话框 TimePickerDialog:  让用户选择一个时间.
       5)自定义对话框
其中AlertDialog是应用最广的一类对话框,它包含标题、内容、按钮3个部分,由此可以扩展出各种简单类型的对话框.
由于AlertDialog的构造方法被声明为protected,所以不能直接使用new关键字来创建AlertDialog类的实例对象,需要使用AlertDialog类中定义的一个内嵌类AlertDialog.Builder。因此必须创建AlertDialog.Builder类的对象实例,然后再调用show()来显示对话框。 


app/Intents

这个demo演示了intent的部分功能,type、action、createChooser.


app/launcherShortCut

这个Demo演示了如何为应用程序中的某一个Activity创建桌面快捷方式;

功能描述:当进入launcherShortCut页面后按Home键跳转至待机画面,长按屏幕后添加当前应用程序的快捷方式,之后通过该快捷方式将直接进入指定的Activity;

相关内容:
Android有两种方式为应用程序添加快捷方式,一种是长按launcher里应用程序的图标即可添加,另一种是在长按HomeScreen,弹出快捷方式添加对话框进行添加,此时显示的是所有包含“android.intent.action.CREATE_SHORTCUT”的应用。有时候我们需要为应用程序中的某个activity设置一个快捷方式,例如一个旅行出游软件同时包含了天气预报功能,正常情况下需要首先启动软件进入软件主界面,之后通过其他操作才可以查看天气情况,而用户希望平时不旅行的时候也可以查看天气,这时就可以为展示天气的Activity设置一个快捷方式,以方便查看。

实现这个快捷方式,可以分下面几步来完成:
1.为需要创建快捷方式的Activity的<intent-filter>添加<action android:name=”android.intent.action.CREATE_SHORTCUT” /> ,标识这个Activity可以支持在Home Screen上添加快捷方式。Launcher Shortcuts 是采用的是activity-alias,activity-alias为Target的别名,类似于Activity.
代码:
        <activity-alias android:name=".app.CreateShortcuts"
            android:targetActivity=".app.LauncherShortcuts"
            android:label="@string/sample_shortcuts">
            <!--  This intent-filter allows your shortcuts to be created in the launcher. -->
            <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity-alias>
2.添加相应用户添加快捷方式的代码,一般在Activity的onCreate方法中为Activity安装快捷方式:
        Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
        shortcutIntent.setClassName(this, this.getClass().getName());
        shortcutIntent.putExtra(EXTRA_KEY, "ApiDemos Provided This Shortcut");

        // Then, set up the container intent (the response to the caller)
        Intent intent = new Intent();
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcut_name));
        Parcelable iconResource = Intent.ShortcutIconResource.fromContext(
                this,  R.drawable.app_sample_code);
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
        // Now, return the result to the launcher
        setResult(RESULT_OK, intent);

原创粉丝点击