1.快捷方式
Android手机上得快捷方式的意思可以以我们实际PC机器上程序的快捷方式来理解。而android中要建立某一个程序快捷方式,只需要长按桌面或者点击Menu按钮——》添加,选择“Shortcuts”或者“快捷方式”,如下图,即可添加自己程序的快捷方式。
【注意】,而通过程序中,如何来控制自己开发的程序,让其支持快捷方式呢,就要在<intent-filter>里增加对于的<Action>如:
<activity android:label="@string/app_name"android:name=".ShortcutsActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.CREATE_SHORTCUT"/> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
当然以上过程是基础只是通过上述配置,让自己Activity能够在触发增加快捷方式的这个动作时,能够找到我们程序的Activity进行相应,而程序快捷方式的名字,图标,事件等都可以在Activity里进行设置。本实例Activity代码如下:
public class ShortcutsActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //要添加的快捷方式的Intent Intent addShortcut; //判断是否要添加快捷方式 if(getIntent().getAction().equals(Intent.ACTION_CREATE_SHORTCUT)){ addShortcut = new Intent(); //设置快捷方式的名字 addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "发送邮件"); //构建快捷方式中专门的图标 Parcelable icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.mail_edit); //添加快捷方式图标 addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); //构建快捷方式执行的Intent Intent mailtoIntent = new Intent(Intent.ACTION_SENDTO,Uri.parse("mailto:jercy@163.com" )); //添加快捷方式Intent addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, mailtoIntent); setResult(RESULT_OK, addShortcut); }else{ //取消 setResult(RESULT_CANCELED); } finish(); } }
以上代码中,需要着重说明 : Parcelable icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.mail_edit);
Android中提供专门的构建快捷方式图标的方式。
main.xml文件没有变动,对于的string.xml里:
<string name="hello">创建桌面快捷方式!</string> <string name="app_name">桌面快捷方式</string>
实例效果如下:
左图:在点击添加快捷方式后,选择相关程序;
右图:点击我们的应用程序后,桌面显示快捷方式,即为上述程序设置名字与图标。
2.实时文件夹(LiveFolders)
实时文件夹,这个功能在之前的NotePad实例中已经实现,其显示的内容是实时更新的,不再你需要去单独打开相关程序,就能够查看,比如:查看邮件,联系人等。
【注意】:实时文件夹本身不存储信息,都是以映射方式查看其ContentProvider所指向的数据信息,并可以自定义显示格式,其间源数据发生变化,也可以实时更新显示内容。
而与上面快捷方式一样,同样需要配置相关Action
<activity android:label="@string/app_name"android:name=".LiveFoldersActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <action android:name= "android.intent.action.CREATE_LIVE_FOLDER" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
同样对应Activiy:
public class LiveFoldersActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //判断是否穿件实时文件夹 if(getIntent().getAction().equals(LiveFolders.ACTION_CREATE_LIVE_FOLDER)){ Intent intent = new Intent(); //设置数据地址 intent.setData(Uri.parse("content://contacts/live_folders/people")); //设置当我们点击之后的时间,这里点击一个联系人后,呼叫 intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT, new Intent(Intent.ACTION_CALL, ContactsContract.Contacts.CONTENT_URI)); //设置实时文件件的名称 intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME, "自己电话本"); //设置实时文件夹的图标 intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON, Intent.ShortcutIconResource.fromContext(this, R.drawable.contacts)); //设置显示模式为列表模式 intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE, LiveFolders.DISPLAY_MODE_LIST); setResult(RESULT_OK, intent); }else{ setResult(RESULT_CANCELED); } finish(); } }
实例效果:
左图:在点击添加文件夹后,选择相关程序;
中图:点击我们的应用程序后,桌面显示实时文件夹图标。
右图:点击“自己电话本”图标后显示对应的效果图,显示通讯录里的信息。
还有一个桌面组件Widget,这个后面进行补充吧,总结上述两个小功能,还是比较简单实用的。