除非它多次调用doInBackground
,否则我的Asynctask工作正常。
我正在通过此Broadcastreceiver
AsyncTask
来电Broadcastreceiver
拨打Service
该服务执行一些后台工作
所以它称之为Broadcastreceiver三次
这是为什么 ?
任何想法?
提示:
onreceive
中的logcat
引用onReceive
中的myBroadCastReceiver
方法,onCommandStart
引用myService
UploadFileAsync.java:
public class UploadFileAsync extends AsyncTask<String, Integer, String> {
private int serverResponseCode = 0;
private String sourceFileUri, link, spec, username, title, abstract_, desc;
private Intent intent;
private Context ctx;
private NotificationCompat.Builder mBuilder;
private NotificationManager mNotifyManager;
public UploadFileAsync(Intent intent, String sourceFileUri, Context ctx) {
this.intent = intent;
this.sourceFileUri = sourceFileUri;
this.ctx = ctx;
}
private void prepareParams() {
this.title = intent.getExtras().getString("title");
this.username = intent.getExtras().getString("username");
this.abstract_ = intent.getExtras().getString("abstract");
this.link = intent.getExtras().getString("link");
this.spec = intent.getExtras().getString("spec");
this.desc = intent.getExtras().getString("desc");
}
@Override
protected String doInBackground(String... params) {
Log.e("doInBackground", "===========================");
try {
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);
if (sourceFile.isFile()) {
try {
String upLoadServerUri = Constants.UPLOAD_FILE_TO_SERVER;
FileInputStream fileInputStream = new FileInputStream(
sourceFile);
URL url = new URL(upLoadServerUri);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE",
"multipart/form-data");
conn.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("masharee3", sourceFileUri);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"masharee3\";filename=\""
+ sourceFileUri + "\"" + lineEnd);
dos.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math
.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0,
bufferSize);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens
+ lineEnd);
serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn
.getResponseMessage();
if (serverResponseCode == 200) {
Log.e("server response = 200", " "+serverResponseMessage);
mBuilder.setContentText(ctx.getResources().getString(R.string.publish_completed))
.setProgress(0, 0, false);
mNotifyManager.notify(Constants.NOT_ID, mBuilder.build());
Intent i = new Intent()
.putExtra("username", username)
.putExtra("title", title)
.putExtra("spec", spec)
.putExtra("link", link)
.putExtra("desc", desc)
.putExtra("abstract", abstract_);
i.setAction("com.omaralmrsomi.masharee3app.UPLOAD_COMPLETE_NOTIFICATION");
ctx.getApplicationContext().sendBroadcast(i);
} else {
mBuilder.setContentText(ctx.getResources().getString(R.string.publish_failed))
.setProgress(0, 0, false);
mNotifyManager.notify(Constants.NOT_ID, mBuilder.build());
}
fileInputStream.close();
dos.flush();
dos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return String.valueOf(serverResponseCode);
}
@Override
protected void onPostExecute(String result) {
Log.e("onPostExecute", "===========================");
}
@Override
protected void onPreExecute() {
Log.e("onPreExecute", "============================");
prepareParams();
mNotifyManager =
(NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(ctx);
mBuilder.setContentTitle(ctx.getResources().getString(R.string.publishing_project))
.setContentText(ctx.getResources().getString(R.string.publishing))
.setSmallIcon(R.drawable.ic_stat);
mBuilder.setProgress(0, 0, true);
mNotifyManager.notify(Constants.NOT_ID, mBuilder.build());
}
}
我的LogCAT
09-13 22:56:40.271 9505-9505/com.omaralmrsomi.masharee3app E/onPreExecute: ============================
09-13 22:56:40.355 9505-9653/com.omaralmrsomi.masharee3app E/doInBackground: ===========================
09-13 22:56:41.064 9505-9653/com.omaralmrsomi.masharee3app E/server response = 200: OK
09-13 22:56:41.078 9505-9505/com.omaralmrsomi.masharee3app E/onRecevie: ======--=========
09-13 22:56:41.090 9505-9505/com.omaralmrsomi.masharee3app E/onRecevie: ======--=========
09-13 22:56:41.102 9505-9505/com.omaralmrsomi.masharee3app E/onRecevie: ======--=========
09-13 22:56:41.110 9505-9505/com.omaralmrsomi.masharee3app E/oncommandstart: ============
09-13 22:56:41.110 9505-9505/com.omaralmrsomi.masharee3app E/getData not null: ===-=-
09-13 22:56:41.110 9505-9505/com.omaralmrsomi.masharee3app E/uploadToServer: =-=-=
09-13 22:56:41.124 9505-9505/com.omaralmrsomi.masharee3app E/oncommandstart: ============
09-13 22:56:41.124 9505-9505/com.omaralmrsomi.masharee3app E/getData not null: ===-=-
09-13 22:56:41.124 9505-9505/com.omaralmrsomi.masharee3app E/uploadToServer: =-=-=
09-13 22:56:41.134 9505-9505/com.omaralmrsomi.masharee3app E/oncommandstart: ============
09-13 22:56:41.134 9505-9505/com.omaralmrsomi.masharee3app E/getData not null: ===-=-
09-13 22:56:41.134 9505-9505/com.omaralmrsomi.masharee3app E/uploadToServer: =-=-=
09-13 22:56:41.164 9505-9505/com.omaralmrsomi.masharee3app E/onPostExecute: ===========================
更新:
这就是我如何调用AsyncTask:
create_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckConnection chk = new CheckConnection(getApplicationContext(), NewPost.this);
if (chk.testConnection()) {
if ((!inputtitle.getText().toString().equals("")) && (!inputAbstract.getText().toString().equals("")) && (!inputdesc.getText().toString().equals(""))) {
if (state == 1) {
PdfPathHolder = FilePath.getPath(NewPost.this, uri);
link = Constants.SERVER_LINK + "" + (PdfPathHolder != null ? PdfPathHolder.substring(PdfPathHolder.lastIndexOf("/") + 1) : null);
username = userName.getText().toString();
title = inputtitle.getText().toString();
desc = inputdesc.getText().toString();
abstract_ = inputAbstract.getText().toString();
new_spec = check();
Intent i = new Intent()
.putExtra("username", username)
.putExtra("title", title)
.putExtra("spec", new_spec)
.putExtra("link", link)
.putExtra("desc", desc)
.putExtra("abstract", abstract_);
new UploadFileAsync(i, PdfPathHolder, NewPost.this).execute("");
} else {
Toast.makeText(NewPost.this, please_select_afile, Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(NewPost.this, one, Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(NewPost.this, bad_internet, Toast.LENGTH_SHORT).show();
}
}
});