2014-07-24 103 views
0

我创建我的应用程序的主屏幕上创建快捷方式时,应用程序启动,它是在所有设备多个主屏幕快捷方式在Nexus 7上标签

但是,的Nexus标签就可以对每一个应用程序启动创建多个快捷方式工作正常?

如何在创建应用程序时将快捷方式创建一次,并在应用程序上卸载该应用程序?

private void AddShortcutIconToHomeScreen() { 
    Intent shortcutIntent = new Intent(this, EmployeeListActivity.class); 
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    Intent addIntent = new Intent(); 
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Device Tracker"); 
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, 
     Intent.ShortcutIconResource.fromContext(getApplicationContext(), 
      R.drawable.ic_launcher)); 
    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
    addIntent.putExtra("duplicate", false); 
    getApplicationContext().sendBroadcast(addIntent); 
    } 

回答

0

如果调用你的方法AddShortcutIconToHomeScreen()之前存在使用方法removeShortcut()您可以删除快捷方式:

方法:

 private void removeShortcut() { 

     Intent shortcutIntent = new Intent(this, EmployeeListActivity.class); 
     shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 


     Intent addIntent = new Intent(); 
     addIntent 
       .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 
     addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Device Tracker"); 

     addIntent 
       .setAction("com.android.launcher.action.UNINSTALL_SHORTCUT"); 
     getApplicationContext().sendBroadcast(addIntent); 
    } 

和清单文件中,添加的权限,

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

谢谢Mehul。基本上,它会创建或删除应用程序第一次拉扯时的快捷方式。如何在App安装或删除时调用此方法? –

+0

使用removeShortcut()方法我也可以在Nexus 7选项卡上看到多个图标,它可以在所有其他设备上使用removeShortcut()方法完美实现 –

+1

如果您只想为首次启动应用程序执行某些操作,那么您需要为了保持'SharedPreference'中的轨道,所以它不会在下次启动时执行相同的操作。并且关于卸载的确认,android没有这样的功能。 –

0

解决使用SharedPreference

private void AddShortcutIconToHomeScreen() { 
    boolean mboolean = false; 

    SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0); 
    mboolean = settings.getBoolean("FIRST_RUN", false); 
    if (!mboolean) { 
     // do the thing for the first time 
     settings = getSharedPreferences("PREFS_NAME", 0); 
     SharedPreferences.Editor editor = settings.edit(); 
     editor.putBoolean("FIRST_RUN", true); 
     editor.commit(); 
     Intent shortcutIntent = new Intent(this, EmployeeListActivity.class); 
     shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

     Intent addIntent = new Intent(); 
     addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 
     addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Device Tracker"); 
     addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, 
      Intent.ShortcutIconResource.fromContext(getApplicationContext(), 
       R.drawable.ic_launcher)); 
     addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
     addIntent.putExtra("duplicate", false); 
     getApplicationContext().sendBroadcast(addIntent); 
    } else { 
     // other time your app loads 
    } 
    } 

新增的AndroidManifest.xml权限我的问题。

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> 
    <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /> 
相关问题