S
Sonse
Neues Mitglied
- 0
Hallo ich bins mal wieder.
Ich möchte von meiner MainActivity eine Wert über EditText einlesen lassen und diesen dann über einen Service an einen Thread weiterleiten.
Ich hab es jetzt schon auf mehrere Arten versuche aber es hat nie funktioniert. Hier mal ein Codebeispiel von mir.
Code in der MainActivity:
Code im Service:
Vielleicht hat mir ja jemand ein Vorschlag wie ich den Code berichtigen kann.
Ich möchte von meiner MainActivity eine Wert über EditText einlesen lassen und diesen dann über einen Service an einen Thread weiterleiten.
Ich hab es jetzt schon auf mehrere Arten versuche aber es hat nie funktioniert. Hier mal ein Codebeispiel von mir.
Code in der MainActivity:
Code:
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]
[LEFT]public[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] onClick(View v) {[/LEFT]
[LEFT][/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] m = Integer.[I]parseInt[/I]([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]Eingabe[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].getText().toString());[/LEFT]
[LEFT][/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]b1[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Bundle();[/LEFT]
[LEFT][/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]b1[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].putInt([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Wert1"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], m);[/LEFT]
[LEFT][/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]intent1[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Intent(MainActivity.[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], MyService.[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/LEFT]
[LEFT][/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]intent1[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].putExtras([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]b1[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/LEFT]
[LEFT]startService([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]intent1[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]); [/LEFT]
}
[/SIZE]
Code im Service:
Code:
[SIZE=2]
[LEFT][/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] onStartCommand(Intent intent, [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] flags, [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] startId) {[/LEFT]
[LEFT][/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]t1[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] == [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]null[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]) {[/LEFT]
[LEFT][/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]zb1a[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = getIntent().getExtras();[/LEFT]
[LEFT][/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] n = [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]zb1a[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].getInt([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Wert1"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/LEFT]
[LEFT][/LEFT]
[LEFT][/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]b1a[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Bundle();[/LEFT]
[LEFT][/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]b1a[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].putInt([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Wert1"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],n);[/LEFT]
[LEFT][/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]intent1a[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Intent(MyService.[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], Thread1.[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]); [/LEFT]
[LEFT][/LEFT]
[LEFT]([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]t1[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Thread1()).start();[/LEFT]
[LEFT]Toast.[I]makeText[/I]([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Thread1 wurde gestartet"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], Toast.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]LENGTH_LONG[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).show();[/LEFT]
}
[/SIZE]
Vielleicht hat mir ja jemand ein Vorschlag wie ich den Code berichtigen kann.