Android開發之向案頭添加捷徑

來源:互聯網
上載者:User

標籤:向案頭添加捷徑   為應用程式添加捷徑   

Android開發之向案頭添加捷徑

對於一個希望擁有更多使用者的應用來說,使用者案頭可以說是所有軟體的必爭之地,如果使用者在手機案頭上建立了該軟體的捷徑,使用者將會更頻繁地使用該軟體。因此,所有 Android程式都應該允許使用者把軟體的捷徑添加到案頭上。

在程式中把一個軟體的捷徑添加到案頭上,只需要如下三步即可:

1. 建立一個添加捷徑的Intent該Intent的Action屬性值應該為com.android.launcher.action.INSTALLSHORTCUT,。

2. 通過為該Intent加Extra屬性來設定捷徑的標題、表徵圖及捷徑對應啟動的程式。

3. 調用sendBroadcast()方法發送廣播即可添加捷徑。

執行個體代碼:

/** * 向案頭添加捷徑 * @author jph * Date:2014.09.05 */public class AddShortcut extends Activity {Button btnAddShortCut;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.mian);btnAddShortCut=(Button)findViewById(R.id.btnAddShortCut);btnAddShortCut.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub//建立一個添加捷徑的IntentIntent addSC=new Intent("com.android.launcher.action.INSTALL_SHORTCUT");//快速鍵的標題String title=getResources().getString(R.string.shotcut_title);//快速鍵的表徵圖Parcelable icon=Intent.ShortcutIconResource.fromContext(AddShortcut.this, R.drawable.ic_launcher);//建立單擊快速鍵啟動本程式的IntentIntent launcherIntent=new Intent(AddShortcut.this, AddShortcut.class);//設定快速鍵的標題addSC.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);//設定快速鍵的表徵圖addSC.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);//設定單擊此快速鍵啟動的程式addSC.putExtra(Intent.EXTRA_SHORTCUT_INTENT,launcherIntent);//向系統發送添加快速鍵的廣播sendBroadcast(addSC);}});}}

最後為應用程式建立快速鍵添加許可權:

<!-- 指定添加安裝捷徑的許可權 --><uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

程式運行:



Android開發之向案頭添加捷徑

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.