该项目的目的是在将数据库上传到webService时运行进度栏。
我有一个做一些工作的AsyncTask,然后调用服务并等待其完成,然后再调用onPostExecute()
private class UploadDatabaseAsyncTask extends AsyncTask<Void, Long, Void> {
private AlertDialog dialog;
private Activity activity;
private TextView messageView;
private String location;
private long maxSize;
private ProgressBar progressBar;
UploadDatabaseAsyncTask(Activity activity, long maxSize, String location) {
super();
this.location = location;
this.maxSize = maxSize;
this.activity = activity;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar = new ProgressBar(activity);
progressBar.setMax(100);
progressBar.setProgress(0);
dialog = new AlertDialog.Builder(activity).setTitle("Uploading").setView(progressBar).create();
messageView = dialog.findViewById(android.R.id.message);
// dialog.show();
if (activity != null && activity.getView() != null)
{
activity.getView().setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
activity.getView().setBackgroundColor(ContextCompat.getColor(App.getAppContext(), R.color.grey));
}
}
@Override
protected void onProgressUpdate(Long... values) {
messageView.setText(values[0] + " / " + maxSize);
BigDecimal current = new BigDecimal(values[0]);
BigDecimal max = new BigDecimal(maxSize);
BigDecimal progress = current.divide(max, 3, RoundingMode.UP);
progressBar.setProgress(Integer.valueOf(progress.multiply(new BigDecimal(100)).toString()));
}
@Override
protected Void doInBackground(Void... voids) {
Intent intent = new Intent();
final Object lock = new Object();
final boolean[] active = new boolean[]{true};
DatabaseSyncService.SyncProgressCallback callback = new DatabaseSyncService.SyncProgressCallback() {
@Override
public void onProgress(long progress) {
onProgressUpdate(progress);
}
@Override
public void onFinish() {
active[0] = false;
lock.notify();
}
};
intent.putExtra(DATABASE_ID, callback);
intent.putExtra("LOCATION", location);
DatabaseSyncService.enqueueWork(activity, intent);
synchronized (lock)
{
while (active[0])
{
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
return null;
}
@Override
protected void onCancelled(Void aVoid) {
if (activity != null && activity.getView() != null)
{
activity.getView().setOnTouchListener(null);
activity.getView().setBackgroundColor(ContextCompat.getColor(App.getAppContext(), R.color.transparent));
}
}
@Override
protected void onCancelled() {
if (activity != null && activity.getView() != null)
{
activity.getView().setOnTouchListener(null);
activity.getView().setBackgroundColor(ContextCompat.getColor(App.getAppContext(), R.color.transparent));
}
}
@Override
protected void onPostExecute(Void aVoid) {
if (activity != null && activity.getView() != null)
{
activity.getView().setOnTouchListener(null);
activity.getView().setBackgroundColor(ContextCompat.getColor(App.getAppContext(), R.color.transparent))
}
AlertDialog.Builder builder = new AlertDialog.Builder(App.getAppContext());
builder.setTitle("Upload Complete");
builder.setMessage("You upload was completed!");
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) { }
});
builder.show();
}
}
我在尝试序列化AsyncTask时遇到DatabaseSyncService.enqueueWork()方法的问题,该方法无法正常工作。知道为什么要这样做吗?
StackTrace
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #4
Process: com.app.app, PID: 15935
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:353)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.app.app.api.SettingApi$UploadDatabaseAsyncTask$2)
at android.os.Parcel.writeSerializable(Parcel.java:1786)
at android.os.Parcel.writeValue(Parcel.java:1734)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:801)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1506)
at android.os.Bundle.writeToParcel(Bundle.java:1181)
at android.os.Parcel.writeBundle(Parcel.java:841)
at android.content.Intent.writeToParcel(Intent.java:10199)
at android.app.job.JobWorkItem.writeToParcel(JobWorkItem.java:117)
at android.app.job.IJobScheduler$Stub$Proxy.enqueue(IJobScheduler.java:205)
at android.app.JobSchedulerImpl.enqueue(JobSchedulerImpl.java:53)
at androidx.core.app.JobIntentService$JobWorkEnqueuer.enqueueWork(JobIntentService.java:343)
at androidx.core.app.JobIntentService.enqueueWork(JobIntentService.java:523)
at androidx.core.app.JobIntentService.enqueueWork(JobIntentService.java:501)
at com.ascsoftware.ascora.sync.DatabaseSyncService.enqueueWork(DatabaseSyncService.java:37)
at com.ascsoftware.ascora.api.SettingApi$UploadDatabaseAsyncTask.doInBackground(SettingApi.java:291)
at com.ascsoftware.ascora.api.SettingApi$UploadDatabaseAsyncTask.doInBackground(SettingApi.java:212)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.io.NotSerializableException: com.app.app.api.SettingApi$UploadDatabaseAsyncTask
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1233)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1597)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1558)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1481)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1227)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
at android.os.Parcel.writeSerializable(Parcel.java:1781)
at android.os.Parcel.writeValue(Parcel.java:1734)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:801)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1506)
at android.os.Bundle.writeToParcel(Bundle.java:1181)
at android.os.Parcel.writeBundle(Parcel.java:841)
at android.content.Intent.writeToParcel(Intent.java:10199)
at android.app.job.JobWorkItem.writeToParcel(JobWorkItem.java:117)
at android.app.job.IJobScheduler$Stub$Proxy.enqueue(IJobScheduler.java:205)
at android.app.JobSchedulerImpl.enqueue(JobSchedulerImpl.java:53)
at androidx.core.app.JobIntentService$JobWorkEnqueuer.enqueueWork(JobIntentService.java:343)
at androidx.core.app.JobIntentService.enqueueWork(JobIntentService.java:523)
at androidx.core.app.JobIntentService.enqueueWork(JobIntentService.java:501)
at com.app.app.sync.DatabaseSyncService.enqueueWork(DatabaseSyncService.java:37)
at com.app.app.api.SettingApi$UploadDatabaseAsyncTask.doInBackground(SettingApi.java:291)
at com.app.app.api.SettingApi$UploadDatabaseAsyncTask.doInBackground(SettingApi.java:212)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
(HTTPLog)-Static: isSBSettingEnabled false
这是回调
public interface SyncProgressCallback extends Serializable {
void onProgress(long progress);
void onFinish();
}
答案 0 :(得分:1)
此行不起作用:
intent.putExtra(DATABASE_ID, callback);
要将对象放入Intent
,该对象必须实现Serializable
/ Parcelable
接口。此外,相同的要求适用于对象的所有字段。
尽管您的callback
对象(属于类型DatabaseSyncService.SyncProgressCallback
)实现了Serializable
,但是它是从内部类callback = new DatabaseSyncService.SyncProgressCallback() {...}
实例化的。根据{{3}},在您的情况下发生java.io.NotSerializableException
是因为:
- 序列化这样的内部类实例也会导致其关联的外部类实例的序列化
- 强烈反对对内部类(即不是静态成员类的嵌套类)进行序列化,包括本地类和匿名类
这意味着您必须使外部类UploadDatabaseAsyncTask
实现Serializable
,而该类又必须应用于其所有字段,依此类推。
那真令人沮丧。
根据经验,请考虑将轻量/纯数据(而不是具有复杂依赖项的复杂对象)放入Intent
。
回到您的代码,一个简单的解决方法是将callback
对象传递给DatabaseSyncService.enqueueWork
方法:
//intent.putExtra(DATABASE_ID, callback);
intent.putExtra("LOCATION", location);
DatabaseSyncService.enqueueWork(activity, intent, callback);
在enqueueWork()
内部,直接使用callback
,而不是从意图中进行反序列化。