2017-08-09 203 views
0

我試圖創建應用程序快捷方式,我指的是一些文件和我使用Android的安裝快捷方式,但快捷方式圖標不顯示

Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); 
launchIntent.setAction(Intent.ACTION_MAIN); 
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplication(), MainActivity.class)); 
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "test app"); 
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(),R.mipmap.ic_launcher)); 
getApplicationContext().sendBroadcast(intent); 

我也允許添加到我的清單

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

但是,它可以在我的舊HTC手機(android 4.4)上正常工作,但不會在另一部手機上使用三星s6(android7.0)。

任何人都可以幫助我嗎?謝謝!

+0

這是你自己的應用程序?是否有一個原因,你沒有從AndroidManifest做到這一點? –

回答

0
  • 權限模型/工作流程已從API 23 - Android M或更高版本更改。
  • 您應該嘗試引入新的運行時權限模型。根據這個模型,在安裝應用程序時不提示用戶的許可,而開發人員需要在運行時

    if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.INSTALL_SHORTCUT) != PackageManager.PERMISSION_GRANTED) { 
    
    if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.INSTALL_SHORTCUT)) { 
         AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 
         builder.setTitle("Need Permission"); 
         builder.setMessage("This app needpermission."); 
         builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, int which) { 
           dialog.cancel(); 
           ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.INSTALL_SHORTCUT}, INSTALL_SHORTCUT); 
          } 
         }); 
         builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, int which) { 
           dialog.cancel(); 
          } 
         }); 
         builder.show(); 
    

檢查,並要求允許您可以使用該代碼還:〜

public static void addShortcut(Context context) 
{ 
    Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); 

    ApplicationInfo appInfo = context.getApplicationInfo(); 


    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "appInfo.name"); 
    shortcut.putExtra("duplicate", false); 


    ComponentName component = new ComponentName(appInfo.packageName, "MainActivity.class"); 
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(component)); 


    ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(context, appInfo.icon); 
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource); 

    context.sendBroadcast(shortcut); 
} 
+1

thx我想知道它是否也是權限問題,但由於快捷方式安裝的保護級別是正常的,因此無需在運行時授予我認爲的權限。我已經在whatsapp中嘗試了相同的功能,並且在創建快捷方式之前不會問我 – user6462498

0

使用方法,以減少混亂。嘗試這樣它會工作

public void createShortCut(){ 
    Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); 
    shortcutintent.putExtra("duplicate", false); 
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname)); 
    Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon); 
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); 
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), EnterActivity.class)); 
    sendBroadcast(shortcutintent); 
} 
0

你犯的錯誤是你應該給Intent.setAction(Intent.ACTION_MAIN); EXTRA_SHORTCUT_INTENT意圖。

您可以使用以下方法,這對創建或刪除Shotcuts

private void createOrRemoveShortcut(String name, boolean create) { 

    Intent shortcutIntent = new Intent(getActivity(), ShortCutActivity.class); 

    shortcutIntent.setAction(Intent.ACTION_MAIN); 

    Intent addIntent = new Intent(); 
    addIntent 
      .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name); 


    if (create) { 
     addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, 
       Intent.ShortcutIconResource.fromContext(getActivity(), 
         R.mipmap.ic_launcher)); 

     addIntent 
       .setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 

     addIntent.putExtra("duplicate", false); //may it's already there so don't duplicate 
    } else { 
     addIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT"); 
    } 


    getActivity().sendBroadcast(addIntent); 
} 

確保您ShortCutActivity在

<activity 
    android:name=".ui.activity.ShortCutActivity" 
    android:excludeFromRecents="true" 
    android:label="@string/title_activity_sample" 
    android:theme="@style/AppTheme.NoActionBar"> 
    <intent-filter> 
     <action android:name="android.intent.action.CREATE_SHORTCUT"/> 

     <category android:name="android.intent.category.DEFAULT"/> 
    </intent-filter> 
</activity> 

如果你想刪除的快捷方式,你應該有以下權限mainfest文件android.intent.action.CREATE_SHORTCUT添加它在mainfest,

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

最後我做它的工作,我不知道這下面的一個成功了,但我所改變的是這條線

new Intent(getApplication(), MainActivity.class)); 

我加上它

intent.addCategory(Intent.CATEGORY_DEFAULT); 
intent.setAction(Intent.ACTION_MAIN); 

其他類別和行動,它的工作。