Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's an old question, but still active as there is no explicit Android feature. And the guys from facebook found a work around - somehow. Today, I found a way that works for me. Not perfect (see remarks at the end of this answer) but it works!</p> <p>Main idea is, that I update the icon of my app's shortcut, created by the launcher on my home screen. When I want to change something on the shortcut-icon, I remove it first and recreate it with a new bitmap.</p> <p>Here is the code. It has a button <code>increment</code>. When pressed, the shortcut is replaced with one that has a new counting number.</p> <p>First you need these two permissions in your manifest:</p> <pre><code>&lt;uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /&gt; &lt;uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /&gt; </code></pre> <p>Then you need this two methods for installing and uninstalling shortcuts. The <code>shortcutAdd</code> method creates a bitmap with a number in it. This is just to demonstrate that it actually changes. You probably want to change that part with something, you want in your app.</p> <pre><code>private void shortcutAdd(String name, int number) { // Intent to be send, when shortcut is pressed by user ("launched") Intent shortcutIntent = new Intent(getApplicationContext(), Play.class); shortcutIntent.setAction(Constants.ACTION_PLAY); // Create bitmap with number in it -&gt; very default. You probably want to give it a more stylish look Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); Paint paint = new Paint(); paint.setColor(0xFF808080); // gray paint.setTextAlign(Paint.Align.CENTER); paint.setTextSize(50); new Canvas(bitmap).drawText(""+number, 50, 50, paint); ((ImageView) findViewById(R.id.icon)).setImageBitmap(bitmap); // Decorate the shortcut Intent addIntent = new Intent(); addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name); addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap); // Inform launcher to create shortcut addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); getApplicationContext().sendBroadcast(addIntent); } private void shortcutDel(String name) { // Intent to be send, when shortcut is pressed by user ("launched") Intent shortcutIntent = new Intent(getApplicationContext(), Play.class); shortcutIntent.setAction(Constants.ACTION_PLAY); // Decorate the shortcut Intent delIntent = new Intent(); delIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); delIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name); // Inform launcher to remove shortcut delIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT"); getApplicationContext().sendBroadcast(delIntent); } </code></pre> <p>And finally, here are two listener to add the first shortcut and update the shortcut with an incrementing counter.</p> <pre><code>@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test); findViewById(R.id.add).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { shortcutAdd("changeIt!", count); } }); findViewById(R.id.increment).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { shortcutDel("changeIt!"); count++; shortcutAdd("changeIt!", count); } }); } </code></pre> <p>Remarks:</p> <ul> <li><p>This way works also if your App controls more shortcuts on the home screen, e.g. with different extra's in the <code>Intent</code>. They just need different names so that the right one is uninstalled and reinstalled.</p></li> <li><p>The programmatical handling of shortcuts in Android is a well known, widely used but not officially supported Android feature. It seems to work on the default launcher and I never tried it anywhere else. So dont blame me, when you get this user-emails "It does not work on my XYZ, double rooted, super blasted phone"</p></li> <li><p>The launcher writes a <code>Toast</code> when a shortcut was installad and one when a shortcut was uninstalled. So I get two <code>Toast</code>s every time I change the icon. This is not perfect, but well, as long as the rest of my app is perfect...</p></li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload