【问题标题】:Trying Back ground Service is Fetching Data From Sqlite Database Send to Web, Same Time Listview Showing Data From Sqlite Database Not Working尝试后台服务正在从 Sqlite 数据库中获取数据发送到 Web,同时 Listview 显示来自 Sqlite 数据库的数据不工作
【发布时间】:2015-05-16 07:00:48
【问题描述】:

帮我解决一下,

每 10 分钟使用后台服务将 Sqlite 数据库数据同步到 Web 服务,并行 Listview 从 sqlite 数据库中获取相同的表数据。

每当服务从本地数据库获取数据到网络服务时,Listview 不显示数据。

OnClick listview item 的全部数据也将显示在下一页,从本地数据库中获取,其显示空白页。

后台服务完成后,在Listview和下一个视图页面中显示数据。

列表视图页面--->在项目视图页面上。

iam 使用 Cursor 适配器从本地数据库获取数据到列表视图。

this link i have referred

【问题讨论】:

  • 到目前为止你尝试过什么?向我们展示您编写的代码,提出具体问题。
  • 只有我用过的简单游标适配器,正在获取 Listview 数据。 Onclick Listview 项目在下一页中显示整行。那个时候后台服务正在运行(同步本地数据库到web),下一页没有显示整行数据。
  • 服务运行完成后,完美显示Listview,同时点击Listview Item整行数据在下一页。
  • 威士忌探戈狐步舞

标签: android sqlite


【解决方案1】:

亲爱的:使用定期服务。我有同样的问题,我像这样解决了。就我而言,这是代码。

服务代码:

public class PeriodicService  extends Service {
    private static final long TIMEREQ = 10000 * 60;// 10 mints

    public static String LOG = "Log";

    private final Context mContext;

    public UpdateServices(Context context) {
        this.mContext = context;
    }

    public UpdateServices() {
        super();
        mContext = UpdateServices.this;
    }

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
        Log.i(LOG, "Service started");
        // save in sqlite after 10 mints
        DatabaseManger db = new DatabaseManger(mContext);
        db.open();
        db.insert("anything", "data");
        db.close();

        // upload data to somewhere server
        new CreateNewProduct(this).execute();

        return START_NOT_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Intent ishintent = new Intent(this, UpdateServices.class);
        PendingIntent pintent = PendingIntent.getService(this, 0, ishintent, 0);
        AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarm.cancel(pintent);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
                TIMEREQ, pintent);
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Intent ishintent = new Intent(this, UpdateServices.class);
        PendingIntent pintent = PendingIntent.getService(this, 0, ishintent, 0);
        AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        manager.cancel(pintent);
        Toast.makeText(this, "service stoped", Toast.LENGTH_SHORT).show();
    }
}

你可以得到帮助from here 启动和停止您的服务

调用启动服务。根据服务的生命周期,在服务停止时调用destroy方法。根据您的选择,在服务延迟一段时间的情况下,停止服务后不使用挂起意图。

来自 Activity 调用

   btn_startService.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startService(new Intent(About.this, UpdateServices.class));
        }
    });

   btn_stopService.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            stopService(new Intent(About.this, UpdateServices.class));

            }
        });

用于列表视图刷新您的列表适配器。

【讨论】:

  • 谢谢。停止服务后的一个疑问,它会在 10 分钟后再次运行
  • 检查我的编辑答案我解决了你想要的问题...... :) 如果它有效,那么接受我的答案
  • 我每 10 分钟给出一次未决意图,如果服务停止,它将自动重新启动或不启动
  • 我已经在 onCreate() 和 onDestory() 中定义了一个pendingintent。检查我编辑的代码...
猜你喜欢
  • 2012-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-03
  • 1970-01-01
  • 2016-07-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多