私は以下のコードに問題があります.onProgressUpdateは実行されていません.しかし、onPreExecute、doInBackground、およびonPostExecuteはそれに応じて実行されます. または、doInBackground でリターンが必要ですか? 何が欠けているかアドバイスしてください。以前は削除していました
notification.contentView.setProgressBar(R.id.pbStatus, 100, progress, false);
notificationManager = (NotificationManager) getApplicationContext().getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
notificationManager.notify(42, notification);
onCreate では、通知はまったく表示されませんでした。以下は完全なコードです:
package com.android.MaxApps;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.RemoteViews;
import android.widget.Toast;
public class StaffChoice extends Activity {
ProgressBar progressBar;
private int progress;
Intent MyI;
PendingIntent MyPI;
NotificationManager MyNM;
Notification notification;
NotificationManager notificationManager;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.staffchoices);
MyI = new Intent(getApplicationContext(), MaxAppsAct.class);
MyPI = PendingIntent.getActivity(getApplicationContext(), 0, MyI, 0);
MyNM = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(getApplicationContext(), MaxAppsAct.class);
final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
notification = new Notification(R.drawable.logo, "Downloading...", System.currentTimeMillis());
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.staffchoices);
notification.contentIntent = pendingIntent;
notification.contentView.setImageViewResource(R.id.imgIcon, R.drawable.save);
notification.contentView.setTextViewText(R.id.tvText, "Downloading...");
notification.contentView.setProgressBar(R.id.pbStatus, 100, progress, false);
notificationManager = (NotificationManager) getApplicationContext().getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
notificationManager.notify(42, notification);
String url = "http://www.domainURL.com/3d.png";
new DownloadFileAsync().execute(url);
}
public class DownloadFileAsync extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute(){
super.onPreExecute();
}
@Override
protected String doInBackground(String... aurl) {
int count;
try {
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
File folder = new File(Environment.getExternalStorageDirectory() + "/MaxApps");
boolean success = false;
if (!folder.exists()) {
success = folder.mkdirs();
}
if (!success) {
} else {
}
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/MaxApps/3d.png");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {}
return null;
}
protected void onProgressUpdate(Integer... progress) {
notification.contentView.setProgressBar(R.id.pbStatus, 100, progress[0], false);
notificationManager.notify(42, notification);
}
@Override
protected void onPostExecute(String unused) {
notificationManager.cancel(42);
Notification MyN = new Notification(); MyN.icon = R.drawable.logo1;
MyN.tickerText = "Download Complete";
MyN.number = 1;
MyN.setLatestEventInfo (getApplicationContext(), "Application Title", "Application Description", MyPI);
MyNM.notify(1, MyN);
}
}
}