-2

当我尝试将数据库文件发送到远程服务器时,我的 android 应用程序冻结了几秒钟,无论如何我可以使用多线程或其他此类功能将其设置为后台线程吗?

这是出现询问我是否要发送的对话框的代码

    public void showYesNoBox(){
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which){
        case DialogInterface.BUTTON_POSITIVE:
            //Yes button clicked
            SendData();
            finish();//go back to the previous Activity
        overridePendingTransition(R.anim.fadein, R.anim.fadeout);

            break;

        case DialogInterface.BUTTON_NEGATIVE:
            //No button clicked

            finish();//go back to the previous Activity
        overridePendingTransition(R.anim.fadein, R.anim.fadeout);
            break;
        }
    }
};

AlertDialog.Builder builder = new AlertDialog.Builder(this, AlertDialog.THEME_HOLO_DARK);
builder.setMessage("Do you want to send the data now?");
builder.setPositiveButton("Yes", dialogClickListener);
builder.setNegativeButton("No", dialogClickListener);
builder.show();
}

这是我单击“是”按钮时执行的代码

public void SendData() {

    File path = getDatabasePath(DataBaseHelper.DATABASE_NAME);
    if (path.exists())
        copyDatabase(path);
}


/**
 * Copy the database to the sdcard
 * @param file
 */
private void copyDatabase(File file) {
    SimpleDateFormat dateFormat1 = new SimpleDateFormat("ddMMyy-HHmmss");
    String dateString = dateFormat1.format(new Date());
    String pathdest = getDir().getAbsolutePath() + Configuration.LOST_FOLDER + "/Database/";
    String pathdir = pathdest;
    File dir = new File(pathdir);
    if (!dir.exists())
        dir.mkdirs();

    String namefile = file.getName();
    int pos = namefile.lastIndexOf('.');
    if (pos != -1) {
        String ext = namefile.substring(pos + 1);
        String name = namefile.substring(0, pos - 1);
        pathdest += name;
        pathdest += "_" + dateString;
        pathdest += ext;
    } else {
        pathdest += namefile;
        pathdest += "_" + dateString;
    }

    File filedest = new File(pathdest);
    try {
        if (filedest.createNewFile())
            copyFile(file.getAbsolutePath(), pathdest);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

对此问题的任何帮助将不胜感激,因为虽然该问题并不能阻止用户发送数据,但在尝试执行操作时,它会在一个屏幕上停留几秒钟。

4

3 回答 3

2

while the problem doesn't prevent the user from being able to send the data it is anoying to be stuck on one screen for a few seconds while it attempts to execute the action.

你可以看到AsyncTask。出于同样的目的。当您想要执行长时间运行的进程(包括进入服务器、执行数据库操作等)时,用户无需停留在 UI 上。这可以在后台使用AsyncTask. 那里有很多很好的教程。只需谷歌它。Checkout Android Background Processing with Threads, Handlers and AsyncTask - TutorialWhat arguments are passing into AsyncTask<arg1, arg2, arg3>? . 希望这可以帮助。

于 2013-07-19T15:07:30.930 回答
0

我一直在使用 asynctask 来处理我想在后台执行的所有进程。

异步任务

于 2013-07-19T15:08:26.643 回答
0

使用所有网络操作AsyncTask

像下面的例子

 private class UploadTask extends AsyncTask<URL, Integer, Long> {
 protected Long doInBackground(URL... urls) {
     int count = urls.length;
     long totalSize = 0;
     // upload task 
     return totalSize;
 }

 protected void onProgressUpdate(Integer... progress) {
     setProgressPercent(progress[0]);
 }

 protected void onPostExecute(Long result) {
     showDialog("Downloaded " + result + " bytes");
 }
 }

一旦创建,任务就会非常简单地执行:

 new UploadTask().execute(url1, url2, url3);
于 2013-07-19T15:09:24.363 回答