Activity bindet sich nicht an lokalen Service

  • Antworten:1
Wicki12
  • Forum-Beiträge: 38

28.04.2012, 09:47:42 via Website

Hallo,

Bitte um Hilfe. Ich versuche aus der LocalBoundServiceActivity heraus einen lokalen Service zu nutzen. Es gelingt mir nicht, die Bindung herzustellen. Nachfolgend der Code.
Der Service:
1package wicki.Android.LocalService;
2
3import java.util.Random;
4
5import android.app.Service;
6import android.content.Intent;
7import android.os.Binder;
8import android.os.IBinder;
9import android.util.Log;
10
11public class LocalService extends Service {
12 private final IBinder mBinder = new LocalBinder();
13 private final Random mGenerator = new Random();
14 /**
15 * Class used for the client Binder. This service always
16 * runs in the same process as its clients
17 */
18 public class LocalBinder extends Binder {
19 LocalService getService() {
20 Log.i("LocalService","**** getService");
21 // Clients can call Service's public methods
22 return LocalService.this;
23 }
24 }
25 @Override
26 public IBinder onBind(Intent intent) {
27 Log.i("LocalService","**** onBind");
28 return mBinder;
29 }
30 /** method for clients */
31 public int getRandomNumber() { return mGenerator.nextInt(100); }
32}
Die Activity:
1package wicki.Android.LocalService;
2
3import android.app.Activity;
4import android.content.ComponentName;
5import android.content.Context;
6import android.content.Intent;
7import android.content.ServiceConnection;
8import android.os.Bundle;
9import android.os.Handler;
10import android.os.IBinder;
11import android.util.Log;
12import android.view.View;
13import android.view.View.OnClickListener;
14import android.widget.Button;
15import android.widget.TextView;
16
17public class LocalBoundServiceActivity extends Activity {
18 boolean mBound = false;
19 TextView txt;
20
21 //http://groups.google.com/group/android-developers/browse_thread/thread/e0a0db3dac32b9e3#
22 /** Defines callbacks for service binding, passed to bindService() */
23 private TestServiceConnection mConnection = null;
24 /** Called when the activity is first created. */
25 @Override
26 public void onCreate(Bundle savedInstanceState) {
27 super.onCreate(savedInstanceState);
28 setContentView(R.layout.main);
29 mConnection = new TestServiceConnection();
30 Button button = (Button) findViewById(R.id.Button01);
31 txt = (TextView)findViewById(R.id.text);
32 button.setOnClickListener(new OnClickListener() {
33 public void onClick(View v) {
34 onButtonClick(v);
35 }
36 });
37 Intent intent = new Intent(this, LocalService.class);
38 bindService(intent, mConnection, 0);//Context.BIND_AUTO_CREATE);
39 this.startService(intent);
40 Log.i("LocalService","**** onCreate");
41 }
42/*
43 protected void onStart() {
44 super.onStart();
45 Intent intent = new Intent(this, LocalService.class);
46 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
47 Log.i("LocalService","**** onStart");
48 }
49*/
50 public void onButtonClick(View v) {
51 if (mConnection.mService != null) mBound=true;
52 Log.i("LocalService","**** onButtonClick, mBound="+mBound);
53 if (mBound) {
54 // Call a method from the LocalService.
55 int num = mConnection.mService.getRandomNumber();
56 TextView text = (TextView)findViewById(R.id.text);
57 text.setText("number: " + num);
58 }
59 }
60 protected void onStop() {
61 super.onStop();
62 // Unbind from the service
63// if (mBound) {
64 unbindService(mConnection); mBound = false;
65// }
66 Log.i("LocalService","**** onStop");
67 }
68 private final Handler handler = new Handler();
69
70 public void onStart() {
71 super.onStart();
72 Runnable r = new Runnable() {
73 public void run() {
74 TextView tv = (TextView)findViewById(R.id.text);
75 int p = -1;
76 String zws="Unable to access TestService!";
77 if (mConnection.mService != null) {
78 zws = "TestService on!";
79 p = mConnection.mService.getRandomNumber();
80 }
81 tv.setText(zws+" Randomwert: "+p);
82 }
83 };
84 handler.postDelayed(r, 10000L);
85 Log.i("LocalService","**** onStart(Runnable)");
86 }
87}
Hier habe ich es zuerst mit der auskommentierten "onStart" versucht, dann aber auf Anregung eines Beitrages aus dem Netz habe ich das Binden in "onCreate" programmiert und in dem "Runnable" zeitverzögert die Service-Abfrage gestellt (Bindung ist laut obigen Beitrags asynchron).

Die Hilfsklasse "TestServiceConnection":
1package wicki.Android.LocalService;
2
3import android.content.ComponentName;
4import android.content.ServiceConnection;
5import android.os.IBinder;
6
7public class TestServiceConnection implements ServiceConnection
8{
9 protected LocalService mService = null;
10
11 @Override
12 public void onServiceConnected(ComponentName name, IBinder service) {
13 mService = ((LocalService.LocalBinder)service).getService();
14 }
15
16 @Override
17 public void onServiceDisconnected(ComponentName name) {
18 mService = null;
19 }
20}
Die Manifest-Datei:
1<?xml version="1.0" encoding="utf-8"?>
2<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="wicki.Android.LocalService"
4 android:versionCode="1"
5 android:versionName="1.0" >
6
7 <uses-sdk android:minSdkVersion="8" />
8 <service android:name=".LocalService"
9 android:enabled="true"/>
10
11 <application
12 android:icon="@drawable/ic_launcher"
13 android:label="@string/app_name" >
14 <activity
15 android:name=".LocalBoundServiceActivity"
16 android:label="@string/app_name" >
17 <intent-filter>
18 <action android:name="android.intent.action.MAIN" />
19
20 <category android:name="android.intent.category.LAUNCHER" />
21 </intent-filter>
22 </activity>
23 </application>
24
25</manifest>
Wo liegt der Fehler?
PS: Das Eclipse-Projekt ist als zip-Datei verfügbar unter: Eclipse-Projekt

Gruß Wicki

Antworten
Wicki12
  • Forum-Beiträge: 38

28.04.2012, 10:47:48 via Website

Hallo,

habe selbst den Fehler nach intensivem Googeln gefunden. In der Manifest-Datei muss die service-Deklaration innerhalb von "application",
ausserhalb von "activity" stehen !
Dank für Eure Mühe !

Gruß Wicki

Antworten