Eclipse中的Android - 在后台调用File Writer类 [英] Android in Eclipse - Invoke File Writer class in Background

查看:163
本文介绍了Eclipse中的Android - 在后台调用File Writer类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击按钮(btnPrintTardy)我需要生成一个.txt文件,我输入到txtbox(editText1)。

When I click the button (btnPrintTardy) I need to generate a .txt file of whatever i have entered into a txtbox (editText1).

这是我的文件作家java类。

This is my File Writer java class.

import android.app.Activity;

import java.io.*;
import android.os.Bundle;
import android.view.*;
import android.widget.*;

public class FileWriter extends Activity {
    EditText txtData;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    txtData = (EditText) findViewById(R.id.editText1);
    }


    public void onClick(View v) {
        // write in data folder
        try {
            File myFile = new File("/data/LatePass/StudentLatePass.txt");
            myFile.createNewFile();
            FileOutputStream fOut = new FileOutputStream(myFile);
            OutputStreamWriter myOutWriter = 
                                    new OutputStreamWriter(fOut);
            myOutWriter.append(txtData.getText());
            myOutWriter.close();
            fOut.close();
            Toast.makeText(getBaseContext(),
                    "Finished writing StudentLatePass.txt'",
                    Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(getBaseContext(), e.getMessage(),
                    Toast.LENGTH_SHORT).show();
        }

                finish();
                };
}

我从我的StudentActivity调用这个方法,有点卡住了我想留在我当前的屏幕活动,但在后台运行文件撰写者。那么我怎么称之为

I am calling this method from my "StudentActivity" however, this is where i am kind of stuck. I want to stay on my current screen activity, but run the filewriter in the background. So how would I call this?

我尝试过

 public void UpdateStudenttxtfile(View View)
    {

    Intent intent = new Intent(View.getContext(), FileWriter.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    }

没有运气:(

推荐答案

我建议使用AsyncTask进行这个后台工作,而不是单独实现:
http://developer.android.com/reference/android/os/AsyncTask.html

I would recommend using an AsyncTask for this background work instead of implementing as a separate activity: http://developer.android.com/reference/android/os/AsyncTask.html

然后,而不是发送一个Intent,你只需调用:

Then, instead of sending an Intent, you just call:

EditText txtData = (EditText) findViewById(R.id.editText1);
FileWriterTask task = new FileWriterTask();
task.execute(txtData.getText().toString());

public class FileWriterTask extends AsyncTask<String, Void, Void> {
  @Override
  protected Void doInBackground(String... params) {
    // Do your filewriting here. The text should now be in params[0]
  }
}

这是AsyncTask的另一个答案:
我在哪里可以扩展AsyncTask?

Here's another answer about AsyncTask: Where do I extend the AsyncTask?

这篇关于Eclipse中的Android - 在后台调用File Writer类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆