Android 更新通知进度失败为什么?
我的问题是无法从我的Android 更新通知进度失败为什么?,android,upload,Android,Upload,我的问题是无法从我的AsyncTask更新通知信息,如进度条或文本 我在这个问题上花了不少时间,但没有找到解决办法。我的目标是只能更改对话框进度条和文本 如何使此通知进度条工作 这是我的密码: private class excuteUploadFile extends AsyncTask<HttpResponse, Integer, HttpResponse> { long totalSize; int NOTIFICATION_ID; S
AsyncTask
更新通知信息,如进度条或文本
我在这个问题上花了不少时间,但没有找到解决办法。我的目标是只能更改对话框进度条和文本
如何使此通知进度条工作
这是我的密码:
private class excuteUploadFile extends
AsyncTask<HttpResponse, Integer, HttpResponse> {
long totalSize;
int NOTIFICATION_ID;
String filepath, fileName;
String serverResponse = null;
String urlServer = "http://myurl";
NotificationManager mNotifyManager;
Builder mBuilder;
ProgressDialog pd;
Boolean nofti_appended = false;
JSONObject jsonfile = null;
public excuteUploadFile(String filepath, String fileName) {
this.filepath = filepath;
this.fileName = fileName;
Log.i("uploader", "Selected file name : " + fileName);
Log.i("uploader", "Selected file path : " + filepath);
}
@Override
protected void onPreExecute() {
pd = new ProgressDialog(BabupMain.this);
// pd.setMessage(getResources().getString(R.string.uploading_file));
pd.setTitle(fileName);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setCancelable(false);
pd.setIcon(android.R.drawable.ic_menu_upload);
pd.setButton(DialogInterface.BUTTON_NEGATIVE,
getResources().getString(R.string.cancel),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
cancel(true);
}
});
// hide button
pd.setButton(DialogInterface.BUTTON_NEUTRAL,
getResources().getString(R.string.hide),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Random r = new Random();
NOTIFICATION_ID = r.nextInt(80 - 65) + 1;
NOTIFICATION_ID++;
mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(
BabupMain.this);
mBuilder.setContentTitle(fileName)
.setContentText(
getResources().getString(
R.string.uploading_file))
.setOngoing(true)
.setSmallIcon(android.R.drawable.ic_menu_upload);
mBuilder.setProgress(100, 100, true);
mNotifyManager.notify(NOTIFICATION_ID, mBuilder.build());
nofti_appended = true;
}
});
pd.show();
}
@Override
protected HttpResponse doInBackground(HttpResponse... arg0) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext httpContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(urlServer);
try {
Log.i("uploader", "Started " + fileName);
if (isCancelled()) {
og.i("uploader", "uploading has been canceled");
}
CustomMultiPartEntity multipartContent = new CustomMultiPartEntity(
new ProgressListener() {
@Override
public void transferred(long num) {
publishProgress((int) ((num / (float) totalSize) * 100));
Log.i("uploader", "uploading " + num + " OF "
+ totalSize);
}
});
multipartContent.addPart("file", new FileBody(new File(filepath)));
totalSize = multipartContent.getContentLength();
Log.i("uploader", "Bytes to Send " + totalSize);
// Send it
httpPost.setEntity(multipartContent);
HttpResponse response = httpClient.execute(httpPost, httpContext);
serverResponse = EntityUtils.toString(response.getEntity());
Log.i("uploader", "Server Response : " + serverResponse);
return null;
} catch (Exception e) {
Log.i("uploader", "Uploading Error : " + e.getMessage());
}
return null;
}
@Override
protected void onCancelled() {
super.onCancelled();
if (nofti_appended) {
mNotifyManager.cancel(NOTIFICATION_ID);
} else {
pd.dismiss();
}
}
@Override
protected void onProgressUpdate(Integer... progress) {
if (nofti_appended == true) {
mBuilder.setProgress(100, progress[0], false);
} else {
pd.setProgress((int) (progress[0]));
}
}
@SuppressWarnings("deprecation")
@Override
protected void onPostExecute(HttpResponse ui) {
Log.i("uploader", "Done: " + fileName);
}
}
私有类excuteUploadFile扩展
异步任务{
总尺寸长;
int通知ID;
字符串文件路径,文件名;
字符串serverResponse=null;
字符串urlServer=”http://myurl";
通知经理通知经理;
造船厂;
进展性帕金森病;
布尔nofti_=false;
JSONObject jsonfile=null;
公共excuteUploadFile(字符串文件路径,字符串文件名){
this.filepath=filepath;
this.fileName=文件名;
Log.i(“上传程序”,“所选文件名:”+文件名);
Log.i(“上传程序”,“所选文件路径:“+filepath”);
}
@凌驾
受保护的void onPreExecute(){
pd=新进度对话框(BabupMain.this);
//pd.setMessage(getResources().getString(R.string.uploading_文件));
pd.setTitle(文件名);
pd.setProgressStyle(ProgressDialog.STYLE_水平);
pd.可设置可取消(假);
pd.setIcon(android.R.drawable.ic_菜单_上传);
pd.设置按钮(DialogInterface.BUTTON_负极,
getResources().getString(R.string.cancel),
新建DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
dialog.dismise();
取消(真);
}
});
//隐藏按钮
pd.设置按钮(DialogInterface.BUTTON_空档,
getResources().getString(R.string.hide),
新建DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
dialog.dismise();
随机r=新随机();
通知ID=r.nextInt(80-65)+1;
通知_ID++;
mNotifyManager=(NotificationManager)getSystemService(Context.NOTIFICATION\u服务);
mBuilder=新通知compat.Builder(
巴布缅;
mBuilder.setContentTitle(文件名)
.setContentText(
getResources().getString(
R.string.uploading_文件)
.正在进行(正确)
.setSmallIcon(android.R.drawable.ic_菜单_上传);
mBuilder.setProgress(100100,true);
mNotifyManager.notify(NOTIFICATION_ID,mBuilder.build());
nofti_=真;
}
});
pd.show();
}
@凌驾
受保护的HttpResponse doInBackground(HttpResponse…arg0){
HttpClient HttpClient=新的DefaultHttpClient();
HttpContext HttpContext=新的BasicHttpContext();
HttpPost HttpPost=新的HttpPost(urlServer);
试一试{
Log.i(“上传器”、“启动”+文件名);
如果(isCancelled()){
og.i(“上传器”,“上传已被取消”);
}
CustomMultiPartEntity multipartContent=新CustomMultiPartEntity(
新ProgressListener(){
@凌驾
已转移的公共无效(长数值){
出版进度((int)((num/(float)totalSize)*100);
Log.i(“上传器”,“上传”+num+“OF”
+总尺寸);
}
});
addPart(“文件”,新文件体(新文件(文件路径));
totalSize=multipartContent.getContentLength();
Log.i(“上传器”、“要发送的字节数”+总大小);
//发送它
setEntity(multipartContent);
HttpResponse response=httpClient.execute(httpPost,httpContext);
serverResponse=EntityUtils.toString(response.getEntity());
Log.i(“上传程序”,“服务器响应:”+serverResponse);
返回null;
}捕获(例外e){
Log.i(“上传器”,“上传错误:+e.getMessage());
}
返回null;
}
@凌驾
受保护的void onCancelled(){
super.onCancelled();
如果(附加nofti_){
mNotifyManager.cancel(通知ID);
}否则{
pd.解散();
}
}
@凌驾
受保护的void onProgressUpdate(整数…进度){
if(nofti_added==true){
mBuilder.setProgress(100,进度[0],false);
}否则{
pd.setProgress((int)(progress[0]);
}
}
@抑制警告(“弃用”)
@凌驾
受保护的void onPostExecute(HttpResponse ui){
Log.i(“上传程序”,“完成:”+文件名);
}
}
可能是因为您没有设置最大值<代码>pd.setMax(100)代码>,您可能还需要pd.setUndeterminate(false)代码>在仍然无法修改同一问题通知进度条之前,我已经尝试过此操作