歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android桌面快捷方式

Android桌面快捷方式

日期:2017/3/1 10:23:11   编辑:Linux編程

Android中的桌面快捷方式和PC機上的快捷方式一樣,用於啟動某一應用程序。要在桌面添加一個快捷方式非常簡單,只需長按桌面或者點擊"Menu"按鈕,然後在彈出的選項中選擇shortcut,然後選擇要添加的快捷方式即可。

下面主要介紹如何通過代碼將一個應用程序添加到桌面快捷方式。

首先在描述文件AndroidManifest.xml中注冊一個action為:<action android:name="android.intent.action.CREATE_SHORTCUT"/>

如下所示:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.test.shortcut"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <application android:icon="@drawable/icon" android:label="@string/app_name">
  7. <activity android:name=".MainActivity"
  8. android:label="@string/app_name">
  9. <intent-filter>
  10. <action android:name="android.intent.action.MAIN" />
  11. <category android:name="android.intent.category.LAUNCHER" />
  12. <action android:name="android.intent.action.CREATE_SHORTCUT"/>
  13. </intent-filter>
  14. </activity>
  15. </application>
  16. </manifest>
接下來是MainActivity:
  1. public class MainActivity extends Activity {
  2. /** Called when the activity is first created. */
  3. @Override
  4. public void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.main);
  7. Intent intent;
  8. //判斷是否要添加快捷方式
  9. if (this.getIntent().getAction().equals(Intent.ACTION_CREATE_SHORTCUT)){
  10. intent = new Intent();
  11. //設置快捷方式名稱
  12. intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "撥打電話");
  13. //設置快捷方式圖標
  14. Parcelable icon = Intent.ShortcutIconResource.fromContext(this, android.R.drawable.stat_sys_phone_call);
  15. intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
  16. //設置快捷方式執行的intent
  17. Uri uri = Uri.parse("tel:055555");
  18. Intent it = new Intent(Intent.ACTION_DIAL, uri);
  19. intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, it);
  20. setResult(RESULT_OK,intent);
  21. }else {
  22. //取消
  23. setResult(RESULT_CANCELED);;
  24. }
  25. this.finish();
  26. }
  27. }

代碼非常簡單,運行程序,長按桌面,選擇shortcut後如圖所示:


選擇將ShortCut添加到桌面,效果如圖:


單擊“撥打電話”圖標,出現如圖所示結果:


更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved