- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我已经查看过有关此问题的所有类似线程,但找不到解决我的问题的方法
我的 AsynkTask 应该从保管箱下载文件并在通知进度条中显示进度。 doInBackground 下载文件,但方法 publishProgress/onProgressUpdate 无法正常工作。 onProgressUpdate 未被调用。
异步任务
public class SongDownload extends AsyncTask<String, String, String> {
DropboxAPI<AndroidAuthSession> dDBApi;
Context context;
Dropbox db;
SharedPreferences prefs;
NotificationManager mNotifyManager;
Notification notification;
String datei;
public SongDownload(Context context, Dropbox db) {
this.db = db;
this.context = context;
Log.v("mp3dropboxsync", "SongDownload");
mNotifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "Neuer DropBox Song!", System.currentTimeMillis());
RemoteViews views = new RemoteViews(context.getApplicationContext()
.getPackageName(), R.layout.notiprogressbar);
views.setProgressBar(R.id.progressBar1, 100, 0, false);
views.setTextViewText(R.id.status_text, "Song Downloading...");
notification.contentView = views;
notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT;
mNotifyManager.notify(10, notification);
}
@Override
protected String doInBackground(String... dropboxFile) {
Log.v("mp3dropboxsync", "DoinBackground");
File file = new File(Environment.getExternalStorageDirectory().getPath()+"/MP3/sonstiges/"+dropboxFile[0]);
datei = dropboxFile[0];
BufferedOutputStream out=null;
try {
if(!file.exists())
{
out = new BufferedOutputStream(new FileOutputStream(file));
db.mDBApi.getFile("/MP3/"+dropboxFile[0], null, out, new ProgressListener() {
@Override
public void onProgress(long bytes, long total) {
try {
Log.v("mp3dropboxsync", "onProgress");
String s = ""+(bytes/(float)total)*100;
publishProgress(s);
} catch (Exception e) {
// TODO Auto-generated catch block
Log.v("mp3dropboxsync", "onProgressERROR");
}
}
});
}
} catch (DropboxException e) {
Log.v("mp3dropboxsync", "Something went wrong while downloading.");
file.delete();
} catch (FileNotFoundException e) {
Log.v("mp3dropboxsync", "File not found.");
} catch (Exception e) {
Log.v("mp3dropboxsync", "Unbekannter Fehler: "+e.getMessage());
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
dropboxFile[0] = "";
}
return dropboxFile[0];
}
@Override
protected void onProgressUpdate(String... progress) {
super.onProgressUpdate(progress);
try {
int percent = Integer.parseInt(progress[0]);
Log.v("mp3dropboxsync","Hi progressing - " + percent + "%");
notification.contentView.setProgressBar(R.id.progressBar1, 100, percent, false);
notification.contentView.setTextViewText(R.id.status_text, "Downloading... "+datei);
mNotifyManager.notify(10, notification);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Log.v("mp3dropboxsync","OnPostExecute");
mNotifyManager.cancel(10);
}
}
日志
08-07 15:14:54.716: I/System.out(29125): waiting for debugger to settle...
08-07 15:14:54.926: I/System.out(29125): debugger has settled (1366)
08-07 15:14:55.496: D/libEGL(29125): loaded /system/lib/egl/libGLES_android.so
08-07 15:14:55.527: D/libEGL(29125): loaded /system/lib/egl/libEGL_adreno200.so
08-07 15:14:55.557: D/libEGL(29125): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
08-07 15:14:55.567: D/libEGL(29125): loaded /system/lib/egl/libGLESv2_adreno200.so
08-07 15:14:55.577: V/mp3dropboxsync(29125): OnStartCommand
08-07 15:14:55.577: V/mp3dropboxsync(29125): In Auth
08-07 15:14:55.577: V/mp3dropboxsync(29125): Nach Auth - linked: true
08-07 15:14:55.577: V/mp3dropboxsync(29125): Dropbox GetListe
08-07 15:14:55.677: I/Adreno200-EGLSUB(29125): <ConfigWindowMatch:2078>: Format RGBA_8888.
08-07 15:14:55.717: D/memalloc(29125): ashmem: Mapped buffer base:0x5266b000 size:1536000 fd:63
08-07 15:14:55.727: D/OpenGLRenderer(29125): Enabling debug mode 0
08-07 15:14:55.967: I/global(29125): In close() at SocketHttpClientConnection
08-07 15:14:55.987: D/memalloc(29125): ashmem: Mapped buffer base:0x52e0c000 size:1536000 fd:67
08-07 15:15:03.464: V/mp3dropboxsync(29125): SongDownload
08-07 15:15:03.484: V/mp3dropboxsync(29125): DoinBackground
08-07 15:15:03.624: I/global(29125): In close() at SocketHttpClientConnection
08-07 15:15:06.517: V/mp3dropboxsync(29125): onProgress
08-07 15:15:07.108: V/mp3dropboxsync(29125): onProgress
08-07 15:15:07.628: V/mp3dropboxsync(29125): onProgress
08-07 15:15:08.149: V/mp3dropboxsync(29125): onProgress
08-07 15:15:08.659: V/mp3dropboxsync(29125): onProgress
08-07 15:15:09.200: V/mp3dropboxsync(29125): onProgress
08-07 15:15:09.740: V/mp3dropboxsync(29125): onProgress
08-07 15:15:10.261: V/mp3dropboxsync(29125): onProgress
08-07 15:15:10.801: V/mp3dropboxsync(29125): onProgress
08-07 15:15:11.402: V/mp3dropboxsync(29125): onProgress
08-07 15:15:11.943: V/mp3dropboxsync(29125): onProgress
08-07 15:15:12.463: V/mp3dropboxsync(29125): onProgress
08-07 15:15:12.974: V/mp3dropboxsync(29125): onProgress
08-07 15:15:13.494: V/mp3dropboxsync(29125): onProgress
08-07 15:15:14.005: V/mp3dropboxsync(29125): onProgress
08-07 15:15:14.585: V/mp3dropboxsync(29125): onProgress
08-07 15:15:15.146: V/mp3dropboxsync(29125): onProgress
08-07 15:15:15.696: V/mp3dropboxsync(29125): onProgress
08-07 15:15:16.247: V/mp3dropboxsync(29125): onProgress
08-07 15:15:16.847: V/mp3dropboxsync(29125): onProgress
08-07 15:15:17.398: V/mp3dropboxsync(29125): onProgress
08-07 15:15:17.928: V/mp3dropboxsync(29125): onProgress
08-07 15:15:18.439: V/mp3dropboxsync(29125): onProgress
08-07 15:15:19.009: V/mp3dropboxsync(29125): onProgress
08-07 15:15:19.550: V/mp3dropboxsync(29125): onProgress
08-07 15:15:20.090: V/mp3dropboxsync(29125): onProgress
08-07 15:15:20.621: V/mp3dropboxsync(29125): onProgress
08-07 15:15:21.242: V/mp3dropboxsync(29125): onProgress
08-07 15:15:21.742: V/mp3dropboxsync(29125): onProgress
08-07 15:15:22.283: V/mp3dropboxsync(29125): onProgress
08-07 15:15:22.793: V/mp3dropboxsync(29125): onProgress
08-07 15:15:23.304: V/mp3dropboxsync(29125): onProgress
08-07 15:15:23.954: V/mp3dropboxsync(29125): onProgress
08-07 15:15:24.545: V/mp3dropboxsync(29125): onProgress
08-07 15:15:25.075: V/mp3dropboxsync(29125): onProgress
08-07 15:15:25.606: V/mp3dropboxsync(29125): onProgress
08-07 15:15:25.936: I/global(29125): In close() at SocketHttpClientConnection
08-07 15:15:25.936: I/global(29125): call socket close in SocketHttpClientConnection, socket=SSL socket over Socket[address=/199.47.219.158,port=443,localPort=36480]
08-07 15:15:25.976: I/global(29125): I/O error closing connection
08-07 15:15:25.976: I/global(29125): java.net.SocketException: Socket is closed
08-07 15:15:25.976: I/global(29125): at java.net.Socket.checkOpenAndCreate(Socket.java:671)
08-07 15:15:25.976: I/global(29125): at java.net.Socket.getSoLinger(Socket.java:434)
08-07 15:15:25.976: I/global(29125): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImplWrapper.getSoLinger(OpenSSLSocketImplWrapper.java:156)
08-07 15:15:25.976: I/global(29125): at org.apache.http.impl.conn.tsccm.AbstractConnPool.closeConnection(AbstractConnPool.java:327)
08-07 15:15:25.976: I/global(29125): at org.apache.http.impl.conn.tsccm.ConnPoolByRoute.deleteEntry(ConnPoolByRoute.java:530)
08-07 15:15:25.976: I/global(29125): at org.apache.http.impl.conn.tsccm.ConnPoolByRoute.deleteClosedConnections(ConnPoolByRoute.java:653)
08-07 15:15:25.976: I/global(29125): at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager.closeExpiredConnections(ThreadSafeClientConnManager.java:301)
08-07 15:15:25.976: I/global(29125): at com.dropbox.client2.session.AbstractSession$IdleConnectionCloserThread.run(AbstractSession.java:530)
08-07 15:15:25.996: W/IdleConnectionHandler(29125): Removing a connection that never existed!
08-07 15:15:26.116: V/mp3dropboxsync(29125): onProgress
08-07 15:15:26.687: V/mp3dropboxsync(29125): onProgress
08-07 15:15:27.197: V/mp3dropboxsync(29125): onProgress
08-07 15:15:27.758: V/mp3dropboxsync(29125): onProgress
08-07 15:15:28.258: V/mp3dropboxsync(29125): onProgress
08-07 15:15:29.019: V/mp3dropboxsync(29125): onProgress
08-07 15:15:29.430: V/mp3dropboxsync(29125): SongDownload
08-07 15:15:29.700: V/mp3dropboxsync(29125): DoinBackground
08-07 15:15:32.272: V/mp3dropboxsync(29125): onProgress
08-07 15:15:32.773: V/mp3dropboxsync(29125): onProgress
08-07 15:15:33.283: V/mp3dropboxsync(29125): onProgress
08-07 15:15:33.784: V/mp3dropboxsync(29125): onProgress
08-07 15:15:34.304: V/mp3dropboxsync(29125): onProgress
08-07 15:15:34.835: V/mp3dropboxsync(29125): onProgress
08-07 15:15:35.345: V/mp3dropboxsync(29125): onProgress
08-07 15:15:35.866: V/mp3dropboxsync(29125): onProgress
08-07 15:15:36.466: V/mp3dropboxsync(29125): onProgress
08-07 15:15:36.967: V/mp3dropboxsync(29125): onProgress
08-07 15:15:37.487: V/mp3dropboxsync(29125): onProgress
08-07 15:15:38.068: V/mp3dropboxsync(29125): onProgress
08-07 15:15:38.569: V/mp3dropboxsync(29125): onProgress
08-07 15:15:39.079: V/mp3dropboxsync(29125): onProgress
08-07 15:15:39.580: V/mp3dropboxsync(29125): onProgress
08-07 15:15:40.150: V/mp3dropboxsync(29125): onProgress
08-07 15:15:40.751: V/mp3dropboxsync(29125): onProgress
08-07 15:15:41.311: V/mp3dropboxsync(29125): onProgress
08-07 15:15:41.822: V/mp3dropboxsync(29125): onProgress
08-07 15:15:42.402: V/mp3dropboxsync(29125): onProgress
08-07 15:15:42.903: V/mp3dropboxsync(29125): onProgress
08-07 15:15:43.453: V/mp3dropboxsync(29125): onProgress
08-07 15:15:43.954: V/mp3dropboxsync(29125): onProgress
08-07 15:15:44.464: V/mp3dropboxsync(29125): onProgress
08-07 15:15:44.985: V/mp3dropboxsync(29125): onProgress
08-07 15:15:45.485: V/mp3dropboxsync(29125): onProgress
08-07 15:15:45.996: V/mp3dropboxsync(29125): onProgress
08-07 15:15:46.496: V/mp3dropboxsync(29125): onProgress
08-07 15:15:47.007: V/mp3dropboxsync(29125): onProgress
08-07 15:15:47.567: V/mp3dropboxsync(29125): onProgress
08-07 15:15:48.258: V/mp3dropboxsync(29125): onProgress
08-07 15:15:48.839: V/mp3dropboxsync(29125): onProgress
08-07 15:15:49.439: V/mp3dropboxsync(29125): onProgress
08-07 15:15:49.990: V/mp3dropboxsync(29125): onProgress
08-07 15:15:50.210: V/mp3dropboxsync(29125): SongDownload
08-07 15:15:50.240: V/mp3dropboxsync(29125): DoinBackground //file already exist
我什么都试过了。还有一些其他效果,例如在 DoinBackground 完成后多次调用 onProgressUpdate。但目前它没有被调用。我也无法解释套接字关闭异常。 =/
希望你能帮助我。我已经尝试重新制作整个项目并重新安装。但它没有用。
谢谢!
最佳答案
请尝试使用我的 POC,它对你有帮助。
公共(public)类 MainActivity 扩展 Activity { 处理器 handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new BackGroundTask().execute("");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class BackGroundTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
Thread t = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
publishProgress("" + i);
}
}
});
t.start();
return null;
}
@Override
protected void onProgressUpdate(String... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("T:" + values[0]);
}
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
}
关于android - AsyncTask publishProgress 不要调用 onProgressUpdate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18104534/
为了让我的代码几乎完全用 Jquery 编写,我想用 Jquery 重写 AJAX 调用。 这是从网页到 Tomcat servlet 的调用。 我目前情况的类似代码: var http = new
我想使用 JNI 从 Java 调用 C 函数。在 C 函数中,我想创建一个 JVM 并调用一些 Java 对象。当我尝试创建 JVM 时,JNI_CreateJavaVM 返回 -1。 所以,我想知
环顾四周,我发现从 HTML 调用 Javascript 函数的最佳方法是将函数本身放在 HTML 中,而不是外部 Javascript 文件。所以我一直在网上四处寻找,找到了一些简短的教程,我可以根
我有这个组件: import {Component} from 'angular2/core'; import {UserServices} from '../services/UserService
我正在尝试用 C 实现一个简单的 OpenSSL 客户端/服务器模型,并且对 BIO_* 调用的使用感到好奇,与原始 SSL_* 调用相比,它允许一些不错的功能。 我对此比较陌生,所以我可能会完全错误
我正在处理有关异步调用的难题: 一个 JQuery 函数在用户点击时执行,然后调用一个 php 文件来检查用户输入是否与数据库中已有的信息重叠。如果是这样,则应提示用户确认是否要继续或取消,如果他单击
我有以下类(class)。 public Task { public static Task getInstance(String taskName) { return new
嘿,我正在构建一个小游戏,我正在通过制作一个数字 vector 来创建关卡,该数字 vector 通过枚举与 1-4 种颜色相关联。问题是循环(在 Simon::loadChallenge 中)我将颜
我有一个java spring boot api(数据接收器),客户端调用它来保存一些数据。一旦我完成了数据的持久化,我想进行另一个 api 调用(应该处理持久化的数据 - 数据聚合器),它应该自行异
首先,这涉及桌面应用程序而不是 ASP .Net 应用程序。 我已经为我的项目添加了一个 Web 引用,并构建了各种数据对象,例如 PayerInfo、Address 和 CreditCard。但问题
我如何告诉 FAKE 编译 .fs文件使用 fsc ? 解释如何传递参数的奖励积分,如 -a和 -target:dll . 编辑:我应该澄清一下,我正在尝试在没有 MSBuild/xbuild/.sl
我使用下划线模板配置了一个简单的主干模型和 View 。两个单独的 API 使用完全相同的配置。 API 1 按预期工作。 要重现该问题,请注释掉 API 1 的 URL,并取消注释 API 2 的
我不确定什么是更好的做法或更现实的做法。我希望从头开始创建目录系统,但不确定最佳方法是什么。 我想我在需要显示信息时使用对象,例如 info.php?id=100。有这样的代码用于显示 Game.cl
from datetime import timedelta class A: def __abs__(self): return -self class B1(A):
我在操作此生命游戏示例代码中的数组时遇到问题。 情况: “生命游戏”是约翰·康威发明的一种细胞自动化技术。它由一个细胞网格组成,这些细胞可以根据数学规则生存/死亡/繁殖。该网格中的活细胞和死细胞通过
如果我像这样调用 read() 来读取文件: unsigned char buf[512]; memset(buf, 0, sizeof(unsigned char) * 512); int fd;
我用 C 编写了一个简单的服务器,并希望调用它的功能与调用其他 C 守护程序的功能相同(例如使用 ./ftpd start 调用它并使用 ./ftpd stop 关闭该实例)。显然我遇到的问题是我不知
在 dos 中,当我粘贴此命令时它会起作用: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://google.
在 dos 中,当我粘贴此命令时它会起作用: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://google.
我希望能够从 cmd 在我的 Windows 10 计算机上调用 python3。 我已重新安装 Python3.7 以确保选择“添加到路径”选项,但仍无法调用 python3 并使 CMD 启动 P
我是一名优秀的程序员,十分优秀!