AsyncTask 可以在服务内运行并写入数据库吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,为什么不呢。使用常规方式。不要忘记添加 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?
让它工作...需要 this.dbs = new DBAdapter(getApplicationContext());
Got it to work...needed this.dbs = new DBAdapter(getApplicationContext());