-2

my android application freezes for a few seconds when i try to send my database file to my remote server, is there anyway i can set this as a background thread using multithreading or another such feature?

this is the code for the dialog box that appears to ask me if i want to send

    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();
}

and here is the code it executes when i click the "Yes" button

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();
    }
}

any help with this problem would be greatly appreciated as 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.

simfrek
  • 39
  • 1
  • 8
  • Check this post on multiThreading http://stackoverflow.com/questions/1921514/how-to-run-a-runnable-thread-in-android – WhiskThimble Jul 19 '13 at 15:08

3 Answers3

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.

You can see AsyncTask. Its for the same purposes. User need not be stuck on UI when you want to do long running processes including taking to server, doing database stuff etc. This can be done in the background using AsyncTask. There are many good tutorials out there. Just Google it. Checkout Android Background Processing with Threads, Handlers and AsyncTask - Tutorial and What arguments are passed into AsyncTask<arg1, arg2, arg3>? . Hope this helps.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • Beat me to the punch :) – marcus.ramsden Jul 19 '13 at 15:08
  • what would i put where it says URL? private class DownloadFilesTask extends AsyncTask { – simfrek Jul 19 '13 at 15:11
  • You can choose the parameter of `AsyncTask` based on your requirements. It need not be `URL`. It would be helpful for you if you first read the documentation page of it or do some tutorial about it. http://stackoverflow.com/questions/6053602/what-arguments-are-passed-into-asynctaskarg1-arg2-arg3 post will definitely help you clear your doubts. – Shobhit Puri Jul 19 '13 at 15:18
  • @marcus.ramsden It happens with me very often ;) – Shobhit Puri Jul 19 '13 at 15:26
0

I've been using asynctask for handling all the processes I want to do in the background.

AsyncTask

kevswanberg
  • 2,079
  • 16
  • 21
0

Use all network operations in AsyncTask

like the below example

 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");
 }
 }

Once created, a task is executed very simply:

 new UploadTask().execute(url1, url2, url3);
blganesh101
  • 3,647
  • 1
  • 24
  • 44