Thêm vào file Manifest quyền được add shortcut:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Thềm method này vào MainActivity.java:
private void addShortcut() { Intent shortcutIntent = new Intent(getApplicationContext(),MainActivity.class); shortcutIntent.setAction(Intent.ACTION_MAIN); //Dùng intent để yêu cầu thêm Intent addIntent = new Intent(); addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Activity Lifecycle"); // Tên sẽ hiển thị, có thể khác tên ứng dụng addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(),R.mipmap.ic_launcher)); addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); addIntent.putExtra("duplicate", false); //Không tạo trùng shorcut getApplicationContext().sendBroadcast(addIntent); }
Trong onCreate ở lớp MainActivity thêm code:
addShortcut();
Chú ý: việc tạo shortcut như thế này không cần thiết khi đưa app lên store vì google sẽ tự động tạo cho chúng ta. Việc này nên dùng để tạo icon khi dev vì không phải mất công vào màn hình quản lý app.
Vậy là xong, các bạn run thử xem sao chú ý khi tối ưu đừng cho hàm addShortcut luôn luôn chạy mà chỉ cho chạy lần đầu thôi.
Các bài viết không xem thì tiếc:
- Siêu tổng hợp android code snippets (cập nhật thường xuyên)
- Các câu lệnh git cực cần thiết cho developer
- Cách gọi helper trong ruby on rails
- Làm việc với font trong Android
- Thay đổi tiêu đề, định dạng datepicker jQuery
- Mẹo tìm kiếm cực hay trong Sublime
- Cách tạo Sticky Sidebar với jQuery
- Làm tròn số phút lên 5 trong java
- Cách dump data trong Ruby
- Failed to open file error 22 mysql
- Select where like trong Ruby On Rails
- Tại sao đã thêm vào file gitignore rồi mà không có tác dụng?
- Giải phương trình bậc 2 bằng Ruby