Aktualizacje bazy danych Sqlite uruchamiają aktualizację usługi za pośrednictwem obserwatora treści

Próbuję użyć Obserwatora treści do zaktualizowania usługi, gdy zmiany w bazie danych sqlite w mojej aplikacji.

Jestem zdezorientowany, co robić, więc poniżej zestawiłem kod. Zwykle obserwatorzy treści używają kontaktów lub mediaplayer z usługą w tle. W moich badaniach przeczytałem, że można go używać z bazą danych sqlite w telefonie.

Pytania: 1. Ponieważ baza danych Sqlite nie ma uri, jakie informacje należy wymienićPeople.CONTENT_URI w

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

2. W moich badaniach nie znalazłem żadnego kodu, który trafiłby do klasy bazy danych, która ostrzegałaby ContentObserver. Czy cały kod dla Content Observer działa w klasie usług?

Zauważ, że to pytanie jest podobne doPowiadomienia Android SQLite DB ijak nasłuchiwać zmian w bazie danych kontaktów Oba pytania nie odpowiadają jednoznacznie na moje pytanie. Jeśli masz kod, który to wyjaśnia, byłoby to bardzo pomocne.

Oto mój kod semi-puseo poniżej. To nie działa. Używam go, aby dowiedzieć się, jak zaktualizować usługę, gdy zmienia się informacja o bazie danych.

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);



}

questionAnswers(1)

yourAnswerToTheQuestion