我有一个ftp客户端通常发送文件,但是当我按下菜单或在手机上接听电话时,它不会在后台发送并关闭应用程序。这是正常的吗?我发送了一个文件,当你差不多完成时,如果我重新开始,有人给我打电话并清除我的上传文件?
谢谢。 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.informacoes);
assunto = (EditText)findViewById(R.id.tf_assunto);
comentario = (EditText)findViewById(R.id.tf_comentario);
onde = (EditText)findViewById(R.id.tf_onde);
if(caminhoArquivo != null){
arquivo = new File(caminhoArquivo);
}
else{
finish();
}
enviar();
sair();
enviarSegundoPlano = new AsyncTask<Void, Void, Void>(){
@Override
protected Void doInBackground(Void... params) {
nomeArquivo = arquivo.getName();
return null;
}
};
enviarSegundoPlano.execute();
}
private void enviarArquivo(final File arquivo){
Tarefa tarefa = new Tarefa(mProgressDialog){
@Override
protected String doInBackground(String... params) {
try{
//ftp.abort();
if(conecta(usuario, senha, host)){
State wifi = Principal.conexao.getNetworkInfo(1).getState();
final String subPasta = txAssunto;
FileChannel fileChannelEntrada = new RandomAccessFile(arquivo, "r").getChannel();
ftp.changeWorkingDirectory(pasta);
ftp.mkd(subPasta);
ftp.changeWorkingDirectory(subPasta);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
if(wifi == NetworkInfo.State.CONNECTED){
ftp.enterLocalPassiveMode();
}
WritableByteChannel saida = Channels.newChannel(ftp.storeFileStream(arquivo.getName()));
//ByteBuffer byteBuffer = ByteBuffer.allocateDirect(16384);
ByteBuffer byteBuffer = ByteBuffer.allocate(90);
byteBuffer.clear();
while(fileChannelEntrada.read(byteBuffer) != -1){
byteBuffer.flip();
saida.write(byteBuffer);
byteBuffer.clear();
publishProgress(getPorcentagem(fileChannelEntrada.size(), fileChannelEntrada.position()));
if(fileChannelEntrada.read(byteBuffer) > 0 && fileChannelEntrada.read(byteBuffer) < 100){
resposta = true;
}
}
}
}
catch(Exception e){
resposta = false;
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
enviarArquivo(arquivoTXT, true);
if(resposta){
try{
new Thread(){
public void run(){
try{
Email email = new Email(Principal.emailUsuario, txAssunto, txOnde, txComentario);
email.enviarEmail();
}
catch(Exception e){
e.printStackTrace();
}
}
}.start();
mostrarTextoTela("Enviado com Sucesso!");
}catch(Exception e){e.printStackTrace();}
}
else{
mostrarTextoTela("Erro ao Enviar");
}
super.onPostExecute(result);
finish();
}
};
tarefa.execute();
}
logcat的
01-09 07:09:26.789: D/dalvikvm(21014): GC_EXTERNAL_ALLOC freed 57K, 48% free 2808K/5379K, external 511K/517K, paused 195ms
01-09 07:10:09.140: D/dalvikvm(21534): GC_EXTERNAL_ALLOC freed 56K, 48% free 2803K/5379K, external 511K/517K, paused 38ms
01-09 07:10:24.281: D/dalvikvm(21534): GC_CONCURRENT freed 215K, 47% free 3026K/5639K, external 816K/996K, paused 4ms+3ms
01-09 07:12:01.234: W/IInputConnectionWrapper(21534): showStatusIcon on inactive InputConnection
01-09 07:12:07.101: W/dalvikvm(22169): threadid=9: thread exiting with uncaught exception (group=0x40018578)
01-09 07:12:07.109: E/AndroidRuntime(22169): FATAL EXCEPTION: AsyncTask #1
01-09 07:12:07.109: E/AndroidRuntime(22169): java.lang.RuntimeException: An error occured while executing doInBackground()
01-09 07:12:07.109: E/AndroidRuntime(22169): at android.os.AsyncTask$3.done(AsyncTask.java:200)
01-09 07:12:07.109: E/AndroidRuntime(22169): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
01-09 07:12:07.109: E/AndroidRuntime(22169): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
01-09 07:12:07.109: E/AndroidRuntime(22169): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
01-09 07:12:07.109: E/AndroidRuntime(22169): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
01-09 07:12:07.109: E/AndroidRuntime(22169): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
01-09 07:12:07.109: E/AndroidRuntime(22169): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
01-09 07:12:07.109: E/AndroidRuntime(22169): at java.lang.Thread.run(Thread.java:1019)
01-09 07:12:07.109: E/AndroidRuntime(22169): Caused by: java.lang.NullPointerException
01-09 07:12:07.109: E/AndroidRuntime(22169): at br.com.marcio.Informacoes$1.doInBackground(Informacoes.java:75)
01-09 07:12:07.109: E/AndroidRuntime(22169): at br.com.marcio.Informacoes$1.doInBackground(Informacoes.java:1)
01-09 07:12:07.109: E/AndroidRuntime(22169): at android.os.AsyncTask$2.call(AsyncTask.java:185)
01-09 07:12:07.109: E/AndroidRuntime(22169): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
01-09 07:12:07.109: E/AndroidRuntime(22169): ... 4 more
答案 0 :(得分:0)
enviarSegundoPlano = new AsyncTask<Void, Void, Void>(){
@Override
protected Void doInBackground(Void... params) {
if(arquivo!=null)
nomeArquivo = arquivo.getName();
else{
Log.d("LOG_APP", "arquivo null");
// ...
}
return null;
}
};
enviarSegundoPlano.execute();
当arkuivo为空时处理情况