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)
- Làm sao thiết kế nhiều màn hình trong Android
- Fragment và Activity trong android cơ bản
- Truyền dữ liệu giữa các Activity trong android
- Lập trình với Recyclerview trong Android – Bài 1 | dotrinh.com
- Lập trình với Recyclerview trong Android – Bài 3 | dotrinh.com
- KHÔNG CÀI ĐƯỢC APP TRONG ANDROID
- Lập trình với Recyclerview trong Android – Bài 2 | dotrinh.com
- Show Indicator trong Android | Hiển thị indicator trong Android
- Lập trình phóng to thu nhỏ ảnh pinch in – pinch out trong Android
- Chuyển một đối tượng sang Json trong Android
- Tạo seekbar và kiến thức hữu ích về seekbar trong Android
- Truyền dữ liệu giữa 2 fragment trong android
- Vòng đời Acivity trong android cơ bản
- Gửi dữ liệu đến BLESerial3 bằng Bluetooth LE Android
