android 如何在后台向数据库添加大量记录
how to add a large number of records in background to database in android
我需要在 android 应用程序中将大量数据插入数据库。我有一个包含列表视图的对话框,其中包含行名称、日期、频率,其中频率值可以是 "Daily"、"Monthly"、"Quarterly"、"Yearly"。在将行添加到列表视图后,我需要单击完成按钮将这些数据保存到数据库中。我需要根据其频率值存储每一行。也就是说,如果频率值为 "Daily" 我需要将 1095(即 3*365--> 3 年的每日记录)记录存储到该名称的数据库中,对于每个频率值("Monthly"--> 3*12, "Quarterly"--> 3*4,...) 所以它会导致向数据库中大量插入行,这会导致执行时间很长,所以用户可能会感觉不好,他无法在应用程序中执行任何其他操作而不完成 this.I 我正在使用 ORMLite 将数据添加到 database.I 已完成如下操作。
for (int i = 0; i < listAdapter.getCount(); i++) {
Account rowAcct = new Account();
rowAcct = listAdapter.getItem(i);
if (rowAcct.getFreq().equals("Daily")) {
for (int y = 0; y < 1095; y++) {
Account nwEvt = new Account();
dueDt = saveController.getNextDay(dueDt);
nwEvt.setDueDate(dueDt);
evtId = saveController.createAcc(nwEvt);
}
} else if (rowAcct.getFreq().equals("Monthly")) {
for (int y = 0; y < 35; y++) {
Account nwEvt = new Account();
Date dueDt = saveController.getNextMonth(dueDt);
nwEvt.setDueDate(dueDt);
saveController.createAcc(nwEvt);
}
} else if (rowAcct.getFreq().equals("Quarterly")) {
for (int y = 0; y < 11; y++) {
Account nwEvt = new Account();
dueDt = saveController.getQuarterMonth(dueDt);
nwEvt.setDueDate(dueDt);
evtId = saveController.createAcc(nwEvt);
}
} else if (rowAcct.getFreq().equals("Half-Yearly")) {
for (int y = 0; y < 5; y++) {
Account nwEvt = new Account();
Date dueDt = saveController.getHalfYear(dueDt);
nwEvt.setDueDate(dueDt);
saveController.createAcc(nwEvt);
}
} else if (rowAcct.getFreq().equals("Yearly")) {
for (int y = 0; y < 2; y++) {
Account nwEvt = new Account();
Date dueDt = saveController.getNextYear(dueDt);
nwEvt.setDueDate(dueDt);
saveController.createAcc(nwEvt);
}
}
}
dialog.dismiss();
我怎样才能最大限度地减少插入时间或使插入过程在后台进行,并让用户可以自由地在应用程序中做其他事情?请建议我让用户免费来执行此操作的最佳方法。
第一个,您需要在工作线程中使用 DB 执行所有工作。只需使用 android 内置 AsyncTasks
第二次检查 this, 答案以改进数据库中的插入。
好的,使用 AsyncTaskLoader =) 它检查当前 Activity 状态 =)
您需要为此使用一个 IntentService class 及其处理程序,它是为长时间 运行 task.Request IntentService class 设计的,点击完成按钮后传递必要的数据,如下所示
Intent intentEv= new Intent(Intent.ACTION_INSERT, null, getApplicationContext(), MyIntentService.class);
intentEv.putExtra("requestId", 101);
intentEv.putExtra("listAccounts",arrayList);
startService(intentEv);
这将请求 Intentservice class,然后您可以使用 IntentService 中的 onHandleIntent(Intent intent) 函数在后台执行数据库插入。
public class MyIntentService extends IntentService{
public static final int STATUS_RUNNING = 0;
public static final int STATUS_FINISHED = 1;
public static final int STATUS_ERROR = 2;
public MyIntentService() {
super("BulkInsertionService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d(TAG, "Service Started!");
ArrayList<Account> listAccounts=(ArrayList<Account>) intent.getSerializableExtra("listAccounts");
/* perform the long running insert operations now.. */
this.stopSelf();
}
}
同时在您的 AndroidManifest.xml 文件中初始化您的 IntentService class
我需要在 android 应用程序中将大量数据插入数据库。我有一个包含列表视图的对话框,其中包含行名称、日期、频率,其中频率值可以是 "Daily"、"Monthly"、"Quarterly"、"Yearly"。在将行添加到列表视图后,我需要单击完成按钮将这些数据保存到数据库中。我需要根据其频率值存储每一行。也就是说,如果频率值为 "Daily" 我需要将 1095(即 3*365--> 3 年的每日记录)记录存储到该名称的数据库中,对于每个频率值("Monthly"--> 3*12, "Quarterly"--> 3*4,...) 所以它会导致向数据库中大量插入行,这会导致执行时间很长,所以用户可能会感觉不好,他无法在应用程序中执行任何其他操作而不完成 this.I 我正在使用 ORMLite 将数据添加到 database.I 已完成如下操作。
for (int i = 0; i < listAdapter.getCount(); i++) {
Account rowAcct = new Account();
rowAcct = listAdapter.getItem(i);
if (rowAcct.getFreq().equals("Daily")) {
for (int y = 0; y < 1095; y++) {
Account nwEvt = new Account();
dueDt = saveController.getNextDay(dueDt);
nwEvt.setDueDate(dueDt);
evtId = saveController.createAcc(nwEvt);
}
} else if (rowAcct.getFreq().equals("Monthly")) {
for (int y = 0; y < 35; y++) {
Account nwEvt = new Account();
Date dueDt = saveController.getNextMonth(dueDt);
nwEvt.setDueDate(dueDt);
saveController.createAcc(nwEvt);
}
} else if (rowAcct.getFreq().equals("Quarterly")) {
for (int y = 0; y < 11; y++) {
Account nwEvt = new Account();
dueDt = saveController.getQuarterMonth(dueDt);
nwEvt.setDueDate(dueDt);
evtId = saveController.createAcc(nwEvt);
}
} else if (rowAcct.getFreq().equals("Half-Yearly")) {
for (int y = 0; y < 5; y++) {
Account nwEvt = new Account();
Date dueDt = saveController.getHalfYear(dueDt);
nwEvt.setDueDate(dueDt);
saveController.createAcc(nwEvt);
}
} else if (rowAcct.getFreq().equals("Yearly")) {
for (int y = 0; y < 2; y++) {
Account nwEvt = new Account();
Date dueDt = saveController.getNextYear(dueDt);
nwEvt.setDueDate(dueDt);
saveController.createAcc(nwEvt);
}
}
}
dialog.dismiss();
我怎样才能最大限度地减少插入时间或使插入过程在后台进行,并让用户可以自由地在应用程序中做其他事情?请建议我让用户免费来执行此操作的最佳方法。
第一个,您需要在工作线程中使用 DB 执行所有工作。只需使用 android 内置 AsyncTasks
第二次检查 this,
好的,使用 AsyncTaskLoader =) 它检查当前 Activity 状态 =)
您需要为此使用一个 IntentService class 及其处理程序,它是为长时间 运行 task.Request IntentService class 设计的,点击完成按钮后传递必要的数据,如下所示
Intent intentEv= new Intent(Intent.ACTION_INSERT, null, getApplicationContext(), MyIntentService.class);
intentEv.putExtra("requestId", 101);
intentEv.putExtra("listAccounts",arrayList);
startService(intentEv);
这将请求 Intentservice class,然后您可以使用 IntentService 中的 onHandleIntent(Intent intent) 函数在后台执行数据库插入。
public class MyIntentService extends IntentService{
public static final int STATUS_RUNNING = 0;
public static final int STATUS_FINISHED = 1;
public static final int STATUS_ERROR = 2;
public MyIntentService() {
super("BulkInsertionService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d(TAG, "Service Started!");
ArrayList<Account> listAccounts=(ArrayList<Account>) intent.getSerializableExtra("listAccounts");
/* perform the long running insert operations now.. */
this.stopSelf();
}
}
同时在您的 AndroidManifest.xml 文件中初始化您的 IntentService class