2014-02-20 80 views
2

我找到了代碼,在主屏幕上創建一個快捷方式的作品,但當我在主屏幕上點擊它說:應用程序沒有安裝。一些幫助?主屏快捷方式應用程序沒有安裝

我的代碼:

 Intent shortcutIntent; 
    shortcutIntent = new Intent(); 
    shortcutIntent.setComponent(new ComponentName(
      getApplicationContext().getPackageName(), ".GetQuoteActivity")); 

    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    final Intent putShortCutIntent = new Intent(); 
    putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, 
      shortcutIntent); 

    // Sets the custom shortcut's title 
    putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Get Quote"); 
    putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(
      GetQuoteActivity.this, R.drawable.ic_launcher)); 
    putShortCutIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
    sendBroadcast(putShortCutIntent); 
} 

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.files.getquote" 
     android:versionCode="1" 
     android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="7" /> 

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

<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > 
     <activity 
      android:name=".GetQuoteActivity" 
      android:theme="@android:style/Theme.NoTitleBar" 
      > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 
</manifest 

回答

2

兩個可能性:

  1. 改成這樣:

    shortcutIntent.setComponent(new ComponentName(
        getApplicationContext().getPackageName(), GetQuoteActivity.class)); 
    
  2. 使用此代碼來創建快捷方式:

    Intent shortcutIntent = new Intent (this, YourActivity.class);  
    Intent addIntent = new Intent(); 
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Title"); 
    addIntent.putExtra("duplicate", false); 
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon)); 
    
    sendBroadcast(addIntent); 
    
+0

絕對完美的我想給你了,但沒有足夠的需要。所以只能標記爲完成。 :) –

+0

還有1個問題,當我點擊應用程序更多的時間它總是顯示快捷方式應用程序名稱創建它可能只顯示1次? –

+1

應用程序未安裝,如何解決? –

相關問題