Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – Sqlite数据库更新触发服务通过内容观察器进行更新大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在尝试使用内容观察器在我的应用程序中的sqlite数据库发生任何更改时更新服务.

我很困惑该怎么做,所以我把下面的代码放在一起.通常,内容观察者与联系人或媒体播放器一起使用后台服务.在我的研究中,我读到它可以与手机上的sqlite数据库一起使用.

问题:
1.由于sqlite数据库没有uri,我将用什么信息替换People.CONTENT_URI

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

2.在我的研究中,我没有发现任何会进入数据库类的代码会提醒ContentObserver. Content Observer的所有代码都在服务类中工作吗?

请注意,此问题类似于Android SQLite DB notifications
how to listen for changes in Contact Database
这两个问题都没有明确回答我的问题.如果您有解释此问题的代码,那将非常有帮助.

这是我下面的半pusedo代码.这是行不通的.我正在使用它来了解如何在数据库信息更改时更新服务.

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;="" 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,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,contentobserver);="" }="">
最佳答案
一般来说,处理这个问题有两个部分:正如您所指出的那样,您需要注册以接收更改的ContentObserver,以及必须通知注册观察者任何更改的SQLiteDatabase.如果这是您拥有的数据库,则可以创建可用于侦听的URI.

(1)首先定义URI,通常在数据库定义文件中.

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

(2)为您的数据库Content Provider:
每个db函数(插入,更新,删除)都应在完成操作后调用notifyChange(),以通知观察者发生了更改.

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

(3)在服务中创建并注册ContentObserver,如same link you provided above所述(记得覆盖deliverSelfNotifications()以返回truE)

public class Myservice extends service {
    private MyContentObserver mObserver;

    @Override
    public void onStartCommand(Intent intent,int startId) {
        ...
        mObserver = new MyContentObserver();
        getContentResolver().registerContentObserver(Dbfile.CONTENT_URI,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).

希望这可以帮助!

大佬总结

以上是大佬教程为你收集整理的android – Sqlite数据库更新触发服务通过内容观察器进行更新全部内容,希望文章能够帮你解决android – Sqlite数据库更新触发服务通过内容观察器进行更新所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。