Sqlite Database Updates Triggers Service for Updating via Content Observer

I am trying to use Content Observer to update a service when any changes happen to the sqlite database in my application.

I am confused what to do, so I put together the code below. Typically, content watchers are used with contacts or a media player with a background service. In my research, I read that it can be used with the sqlite database over the phone.

Questions: 1. Since the Sqlite database does not contain uri, what information do I replace People.CONTENT_URIin

this.getContentResolver().registerContentObserver (People.CONTENT_URI, true, contentObserver);

2. In my research, I did not find the code that would fall into the database class that warned ContentObserver. Does all code for the content observer work in the service class?

Please note that this question is similar to Android SQLite DB notifications and how to listen to changes in the contacts database . Both questions do not explicitly answer my question. If you have code that explains this, this will be very helpful.

Here is my semi-pusedo code below. This does not work. I use it to learn how to update a service when changing database information.

package com.example.com.test.content.observer;

import java.sql.Date;
import java.util.Calendar;
import java.util.List;

import com.google.android.gcm.demo.app.Alerts.AlarmsService;
import com.google.android.gcm.demo.app.Alerts.Alerts;
import com.google.android.gcm.demo.app.sqllite.DatabaseSqlite;

import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.provider.Contacts.People;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.database.ContentObserver;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

public class AlarmService extends Service
{

    Handler mHandler = new Handler();
    DatabaseSqlite db = new DatabaseSqlite(this);
    List<Alerts> listAlerts;
    PendingIntent pendingIntent;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.getApplicationContext()
                .getContentResolver()
                .registerContentObserver(?????, true,
                        contentObserver);
    }


    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("TAG", "started onstart command Created from Alerts service .");
        return super.onStartCommand(intent, flags, startId);// START_STICKY;
    }

    @Override
    public void onStart(final Intent intent, int startId) {
        super.onStart(intent, startId);

         runThread();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private class MyContentObserver extends ContentObserver {

        @SuppressLint("ParserError")
        public MyContentObserver(Handler mHandler) {
            super(mHandler);
        }

        @Override
        public void onChange(boolean selfChange) {

             runThread();

            super.onChange(selfChange);
        }



        public void runThread(){

            Thread thread = new Thread() {
                @Override
                public void run() {

                    Boolean x = true;
                    while (x) {

                        db.open();
                        listAlerts = db.getAlarmsForService();
                        db.close();
                        int alerts=listAlerts.size();
                        for (int i = 0; i < alerts; i++) {
                            Alerts item = listAlerts.get(i);
                            item.getRowId();
                            item.getRemoteServerId();
                        String alertInMills = item.getAlertDateInMills();
                        String alertDuration = item.getAlertDurationInMinutes();
                        String eventName = item.getEventName();


                        long longAlertInMills = Long.parseLong(alertInMills);



                         pendingIntent = PendingIntent.getService(AlarmsService.this, 0,intent, 0);

                         AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

                         Calendar calendar = Calendar.getInstance();

                        // go to data base for time in mills

                         calendar.setTimeInMillis(longAlertInMills);

                         alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                         pendingIntent);
                        //
                         System.out.println(calendar.toString());

                        }

                        //
                        System.out.println("thread");
                        x = false;

                    }

                }
            };

            thread.start();
        }

        }


    MyContentObserver contentObserver = new MyContentObserver(mHandler);

    this.getContentResolver().registerContentObserver (People.CONTENT_URI, true, contentObserver);



}
+5
source share
1 answer

, : ContentObserver, , SQLiteDatabase, . , URI, .

(1) URI, , .

public static final Uri CONTENT_URI = Uri.parse("mycontent://packagename/something");

(2) :

db (, , ) notifyChange(), , .

    rowId = db.insert(tableName, null, cv);
    ...
    getContext().getContentResolver().notifyChange(newUri, null);

(3) ContentObserver , , ( deliverSelfNotifications(), true)

public class MyService extends Service {
    private MyContentObserver mObserver;

    @Override
    public void onStartCommand(Intent intent, int flags, int startId) {
        ...
        mObserver = new MyContentObserver();
        getContentResolver().registerContentObserver(Dbfile.CONTENT_URI, null, mObserver);
    }

    @Override
    public void onDestroy() {
        ...
        if (mObserver != null) {
            getContentResolver().unregisterContentObserver(mObserver);
            mObserver = null;
        }
    }

    // define MyContentObserver here
}

(4) ContentObserver.onChange() - , .

, , URI , , URI ContentObserver.onChange(boolean, Uri) .

, !

+3

All Articles