Search code examples
androidandroid-serviceandroid-contentprovidercalllog

how to create a service to interact with a content provider?


    String strOrder = android.provider.CallLog.Calls.DATE + " DESC";

    Cursor mCallCursor = getContentResolver().query(
            CallLog.Calls.CONTENT_URI, null, CallLog.Calls.TYPE + "=?",
            new String[] { String.valueOf(CallLog.Calls.MISSED_TYPE) },
            strOrder);

    // get start of cursor
    if (mCallCursor.moveToFirst()) {

        // loop through cursor
        do {

            mCall = mCallCursor.getString(mCallCursor
                    .getColumnIndex(CallLog.Calls.NUMBER));

            Toast.makeText(getBaseContext(), mCall + " ",
                    Toast.LENGTH_SHORT).show();

        } while (mCallCursor.moveToNext());

    }

My app needs to run in the background using a service, but I don't know how to accomplish that. I created a class to access the call logs content provider, specifically the missed calls, so that when a certain number failed to reach the user for 3 consecutive tries, it will set the ringer on if the phone is silent.


Solution

  • Solved! I may have confused you with my question. what I did was I created a Started service class. override onStartCommand() in myService class after calling startService(intent) from the MainActivity class and create a new class that extends ContentObserver then overrride onChange() method in my ContentObserver class.