我將一些代碼從普通方法移到了AsyncTask doInBackground方法中,除filereader之外,一切正常。我得到「上下文無法解決」。 我試着把「context.getApplicationContext()」放在一起,但這只是讓我「類型AlarmReceiver.backgroundputFTP的方法openFileInput(字符串)未定義」。任何人的想法請好嗎?AsyncTask中的FileInputStream錯誤
public void putFTP(Context context)
{
new backgroundputFTP().execute();
}
private class backgroundputFTP extends AsyncTask< Void, Void,Void >
{
@Override
protected Void doInBackground(Void... params)
{
//
// Push query result text file to FTP server
//
FTPClient client = new FTPClient();
FileInputStream fis = null;
Looper.prepare();
try {
client.connect(ipAddress);
client.enterLocalPassiveMode();
client.login(user, pass);
//
// Create an InputStream of the file to be uploaded
//
filename = "sdcardstats.txt";
fis = context.getApplicationContext().openFileInput(filename);
感謝試圖幫助,但結果「的方法的getContext()是未定義的類型AlarmReceiver」 – Kevmeister
@Kevmeister然後,定義在AlarmReceiver現場上下文mContext,做mContext =上下文您putFTP ,首先,然後使用AsyncTask中的mContext.openFileInput –