개발/개발 자료

(Android) 바탕화면 바로가기 만들기

시원한물냉 2013. 12. 11. 09:45

private void addShortcut(Context context) {

Intent shortcutIntent = new Intent();

shortcutIntent.setAction(Intent.ACTION_MAIN);

shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);

shortcutIntent.setClassName(context, getClass().getName());

shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);

 

Parcelable iconResource = Intent.ShortcutIconResource.fromContext( this,  R.drawable.ic_launcher);

 

Intent intent = new Intent();

intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);

intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getResources().getString(R.string.app_name));

intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,iconResource);

intent.putExtra("duplicate", false);

intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");       

sendBroadcast(intent);  

 } 


그리고 퍼미션 추가

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



필요한곳에 위 메서드를 만들고 호출해주면 된다.

본인은 로딩화면에서 처리해주었다.


실행할때마다 바로가기를 만드는데 최초 1회만 실행시켜주는 처리를 해주어야 할것같다.

프리퍼런스나 데이터값 저장을 해야할것같다.