September 2014

Sunday, September 28, 2014

Handling Looper.prepare() in android.....


This article shows how to handle Looper.prepare() in android.

Show Toast inside Thread by using UiThread.


new Thread() {
        @Override
        public void run() {
               try {
                     // code runs in a thread
                     runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                dialog.dismiss();
                            }
                     });
               } catch (final Exception ex) {
                   Log.i("---","Exception in thread");
               }
        }
 }.start();
This will avoid Looper.prepare() in android.

Wednesday, September 3, 2014

Installing & Uninstalling Home Screen Shortcut.....


This Article shows to create installing & uninstalling home screen shortcut.

1- Create method for adding shortcut to home screen(addShortcut()).

private void addShortcut() {
Intent shortcut = new Intent(this, MainActivity.class);
shortcut.setAction(Intent.ACTION_MAIN);
Intent add = new Intent();
add.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcut);
add.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Your Activity Name");
add.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher));
add.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(add);
    }

2- Create method for removing shortcut from home screen(removeShortcut()).

private void removeShortcut() {
Intent shortcut = new Intent(this, MainActivity.class);
shortcut.setAction(Intent.ACTION_MAIN);
Intent add = new Intent();
add.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcut);
add.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Your Activity Name");
add.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
sendBroadcast(add);
    }

3- Add following permissions to Manifest.xml.

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