2013-10-08 73 views
1

Android手機主屏幕上的應用程序快捷方式我已經創建了一個Android應用程序,它工作正常 ,但現在我想添加一個功能,在安裝應用程序應創建主屏幕上的快捷方式如何創建上安裝

請建議 我不想完整的代碼工作 簡單的步驟將是您的應用程序使用在AndroidManifest.xml中INSTALL_SHORTCUT權限不夠

+0

http://codinggeekorg.wordpress.com/2011/01/02/android-how-添加主屏幕快捷方式到您的應用程序# –

回答

3

首先聲明。

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> 



    Intent shortcutIntent = new Intent(); 
    shortcutIntent.setClassName("com.example.androidapp", "SampleIntent"); 
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    shortcutIntent.putExtra("someParameter", "HelloWorld"); 

    Intent addIntent = new Intent(); 
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Name"); 
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.icon)); 

    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
    context.sendBroadcast(addIntent); 
+0

這個鱈魚部分去在發射器活動? –

2

在您的清單..

<activity android:name=".ShortCutActivity" android:label="@string/shortcut_label"> 
    <intent-filter> 
    <action android:name="android.intent.action.CREATE_SHORTCUT" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 
</activity> 

然後在接收意向的活動,您的快捷方式創建一個意圖,並返回它作爲活動的結果。

// create shortcut if requested 
ShortcutIconResource icon = 
    Intent.ShortcutIconResource.fromContext(this, R.drawable.icon); 

Intent intent = new Intent(); 

Intent launchIntent = new Intent(this,ActivityToLaunch.class); 

intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent); 
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, someNickname()); 
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); 

setResult(RESULT_OK, intent); 
+0

爲什麼我們需要Java代碼?沒人解釋! – Zingam

1

測試試試:

Intent shortcutIntent = new Intent(Intent.ACTION_MAIN); shortcutIntent.setClassName(this, this.getClass().getName()); 

Intent intent = new Intent(); 
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "hello"); 
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon); 
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource); 

intent.setAction(Intent.ACTION_CREATE_SHORTCUT); 
getApplicationContext().sendBroadcast(intent); 

好運...