Tips for Android Developer
Press Back button two time to exit and move task to background
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
boolean doubleBackToExitPressedOnce = false; /////// @Override public void onBackPressed() { if (doubleBackToExitPressedOnce) { super.onBackPressed(); return; } this.doubleBackToExitPressedOnce = true; Toast.makeText(this, "Chạm lần nữa để thoát", Toast.LENGTH_SHORT).show(); new Handler().postDelayed(new Runnable() { @Override public void run() { doubleBackToExitPressedOnce = false; } }, 2000); //2000 <-> 2 second } |
Open message, put adress and body text on it
1 2 3 4 5 6 7 8 |
private void sendSmS(String address, String sms_body) { Intent smsIntent = new Intent(Intent.ACTION_VIEW); smsIntent.setType("vnd.android-dir/mms-sms"); smsIntent.putExtra("address", address); smsIntent.putExtra("sms_body",sms_body); startActivity(smsIntent); } |
Made a USSD call
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
private void Uri ussdToCallableUri(String ussd) { String uriString = ""; if(!ussd.startsWith("tel:")) uriString += "tel:"; for(char c : ussd.toCharArray()) { if(c == '#') uriString += Uri.encode("#"); else uriString += c; } return Uri.parse(uriString); } private void callUSSD(String code) { Intent callIntent = new Intent(Intent.ACTION_CALL, ussdToCallableUri(code)); startActivity(callIntent); } //Use callUSSD("*101#"); |
Good Calendar Date Picker
1 |
https://github.com/code-troopers/android-betterpickers |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); imageButtonCalendar.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FragmentManager fm = getFragmentManager(); DateTime now = DateTime.now(); CalendarDatePickerDialogFragment calendarDatePickerDialogFragment = CalendarDatePickerDialogFragment .newInstance(NgayFragment.this, year, month - 1, day); calendarDatePickerDialogFragment.setDateRange( new MonthAdapter.CalendarDay(oldestYear, oldestMonth - 1, oldestDay), new MonthAdapter.CalendarDay(now.getYear(), now.getMonthOfYear() - 1, now.getDayOfMonth())); calendarDatePickerDialogFragment.show(fm, FRAG_TAG_DATE_PICKER); } });; } ///// @Override public void onDateSet(CalendarDatePickerDialogFragment dialog, int year, int monthOfYear, int dayOfMonth) { //your code } |
Retrieve contact name from phone number
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public static String getContactName(Context context, String phoneNumber) { ContentResolver cr = context.getContentResolver(); Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null); if (cursor == null) { return null; } String contactName = null; if(cursor.moveToFirst()) { contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)); } if(cursor != null && !cursor.isClosed()) { cursor.close(); } if(contactName == null) return phoneNumber; return contactName; } |
Updating….