BackgroundWorker

R

RED-BARON

Fortgeschrittenes Mitglied
19
Hallo Forum,

manchmal möchte man verschiedene Aufgaben synchron
oder asynchron durchführen, wobei die GUI nicht blockiert
werden soll.

Ich möchte meine Lösung basieren auf der Thread-Klasse
Euch vorstellen und gern auch diskutieren.

Mit BackgroundWorker.start() wird ein "Prozess" oder "Prozessliste"
asynchron zum GUI-Thread ausgeführt.

Mit BackgroundWorker.startasync() wird eine "Prozessliste" asynchron
und wiederum asynchron zum GUI-Thread ausgeführt.

Und nach jeder Änderung des Beitrages ist der Code wieder völlig ver-formatiert :( sorry

BackgroundWorker.java
Code:
[B][COLOR=#7f0055][LEFT]public[/B][/COLOR] [B][COLOR=#7f0055]class[/B][/COLOR] BackgroundWorker [B][COLOR=#7f0055]extends[/B][/COLOR] Thread {

[B][COLOR=#7f0055]private[/B][/COLOR] [B][COLOR=#7f0055]boolean[/B][/COLOR] [COLOR=#0000c0]exception[/COLOR] = [B][COLOR=#7f0055]false[/B][/COLOR];
[B][COLOR=#7f0055]private[/B][/COLOR] BackgroundAsyncHelper [COLOR=#0000c0]backgroundAsyncHelper[/COLOR] = [B][COLOR=#7f0055]null[/B][/COLOR]; 
[B][COLOR=#7f0055]protected[/B][/COLOR] BackgroundWorkerEvents [COLOR=#0000c0]eventSink[/COLOR];
[B][COLOR=#7f0055]protected[/B][/COLOR] List<BackgroundProcess> [COLOR=#0000c0]processList[/COLOR] = [B][COLOR=#7f0055]new[/B][/COLOR] ArrayList<BackgroundProcess>();


[B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]interface[/B][/COLOR] BackgroundWorkerEvents { 
[B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]void[/B][/COLOR] onProcessProgress([B][COLOR=#7f0055]final[/B][/COLOR] BackgroundProcess process, String message, Integer progress);
[B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]void[/B][/COLOR] onProcessFinish(BackgroundWorker worker, BackgroundProcess process, Exception e);
[B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]void[/B][/COLOR] onProcessListFinish(BackgroundWorker worker, List<BackgroundProcess> processList, Exception e); 
[B][COLOR=#7f0055]public[/B][/COLOR] Handler getMessageQueue(); [COLOR=#3f7f5f]// return getWindow().getDecorView().getHandler();[/LEFT]
[/COLOR][LEFT]}

[B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]interface[/B][/COLOR] BackgroundProcess {
[B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]void[/B][/COLOR] run(BackgroundWorker worker, BackgroundProcess process) [B][COLOR=#7f0055]throws[/B][/COLOR] Exception;
[B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]int[/B][/COLOR] getPrgogessId();
}

[B][COLOR=#7f0055]private[/B][/COLOR] [B][COLOR=#7f0055]class[/B][/COLOR] BackgroundAsyncHelper [B][COLOR=#7f0055]extends[/B][/COLOR] BackgroundWorker {

[B][COLOR=#7f0055]private[/B][/COLOR] List<BackgroundWorker> [COLOR=#0000c0]workerList[/COLOR] = [B][COLOR=#7f0055]new[/B][/COLOR] ArrayList<BackgroundWorker>();

[B][COLOR=#7f0055]public[/B][/COLOR] BackgroundAsyncHelper(BackgroundWorkerEvents eventSink, List<BackgroundProcess> processList) {
[B][COLOR=#7f0055]super[/B][/COLOR](eventSink, processList);
}

[B][COLOR=#7f0055]private[/B][/COLOR] List<BackgroundWorker> getBackgroundWorkerList()
{
[B][COLOR=#7f0055]return[/B][/COLOR] [B][COLOR=#7f0055]this[/B][/COLOR].[COLOR=#0000c0]workerList[/COLOR];
}

[COLOR=#646464]@Override[/LEFT]
[/COLOR][LEFT][B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]void[/B][/COLOR] run () {

[B][COLOR=#7f0055]try[/B][/COLOR] {

[B][COLOR=#7f0055]final[/B][/COLOR] BackgroundWorker frameWorker = [B][COLOR=#7f0055]this[/B][/COLOR];

[B][COLOR=#7f0055]for[/B][/COLOR](BackgroundProcess process : [COLOR=#0000c0]processList[/COLOR]) {
[COLOR=#0000c0]workerList[/COLOR].add([B][COLOR=#7f0055]new[/B][/COLOR] BackgroundWorker([B][COLOR=#7f0055]this[/B][/COLOR].[COLOR=#0000c0]eventSink[/COLOR], process));
}

[B][COLOR=#7f0055]for[/B][/COLOR](BackgroundWorker worker : [COLOR=#0000c0]workerList[/COLOR]) { 
worker.start();
}

[B][COLOR=#7f0055]for[/B][/COLOR]([B][COLOR=#7f0055]final[/B][/COLOR] BackgroundWorker worker : [COLOR=#0000c0]workerList[/COLOR]) {

[B][COLOR=#7f0055]try[/B][/COLOR] {
worker.join();
} 
[B][COLOR=#7f0055]catch[/B][/COLOR] ([B][COLOR=#7f0055]final[/B][/COLOR] InterruptedException e) {
[COLOR=#3f7f5f]// [/COLOR][B][COLOR=#7f9fbf]TODO[/B][/COLOR][COLOR=#3f7f5f] Auto-generated catch block[/LEFT]
[/COLOR][LEFT]getMessageQueue().post([B][COLOR=#7f0055]new[/B][/COLOR] Runnable()
{
[COLOR=#646464]@Override[/LEFT]
[/COLOR][LEFT][B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]void[/B][/COLOR] run() {
[COLOR=#3f7f5f]// [/COLOR][B][COLOR=#7f9fbf]TODO[/B][/COLOR][COLOR=#3f7f5f] Auto-generated method stub[/LEFT]
[/COLOR][LEFT][COLOR=#0000c0]eventSink[/COLOR].onProcessFinish(frameWorker, worker.getBackgroundProcess(), e);
}
});
Thread.[I]currentThread[/I]().interrupt();
}
}
[B][COLOR=#7f0055]if[/B][/COLOR]( ![B][COLOR=#7f0055]this[/B][/COLOR].isInterrupted() && [COLOR=#0000c0]workerList[/COLOR].size() > 1 )
{
[B][COLOR=#7f0055]for[/B][/COLOR](BackgroundWorker worker : [COLOR=#0000c0]workerList[/COLOR])
{
[B][COLOR=#7f0055]if[/B][/COLOR]( worker.isException() )
{
[B][COLOR=#7f0055]return[/B][/COLOR];
}
}

getMessageQueue().post([B][COLOR=#7f0055]new[/B][/COLOR] Runnable()
{
[COLOR=#646464]@Override[/LEFT]
[/COLOR][LEFT][B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]void[/B][/COLOR] run() {
[COLOR=#3f7f5f]// [/COLOR][B][COLOR=#7f9fbf]TODO[/B][/COLOR][COLOR=#3f7f5f] Auto-generated method stub[/LEFT]
[/COLOR][LEFT][COLOR=#0000c0]eventSink[/COLOR].onProcessListFinish(frameWorker, [COLOR=#0000c0]processList[/COLOR], [B][COLOR=#7f0055]null[/B][/COLOR]);
}
});
}
}
[B][COLOR=#7f0055]catch[/B][/COLOR](Exception e)
{
e.printStackTrace();
}
}
}


[B][COLOR=#7f0055]public[/B][/COLOR] BackgroundWorker(BackgroundWorkerEvents eventSink, BackgroundProcess process)
{
[B][COLOR=#7f0055]this[/B][/COLOR].[COLOR=#0000c0]eventSink[/COLOR] = eventSink;
[B][COLOR=#7f0055]this[/B][/COLOR].[COLOR=#0000c0]processList[/COLOR].add(process);
}
[B][COLOR=#7f0055]public[/B][/COLOR] BackgroundWorker(BackgroundWorkerEvents eventSink, List<BackgroundProcess> processList)
{
[B][COLOR=#7f0055]this[/B][/COLOR].[COLOR=#0000c0]eventSink[/COLOR] = eventSink;
[B][COLOR=#7f0055]this[/B][/COLOR].[COLOR=#0000c0]processList[/COLOR] = processList;
}

[B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]synchronized[/B][/COLOR] [B][COLOR=#7f0055]void[/B][/COLOR] startasync()
{
[COLOR=#0000c0]backgroundAsyncHelper[/COLOR] = [B][COLOR=#7f0055]new[/B][/COLOR] BackgroundAsyncHelper([B][COLOR=#7f0055]this[/B][/COLOR].[COLOR=#0000c0]eventSink[/COLOR], [COLOR=#0000c0]processList[/COLOR]);
[COLOR=#0000c0]backgroundAsyncHelper[/COLOR].start();
}

[B][COLOR=#7f0055]protected[/B][/COLOR] BackgroundProcess getBackgroundProcess()
{
[B][COLOR=#7f0055]return[/B][/COLOR] [B][COLOR=#7f0055]this[/B][/COLOR].[COLOR=#0000c0]processList[/COLOR].get(0);
}

[B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]boolean[/B][/COLOR] isDone() {
[B][COLOR=#7f0055]boolean[/B][/COLOR] isAlive = [B][COLOR=#7f0055]false[/B][/COLOR]; 
[B][COLOR=#7f0055]if[/B][/COLOR]( [COLOR=#0000c0]backgroundAsyncHelper[/COLOR] != [B][COLOR=#7f0055]null[/B][/COLOR] ) {
[B][COLOR=#7f0055]for[/B][/COLOR](BackgroundWorker worker : [COLOR=#0000c0]backgroundAsyncHelper[/COLOR].getBackgroundWorkerList()) { 
isAlive |= worker.isAlive(); 
}
isAlive |= [COLOR=#0000c0]backgroundAsyncHelper[/COLOR].isAlive();
} 
isAlive |= [B][COLOR=#7f0055]this[/B][/COLOR].isAlive(); 
[B][COLOR=#7f0055]return[/B][/COLOR] !isAlive;
}

[COLOR=#646464]@Override[/LEFT]
[/COLOR][LEFT][B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]void[/B][/COLOR] interrupt()
{
[B][COLOR=#7f0055]if[/B][/COLOR]( [COLOR=#0000c0]backgroundAsyncHelper[/COLOR] != [B][COLOR=#7f0055]null[/B][/COLOR] ) {
[B][COLOR=#7f0055]for[/B][/COLOR](BackgroundWorker worker : [COLOR=#0000c0]backgroundAsyncHelper[/COLOR].getBackgroundWorkerList()) { 
worker.interrupt();
} 
[COLOR=#0000c0]backgroundAsyncHelper[/COLOR].interrupt(); 
}
[B][COLOR=#7f0055]super[/B][/COLOR].interrupt();
}

[COLOR=#646464]@Override[/LEFT]
[/COLOR][LEFT][B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]void[/B][/COLOR] run ()
{ 
[B][COLOR=#7f0055]final[/B][/COLOR] BackgroundWorker worker = [B][COLOR=#7f0055]this[/B][/COLOR];

[B][COLOR=#7f0055]for[/B][/COLOR]([B][COLOR=#7f0055]final[/B][/COLOR] BackgroundProcess process : [COLOR=#0000c0]processList[/COLOR]) 
{ 
[B][COLOR=#7f0055]if[/B][/COLOR]( ![B][COLOR=#7f0055]this[/B][/COLOR].isInterrupted() )
{
[B][COLOR=#7f0055]try[/B][/COLOR] 
{
process.run([B][COLOR=#7f0055]this[/B][/COLOR], process);

getMessageQueue().post([B][COLOR=#7f0055]new[/B][/COLOR] Runnable()
{
[COLOR=#646464]@Override[/LEFT]
[/COLOR][LEFT][B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]void[/B][/COLOR] run() {
[COLOR=#3f7f5f]// [/COLOR][B][COLOR=#7f9fbf]TODO[/B][/COLOR][COLOR=#3f7f5f] Auto-generated method stub[/LEFT]
[/COLOR][LEFT][COLOR=#0000c0]eventSink[/COLOR].onProcessFinish(worker, process, [B][COLOR=#7f0055]null[/B][/COLOR]);
}
});
}
[B][COLOR=#7f0055]catch[/B][/COLOR] ([B][COLOR=#7f0055]final[/B][/COLOR] InterruptedException e) {
[COLOR=#3f7f5f]// [/COLOR][B][COLOR=#7f9fbf]TODO[/B][/COLOR][COLOR=#3f7f5f] Auto-generated catch block[/LEFT]
[/COLOR][LEFT]getMessageQueue().post([B][COLOR=#7f0055]new[/B][/COLOR] Runnable()
{
[COLOR=#646464]@Override[/LEFT]
[/COLOR][LEFT][B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]void[/B][/COLOR] run() {
[COLOR=#3f7f5f]// [/COLOR][B][COLOR=#7f9fbf]TODO[/B][/COLOR][COLOR=#3f7f5f] Auto-generated method stub[/LEFT]
[/COLOR][LEFT][COLOR=#0000c0]eventSink[/COLOR].onProcessFinish(worker, process, e);
}
});
Thread.[I]currentThread[/I]().interrupt();
} 
[B][COLOR=#7f0055]catch[/B][/COLOR] ([B][COLOR=#7f0055]final[/B][/COLOR] Exception e) 
{
[COLOR=#3f7f5f]// [/COLOR][B][COLOR=#7f9fbf]TODO[/B][/COLOR][COLOR=#3f7f5f] Auto-generated catch block[/LEFT]
[/COLOR][LEFT]
worker.setException([B][COLOR=#7f0055]true[/B][/COLOR]);

getMessageQueue().post([B][COLOR=#7f0055]new[/B][/COLOR] Runnable()
{
[COLOR=#646464]@Override[/LEFT]
[/COLOR][LEFT][B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]void[/B][/COLOR] run() {
[COLOR=#3f7f5f]// [/COLOR][B][COLOR=#7f9fbf]TODO[/B][/COLOR][COLOR=#3f7f5f] Auto-generated method stub[/LEFT]
[/COLOR][LEFT][COLOR=#0000c0]eventSink[/COLOR].onProcessFinish(worker, process, e);
}
}); 
}
}
}

[B][COLOR=#7f0055]if[/B][/COLOR]( ![B][COLOR=#7f0055]this[/B][/COLOR].isInterrupted() && [COLOR=#0000c0]processList[/COLOR].size() > 1 && !worker.isException() )
{
getMessageQueue().post([B][COLOR=#7f0055]new[/B][/COLOR] Runnable()
{
[COLOR=#646464]@Override[/LEFT]
[/COLOR][LEFT][B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]void[/B][/COLOR] run() {
[COLOR=#3f7f5f]// [/COLOR][B][COLOR=#7f9fbf]TODO[/B][/COLOR][COLOR=#3f7f5f] Auto-generated method stub[/LEFT]
[/COLOR][LEFT][COLOR=#0000c0]eventSink[/COLOR].onProcessListFinish(worker, [COLOR=#0000c0]processList[/COLOR], [B][COLOR=#7f0055]null[/B][/COLOR]);
}
});
}
}

[B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]void[/B][/COLOR] setProgress([B][COLOR=#7f0055]final[/B][/COLOR] BackgroundProcess process, [B][COLOR=#7f0055]final[/B][/COLOR] String message, [B][COLOR=#7f0055]final[/B][/COLOR] Integer progress)
{ 
getMessageQueue().post([B][COLOR=#7f0055]new[/B][/COLOR] Runnable()
{
[COLOR=#646464]@Override[/LEFT]
[/COLOR][LEFT][B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]void[/B][/COLOR] run() {
[COLOR=#3f7f5f]// [/COLOR][B][COLOR=#7f9fbf]TODO[/B][/COLOR][COLOR=#3f7f5f] Auto-generated method stub[/LEFT]
[/COLOR][LEFT][COLOR=#0000c0]eventSink[/COLOR].onProcessProgress(process, message, progress);
}
}); 
}

[B][COLOR=#7f0055]private[/B][/COLOR] Handler getMessageQueue()
{
Handler handler = [B][COLOR=#7f0055]null[/B][/COLOR];

[B][COLOR=#7f0055]synchronized[/B][/COLOR] ([B][COLOR=#7f0055]this[/B][/COLOR]) 
{ 
handler = [B][COLOR=#7f0055]this[/B][/COLOR].[COLOR=#0000c0]eventSink[/COLOR].getMessageQueue();
[B][COLOR=#7f0055]while[/B][/COLOR] (handler == [B][COLOR=#7f0055]null[/B][/COLOR]) 
{
[B][COLOR=#7f0055]try[/B][/COLOR] 
{
wait(50);
handler = [B][COLOR=#7f0055]this[/B][/COLOR].[COLOR=#0000c0]eventSink[/COLOR].getMessageQueue();
} 
[B][COLOR=#7f0055]catch[/B][/COLOR] (InterruptedException e) 
{
}
}
}
[B][COLOR=#7f0055]return[/B][/COLOR] handler;
}
[B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]boolean[/B][/COLOR] isException() {
[B][COLOR=#7f0055]return[/B][/COLOR] [COLOR=#0000c0]exception[/COLOR];
}
[B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]void[/B][/COLOR] setException([B][COLOR=#7f0055]boolean[/B][/COLOR] exception) {
[B][COLOR=#7f0055]this[/B][/COLOR].[COLOR=#0000c0]exception[/COLOR] = exception;
} 
}[/LEFT]


BackgroundJob1.java​
Code:
[B][COLOR=#7f0055][LEFT]public[/B][/COLOR] [B][COLOR=#7f0055]class[/B][/COLOR] BackgroundJob1 [B][COLOR=#7f0055]implements[/B][/COLOR] BackgroundWorker.BackgroundProcess {

[B][COLOR=#7f0055]private[/B][/COLOR] [B][COLOR=#7f0055]int[/B][/COLOR] [COLOR=#0000c0]prgogessId[/COLOR] = 0;

[B][COLOR=#7f0055]public[/B][/COLOR] BackgroundJob1() {
[COLOR=#3f7f5f]// [/COLOR][B][COLOR=#7f9fbf]TODO[/B][/COLOR][COLOR=#3f7f5f] Auto-generated constructor stub[/LEFT]
[/COLOR][LEFT]}
[B][COLOR=#7f0055]public[/B][/COLOR] BackgroundJob1([B][COLOR=#7f0055]int[/B][/COLOR] prgogessId) {
[COLOR=#3f7f5f]// [/COLOR][B][COLOR=#7f9fbf]TODO[/B][/COLOR][COLOR=#3f7f5f] Auto-generated constructor stub[/LEFT]
[/COLOR][LEFT]setPrgogessId(prgogessId);
}

[COLOR=#646464]@Override[/LEFT]
[/COLOR][LEFT][B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]void[/B][/COLOR] run(BackgroundWorker worker, BackgroundProcess process) [B][COLOR=#7f0055]throws[/B][/COLOR] Exception {
[COLOR=#3f7f5f]// [/COLOR][B][COLOR=#7f9fbf]TODO[/B][/COLOR][COLOR=#3f7f5f] Auto-generated method stub[/LEFT]
[/COLOR][LEFT][B][COLOR=#7f0055]int[/B][/COLOR] i = 0;
[B][COLOR=#7f0055]while[/B][/COLOR]( i < 20 )
{ 
worker.setProgress(process, String.[I]valueOf[/I](worker.getId()), Integer.[I]valueOf[/I](i));
Thread.[I]sleep[/I](500);
i++;

[B][COLOR=#7f0055]if[/B][/COLOR]( worker.getId() % i == 1 )
{
[B][COLOR=#7f0055]throw[/B][/COLOR] [B][COLOR=#7f0055]new[/B][/COLOR] Exception([COLOR=#2a00ff]"error"[/COLOR]);
}
}
}
[COLOR=#646464]@Override[/LEFT]
[/COLOR][LEFT][B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]int[/B][/COLOR] getPrgogessId() {
[B][COLOR=#7f0055]return[/B][/COLOR] [COLOR=#0000c0]prgogessId[/COLOR];
}

[B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]void[/B][/COLOR] setPrgogessId([B][COLOR=#7f0055]int[/B][/COLOR] prgogessId) {
[B][COLOR=#7f0055]this[/B][/COLOR].[COLOR=#0000c0]prgogessId[/COLOR] = prgogessId;
}
}[/LEFT]

MainActivity.java​
Code:
[LEFT][COLOR=#646464]@Override[/LEFT]
[/COLOR][LEFT][B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]void[/B][/COLOR] onResume()
{
[B][COLOR=#7f0055]super[/B][/COLOR].onResume();

List<BackgroundProcess> processList = [B][COLOR=#7f0055]new[/B][/COLOR] ArrayList<BackgroundProcess>();
processList.add([B][COLOR=#7f0055]new[/B][/COLOR] BackgroundJob1(R.id.[I][COLOR=#0000c0]text4[/I][/COLOR]));
processList.add([B][COLOR=#7f0055]new[/B][/COLOR] BackgroundJob1(R.id.[I][COLOR=#0000c0]text5[/I][/COLOR]));
processList.add([B][COLOR=#7f0055]new[/B][/COLOR] BackgroundJob1(R.id.[I][COLOR=#0000c0]text6[/I][/COLOR]));
processList.add([B][COLOR=#7f0055]new[/B][/COLOR] BackgroundJob1(R.id.[I][COLOR=#0000c0]text7[/I][/COLOR]));

[COLOR=#0000c0]worker[/COLOR] = [B][COLOR=#7f0055]new[/B][/COLOR] BackgroundWorker([B][COLOR=#7f0055]this[/B][/COLOR], processList);
[COLOR=#0000c0]worker[/COLOR].start();
}

[COLOR=#646464]@Override[/LEFT]
[/COLOR][LEFT][B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]void[/B][/COLOR] onBackPressed()
{
[B][COLOR=#7f0055]if[/B][/COLOR]( [COLOR=#0000c0]worker[/COLOR].isDone() )
{
finish();
}
[COLOR=#0000c0]worker[/COLOR].interrupt();
}

[B][COLOR=#7f0055]public[/B][/COLOR] Handler getMessageQueue() {
[COLOR=#3f7f5f]// [/COLOR][B][COLOR=#7f9fbf]TODO[/B][/COLOR][COLOR=#3f7f5f] Auto-generated method stub [/LEFT]
[/COLOR][LEFT][B][COLOR=#7f0055]return[/B][/COLOR] getWindow().getDecorView().getHandler();
}


[COLOR=#646464]@Override[/LEFT]
[/COLOR][LEFT][B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]void[/B][/COLOR] onProcessProgress(BackgroundProcess process, String message,
Integer progress) {
[COLOR=#3f7f5f]// [/COLOR][B][COLOR=#7f9fbf]TODO[/B][/COLOR][COLOR=#3f7f5f] Auto-generated method stub[/LEFT]
[/COLOR][LEFT]TextView textView = (TextView)findViewById(process.getPrgogessId());
[B][COLOR=#7f0055]if[/B][/COLOR]( textView != [B][COLOR=#7f0055]null[/B][/COLOR] )
{
textView.setText(message +[COLOR=#2a00ff]":"[/COLOR]+ progress.toString());
}
[B][COLOR=#7f0055]return[/B][/COLOR]; 
}



[COLOR=#646464]@Override[/LEFT]
[/COLOR][LEFT][B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]void[/B][/COLOR] onProcessFinish(BackgroundWorker worker,
BackgroundProcess process, Exception e) {
[COLOR=#3f7f5f]// [/COLOR][B][COLOR=#7f9fbf]TODO[/B][/COLOR][COLOR=#3f7f5f] Auto-generated method stub[/LEFT]
[/COLOR][LEFT]TextView textView = (TextView)findViewById(process.getPrgogessId());
[B][COLOR=#7f0055]if[/B][/COLOR]( e != [B][COLOR=#7f0055]null[/B][/COLOR] ) {
textView.setText(e.toString());
} [B][COLOR=#7f0055]else[/B][/COLOR] {
textView.setText([COLOR=#2a00ff]"done"[/COLOR]);
}

[B][COLOR=#7f0055]return[/B][/COLOR]; 
}



[COLOR=#646464]@Override[/LEFT]
[/COLOR][LEFT][B][COLOR=#7f0055]public[/B][/COLOR] [B][COLOR=#7f0055]void[/B][/COLOR] onProcessListFinish(BackgroundWorker worker,
List<BackgroundProcess> processList, Exception e) {
[COLOR=#3f7f5f]// [/COLOR][B][COLOR=#7f9fbf]TODO[/B][/COLOR][COLOR=#3f7f5f] Auto-generated method stub[/LEFT]
[/COLOR][LEFT]
TextView textView = (TextView)findViewById(processList.get(0).getPrgogessId());
[B][COLOR=#7f0055]if[/B][/COLOR]( e != [B][COLOR=#7f0055]null[/B][/COLOR] ) {
textView.setText(e.toString());
} [B][COLOR=#7f0055]else[/B][/COLOR] {
textView.setText([COLOR=#2a00ff]"list done"[/COLOR]);
}
[/LEFT]
}
 
Zuletzt bearbeitet:
Warum nicht einfach AyncTask nutzen?
 
Bevorzuge selber auch AsyncTask

Sent from my GT-I9100 using Android-Hilfe.de App
 
MDXDave schrieb:
Warum nicht einfach AyncTask nutzen?

Was mach ich eigentlich wenn der Task weitergeführt werden soll, wenn ich die Application beende? Ist z.B. Uploads/Downloads durchaus sinnvoll.

Was macht der AsyncTask dann?

Es wäre doch mal praktisch einen Prozessübergreifenden BackgroundWorker (Service) zu schreiben. Ich habe das vor einiger Zeit mal gemacht, aber der ist nicht sehr gut wieder zu verwenden gewesen, deswegen möchte ich den lieber nicht hier rein stellen.

Prinzip:

Service der eine Liste von so genannten "Lanes" (Assoziation: Autobahn).
Diese Lanes sind Erweiterungen (extends) von LinkedList<ITask>.
ITask ist hierbei ein Interface welches Parcelable erweitert (extends) für die IPC. Für ITask habe ich auch ein aidl File angelegt (Android IPC specific)

Der Service wartet nun bis eine Lane existiert in welcher ein Task liegen sollte. Für jede vorhandene Lane wird ein eigener Thread gestartet und darin die Tasks nacheinander abgearbeitet.

So kann man steuern ob die Tasks parallel oder nacheinander ausgeführt werden sollen.
Nacheinander: Eine Lane mit vielen Tasks
Parallel: Viele Lanes mit wenigen (oder gar nur einem) Task.

Nun muss man nur noch in seiner Applikation von ITask ableiten, die Parcelable sache implementieren und den Task an den Service schicken, welcher sich dann darum kümmert.
Selbst wenn die Application beendet ist. (Wichtig ist natürlich den Service auf "remote" zu stellen, sonst stirbt er mit dem Prozess der Application.

-Service
--Lanes
---Lane1
----Task1
----Task2
---Lane2
----Task3
----Task4

Lane 1 und 2 werden parallel bearbeitet.

Ich hoffe das war verständlich.
 
  • Danke
Reaktionen: MDXDave und cuehrer
MDXDave schrieb:
Warum nicht einfach AyncTask nutzen?

weil der Code wenig portabel wäre, das war der ursprüngliche Gedanke.
 
Zurück
Oben Unten