2012-03-15 70 views
0

我希望用戶能夠在菜單中看到我的應用程序,直到他配置好之後,我想刪除快捷方式,同時我想在用戶桌面上放置一個快捷方式。我很漂亮新來的java菜單中的自動刪除快捷方式

這有一種方法,我可以實現這個嗎?

+0

在什麼菜單?你可以說得更詳細點嗎? – Calvin 2012-03-15 14:10:48

+0

@Calvin在應用程序抽屜裏 – opc0de 2012-03-15 14:17:14

+0

Launcher是正確的術語。 :)如果是這樣的話,我不知道,因爲每個安裝的應用程序應該出現在那裏。但作爲替代方案,您可以實現IF(配置文件未找到),然後顯示設置頁面Else顯示您的主要活動。 – Calvin 2012-03-15 14:26:31

回答

1

我不知道有什麼辦法可以讓它出現在應用程序抽屜中,但稍後再刪除它。

要在主屏幕上創建快捷方式,可以使用以下方法。

請注意 -我相信這裏播出的意圖以及您在清單中必須申請的權限不是公共API的一部分。此方法不適用於每個主屏幕實現。此方法可能會隨時停止工作(並且可能無法在較新版本的Android上工作,我只在Nexus S上進行測試,但似乎在該設備上工作)。

你已經被警告

public boolean setShortCut(Context context, String appName) 
{ 
    System.out.println("in the shortcutapp on create method "); 
    boolean flag =false ; 
    int app_id=-1; 
    PackageManager p = context.getPackageManager(); 
    Intent i = new Intent(Intent.ACTION_MAIN); 
    i.addCategory(Intent.CATEGORY_LAUNCHER); 
    List<ResolveInfo> res =p.queryIntentActivities(i,0); 
    System.out.println("the res size is: "+res.size()); 

    for(int k=0;k<res.size();k++) 
    { 
     System.out.println("the application name is: "+res.get(k).activityInfo.loadLabel(p)); 
     if(res.get(k).activityInfo.loadLabel(p).toString().equals(appName)){ 
      flag = true; 
      app_id = k; 
      break; 
     } 
    } 

    if(flag){ 
     ActivityInfo ai = res.get(app_id).activityInfo; 

     Intent shortcutIntent = new Intent(); 
     shortcutIntent.setClassName(ai.packageName, ai.name); 
     shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     Intent intent = new Intent(); 
     intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 

     intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "SIZETESTDYNAMIC"); 

     intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.icon)); 
     //intent.addCategory(Intent.CATEGORY_DEFAULT); 
     intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
     context.sendBroadcast(intent); 
     System.out.println("in the shortcutapp on create method completed"); 
    } 
    else 
     System.out.println("appllicaton not found"); 
    return true; 
} 

,你就會有這個權限添加到您的清單:

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