Android – Create new Contact Programmatically

Step 1:

Add permission in Manifest.xml

<uses-permission android:name="android.permission.WRITE_CONTACTS" />
Step 2:
In java code

public void WritePhoneContact(String displayName, String number, Context cntx /*App or Activity Ctx*/) {
    Context contetx = cntx; //Application's context or Activity's context
    String strDisplayName = displayName; // Name of the Person to add
    String strNumber = number; //number of the person to add with the Contact
    System.out.println("NAME ====> " + strDisplayName + "    NUMBER ====>  " + strNumber);
    ArrayList<ContentProviderOperation> cntProOper = new ArrayList<ContentProviderOperation>();
    int contactIndex = cntProOper.size();//ContactSize

    //Newly Inserted contact
    // A raw contact will be inserted ContactsContract.RawContacts table in contacts database.
    cntProOper.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)//Step1
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null).build());

    //Display name will be inserted in ContactsContract.Data table
    cntProOper.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)//Step2
            .withValueBackReference(ContactsContract.RawContacts.Data.RAW_CONTACT_ID, contactIndex)
            .withValue(ContactsContract.RawContacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, strDisplayName) // Name of the contact
            .build());
    //Mobile number will be inserted in ContactsContract.Data table
    cntProOper.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)//Step 3
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, contactIndex)
            .withValue(ContactsContract.RawContacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, strNumber) // Number to be added
            .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE).build()); //Type like HOME, MOBILE etc
    try {
        // We will do batch operation to insert all above data
        //Contains the output of the app of a ContentProviderOperation.
        //It is sure to have exactly one of uri or count set
        ContentProviderResult[] contentProresult = null;
        contentProresult = contetx.getContentResolver().applyBatch(ContactsContract.AUTHORITY, cntProOper); //apply above data insertion into contacts list
    } catch (RemoteException exp) {
        //logs;
    } catch (OperationApplicationException exp) {
        //logs
    }
}
Advertisements

My Nexus device won’t charge or turn on

First, do these quick checks:

Method 1:

  • Make sure the power outlet your Nexus device is plugged into is working properly.
  • Make sure you’re using the power adapter and USB cable that came with your Nexus device. Check to make sure that this charger is working correctly by trying it with another device.
  • Make sure there isn’t any dust or lint in the power port. The inside of the port should look like this:
  • Make sure the power cord is securely connected to the device and the power adapter.

After you’ve plugged your device in:

  1. Check to see if a battery icon appears on the screen after 1 minute.
  2. If the battery icon appears, press and hold the Power button for 15 to 30 seconds to see if your device turns on.

Method : 2:

  1. Unplug the USB cable from the power adapter and use it to connect your Nexus device to a computer’s USB port while the computer is on and connected to a power source.
  2. After 10-15 minutes, disconnect the USB cable from your Nexus device and reconnect it within 10 seconds.
  3. After 1 minute, check to see if a battery icon appears on the screen.
  4. Press & hold the Power button for at least 15 seconds to see if your device turns on.

Get Configured Email Address From Android Device Programmatically

In java code need the following piece of code.

String gmail = null;

    Pattern gmailPattern = Patterns.EMAIL_ADDRESS; // API level 8+

    Account[] accounts = AccountManager.get(this).getAccounts();

    for (Account account : accounts) {

        if (gmailPattern.matcher(account.name).matches()) {

            gmail = account.name;

        }

    }

    Log.d(“gmail”, gmail+“**”);

And also need permission in AndroidManifest.xml

<uses-permission android:name=“android.permission.GET_ACCOUNTS” />

Get Configured Email Address From Android Device Programmatically

In java code need the following piece of code.

String gmail = null;

    Pattern gmailPattern = Patterns.EMAIL_ADDRESS; // API level 8+

    Account[] accounts = AccountManager.get(this).getAccounts();

    for (Account account : accounts) {

        if (gmailPattern.matcher(account.name).matches()) {

            gmail = account.name;

        }

    }

    Log.d(“gmail”, gmail+“**”);

And also need permission in AndroidManifest.xml

<uses-permission android:name=“android.permission.GET_ACCOUNTS” />

Hide and Unhide Application in HomeScreen Android

For Hide application icon:

PackageManager p = getPackageManager();
ComponentName componentName = new ComponentName(this, com.apps.MainActivity.class); // activity which is first time open in manifiest file which is declare as <category android:name="android.intent.category.LAUNCHER" />
p.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

For Unhide Application:

PackageManager p = getPackageManager();
ComponentName componentName = new ComponentName(this, com.apps.MainActivity.class);
p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

Known Issue:

After hide application your Activity will destroyed. So trying to start my main activity but its giving me ActivityNotFoundException.

Solution:

After hide you MainActivity once you hide it the activity will not be found it’s destroy so you need to create same another activity like mainActivity2 and you need to store boolean value to sharerdprefrence that whether icon is hiddne then u need to open mainActivity2 else MainActivity

Find the date is Current date or not – Android

SimpleDateFormat sdf = new SimpleDateFormat(

“dd-MM-yyyy HH:mm:ss”);

Date date = sdf.parse(date_string);

Calendar calendar = Calendar.getInstance();

calendar.setTime(date);

calendar.set(calendar.get(calendar.YEAR), calendar.get(calendar.MONTH),

calendar.get(calendar.DATE), calendar.get(calendar.HOUR),

calendar.get(calendar.MINUTE), calendar.get(calendar.SECOND));

long startTime = calendar.getTimeInMillis();

Boolean isCurrnetDate =  DateUtils.isToday(startTime);