Wert an Service und Thread übergeben

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:
[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.
 
Hey Sonse ich versuch immer und jedem zu helfen :D

Ich kenn mich zwar nciht so gut mit Edittext feldern aus und hab auch grad kein Bock in meinem Android Buch nachzuschauen :flapper::flapper::flapper:
aber ich würde es so amchen ein Edittext erstellen und ein Button dazu. Sobald der Button gedrückt wird wird das was im Edittext steht in einer Variablen gespeichert oder sofort an den Thread bzw. Service geschickt.

Also ist jetzt mehr eine allgemeine herangehensweise statt dir speziell jetzt bei deinem Beispiel zu helfen sorry deswegen.

Falls du mehr über Edittext erfahren willst kannst du ja mal iin der offizielen android dukumentaion reingucken und dich mal durchlesen.

Der ursprüngliche Beitrag von 17:45 Uhr wurde um 17:46 Uhr ergänzt:

haha ich sehe das amchst du ja schon hahahahahahahhaha tut mir leid dass ich das nciht egsehen habe
 

Ähnliche Themen

R
  • RalfKahl
Antworten
10
Aufrufe
302
RalfKahl
R
5
Antworten
22
Aufrufe
1.422
590239
5
M
  • MikelKatzengreis
Antworten
5
Aufrufe
132
swa00
swa00
Zurück
Oben Unten