Я сделал код, который загружает APK с ftp, и пытаюсь установить его после загрузки. Я написал это для Сота, поэтому в каждом соединении я должен использовать темы. Как я могу использовать startActivity в классе в потоке или ждать завершения потока?
public class FTPapkDowload {
protected static final String TAG = "Tablet Development";
public FTPClient mFTPClient = null;
public FTPClient mFtp = null;
public void Start() {
Thread apkdowload = new Thread(new Runnable() {
@Override
public void run() {
ftpConnect("mysite", "username", "password",21);
Log.d(TAG, "Connected");
ftpDownload("/httpdocs/Shamir/app.apk", "sdcard/Download/app.apk");
ftpDisconnect();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/Download/" + "app.apk")), "application/vnd.android.package-archive");
startActivity(intent); //Here is the problem
}
//Connection
public boolean ftpConnect(String host, String username,
String password, int port) {
try {
mFTPClient = new FTPClient();
mFTPClient.connect(host, port);
if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
boolean status = mFTPClient.login(username, password);
mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
mFTPClient.enterLocalPassiveMode();
return status;
}
} catch (Exception e) {
Log.d(TAG, "Error: could not connect to host " + host);
}
return false;
}
//Downloading
public boolean ftpDownload(String srcFilePath, String desFilePath) {
boolean status = false;
try {
FileOutputStream desFileStream = new FileOutputStream(
desFilePath);
status = mFTPClient
.retrieveFile(srcFilePath, desFileStream);
desFileStream.close();
return status;
} catch (Exception e) {
Log.d(TAG, "download failed");
}
return status;
}
public boolean ftpDisconnect() {
try {
mFTPClient.logout();
mFTPClient.disconnect();
Log.d(TAG, "Disconected from FTP on apk Download");
return true;
} catch (Exception e) {
Log.d(TAG,"Error occurred while disconnecting from ftp server on apk download.");
}
return false;
}
});
apkdowload.start();
}
}