AsyncTask 可以在服务内运行并写入数据库吗?

发布于 2024-11-16 02:18:24 字数 2301 浏览 1 评论 0原文

public class IdService extends Service {
    private UploadFilesTask task;
    DBAdapter dbs;

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

    @Override
    public void onCreate() {
        super.onCreate();               
    }

    @Override
    public void onStart(Intent intent, int startId) {   
        super.onStart(intent, startId); 
        if(Networking.isNetworkAvailable(this)){
            if ( intent.hasExtra("start") && taskIsNotRunning() ) {
                task = new UploadFilesTask();
                task.execute();             
            } 
        }
        else{
            if ( intent.hasExtra("start") && taskIsNotRunning() ) {
                Calendar cal = Calendar.getInstance();
                cal.add(Calendar.MINUTE, 5);
                String start = "start";
                Intent i = new Intent(IdService.this, IdService.class);
                i.putExtra("start", start);
                PendingIntent sender = PendingIntent.getService(this, 192837, i, PendingIntent.FLAG_UPDATE_CURRENT);
                AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
                am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);  
             }
        }
    }   

    private boolean taskIsNotRunning() {
        return task == null || task.getStatus() != AsyncTask.Status.RUNNING || task.isCancelled();
    }

    @Override
    public void onDestroy() {  
             super.onDestroy();

    }
    private class UploadFilesTask extends AsyncTask<Object, Object, Object> {
        @Override
        protected Object doInBackground(Object... arg0) {  
            SQLiteDatabase db = dbs.getWritableDatabase(); 
            ContentValues values = new ContentValues();
            values.put(DBAdapter.ID,  "blank");
            db.insert(DBAdapter.Table, null, values);
            return null;            
        }

        @Override
        protected void onPostExecute(Object result) {       
            super.onPostExecute(result);
            stopSelf();
        }   
    }
}

我想运行一个将值插入到 sql 数据库表中的服务。如果没有互联网连接,我希望它等待 5 分钟,然后再次运行该服务。

代码中的问题在于 SQLiteDatabase db = dbs.getWritableDatabase(); 我可以评估服务中的数据库吗?如果是这样我该如何让我的代码运行?谢谢

public class IdService extends Service {
    private UploadFilesTask task;
    DBAdapter dbs;

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

    @Override
    public void onCreate() {
        super.onCreate();               
    }

    @Override
    public void onStart(Intent intent, int startId) {   
        super.onStart(intent, startId); 
        if(Networking.isNetworkAvailable(this)){
            if ( intent.hasExtra("start") && taskIsNotRunning() ) {
                task = new UploadFilesTask();
                task.execute();             
            } 
        }
        else{
            if ( intent.hasExtra("start") && taskIsNotRunning() ) {
                Calendar cal = Calendar.getInstance();
                cal.add(Calendar.MINUTE, 5);
                String start = "start";
                Intent i = new Intent(IdService.this, IdService.class);
                i.putExtra("start", start);
                PendingIntent sender = PendingIntent.getService(this, 192837, i, PendingIntent.FLAG_UPDATE_CURRENT);
                AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
                am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);  
             }
        }
    }   

    private boolean taskIsNotRunning() {
        return task == null || task.getStatus() != AsyncTask.Status.RUNNING || task.isCancelled();
    }

    @Override
    public void onDestroy() {  
             super.onDestroy();

    }
    private class UploadFilesTask extends AsyncTask<Object, Object, Object> {
        @Override
        protected Object doInBackground(Object... arg0) {  
            SQLiteDatabase db = dbs.getWritableDatabase(); 
            ContentValues values = new ContentValues();
            values.put(DBAdapter.ID,  "blank");
            db.insert(DBAdapter.Table, null, values);
            return null;            
        }

        @Override
        protected void onPostExecute(Object result) {       
            super.onPostExecute(result);
            stopSelf();
        }   
    }
}

I want to run a service which inserts a value into a table in sql database. If there is no internet connection i want it to wait 5 minutes and then run the service again.

The issue in the code is at SQLiteDatabase db = dbs.getWritableDatabase();
Can i assess the db within a service? If so how do i make me code run? thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↙温凉少女 2024-11-23 02:18:24

是的,为什么不呢。使用常规方式。不要忘记添加 SQLiteDatabse.setLockingEnabled(true);让数据库连接线程安全!

PS我看不到你从哪里启动asyncTask?

Yes, why not. Use the regular way. Don't forget to add SQLiteDatabse.setLockingEnabled(true); to make the database connection thread safe!

P.S. I can not see where do you start the asyncTask?

音盲 2024-11-23 02:18:24

让它工作...需要 this.dbs = new DBAdapter(getApplicationContext());

Got it to work...needed this.dbs = new DBAdapter(getApplicationContext());

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文