안드로이드 앱 최초 실행시 자동으로 바탕화면에 아이콘(shortcut)을 생성해주는 코드다.
SharedPreferences의 "check"라는 키를 이용해
해당 키의 value가 비어있으면(isEmpty()) shortcut을 생성하고,
그 후에는 "exist"라는 value를 채워준다.
SharedPreferences는 이렇듯 간단한 ON/OFF 설정에 이용하면 좋다.
사용법도 SQLDatabase보다 간단하며
앱이 삭제되지 않는 한 계속 지속되기 때문이다.
if(pref.getString("check","").isEmpty()) 이하 코드를 다르게 해서
앱 최초 실행시 취할 액션을 마음대로 꾸며보는 것도 좋다.
//데스크탑 아이콘 생성
public void createDesktopIcon() {
SharedPreferences pref = getSharedPreferences("pref", MODE_PRIVATE);
pref.getString("check", "");
if(pref.getString("check", "").isEmpty()){
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shortcutIntent.setClassName(this, getClass().getName());
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
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,
Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher)); //앱 아이콘
intent.putExtra("duplicate", false);
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(intent);
}
SharedPreferences.Editor editor = pref.edit();
editor.putString("check", "exist");
editor.commit();
}
'MOBILE' 카테고리의 다른 글
Custom ListView (커스텀 리스트뷰) Footer를 이용한 더보기 구현 (0) | 2016.08.21 |
---|---|
뒤로가기(Back 버튼) 두번 눌러 앱 종료하기 (0) | 2016.08.21 |
파일 생성 및 저장 (0) | 2016.08.21 |
현재시간(Local Time) 가져오기 (0) | 2016.08.21 |
안드로이드 PHP GET 방식 통신에서 한글 깨짐(?) 해결 (0) | 2016.08.21 |
Android SharedPreferences 사용 예제 (0) | 2016.08.21 |
알림창 띄우기(Multi Choice, Single Choice) (0) | 2016.08.21 |
안드로이드 뒤로가기 버튼 더블클릭해서 앱 종료하기 (0) | 2016.08.21 |
댓글