2017-08-09 204 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); 

其他类别和行动,它的工作。