標籤:向案頭添加捷徑 為應用程式添加捷徑
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開發之向案頭添加捷徑