Añadir acceso directo a la aplicación de Android En la pantalla principal, haga clic en el botón

Quiero que sea fácil agregar mi aplicación a la pantalla de inicio presionando un botón. Así que lo que estoy pensando es un botón en la parte inferior de mi aplicación que dice "Añadir a la pantalla de inicio" y cuando se presiona, agrega el acceso directo a la pantalla de inicio sin cerrar la aplicación. ¿Qué código debo agregar para hacer eso?

Envíe una instalación INSTALL_SHORTCUT con el Intent resultante como extra (en este caso, el resultado Intent está abriendo una actividad directamente).

//where this is a context (eg your current activity) final Intent shortcutIntent = new Intent(this, SomeActivity.class); final Intent intent = new Intent(); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); // Sets the custom shortcut's title intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); // Set the custom shortcut icon intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon)); // add the shortcut intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); sendBroadcast(intent); 

También necesita este permiso en su manifiesto:

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

El primer paso, usted debe hacer el luncher podría recibir una difusión:

  <!-- Intent received used to install shortcuts from other applications --> <receiver android:name="com.android.launcher2.InstallShortcutReceiver" android:permission="com.android.launcher.permission.INSTALL_SHORTCUT"> <intent-filter> <action android:name="com.android.launcher.action.INSTALL_SHORTCUT"/> </intent-filter> </receiver> 

A continuación, agregue un permiso en manifest.xml

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

Finalmente, cree una función y llámela cuando haga clic en el botón:

 public void createShortCut(){ // a Intent to create a shortCut Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); //repeat to create is forbidden shortcutintent.putExtra("duplicate", false); //set the name of shortCut shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname)); //set icon Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon); shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); //set the application to lunch when you click the icon shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext() , EnterActivity.class)); //sendBroadcast,done sendBroadcast(shortcutintent); } 

Hazlo asi:

 button.setOnClickListener(new OnClickListener() { public void onClick(View v) { createShortCut(); } }); 
  • Captura continua de la pantalla de Android para su procesamiento en una aplicación
  • Tamaños de imagen de Android
  • Cómo agregar iconos a Preferencias
  • Android VideoView SplashScreen
  • ¿Reduce Android la velocidad de descarga cuando se apaga la pantalla?
  • Aplicación para Android: admite todos los tamaños de pantalla
  • Pantalla blanca después de Splash en android - ionic 2
  • Android: Manejo eficiente de la rotación de la pantalla
  • Convertir imágenes (mapa de bits) a un formato de vídeo popular en Android
  • Archivo en blanco se guarda cuando se utiliza screencap programatically en Android
  • Cómo controla la orientación de la pantalla del dispositivo Android
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.