# BoundServiceKommunikation



## bandchef (3. Jan 2014)

Hi Leute!

Ich hab ein Problem mit meinem Quellcode. Der Emulator bringt mir immer den Fehler, dass die System UI gestoppt wurde. Hier der Code:


```
package com.example.boundservicekommunikation;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

import com.example.boundservicekommunikation.MyService.ICallback;
import com.example.boundservicekommunikation.MyService.MyBinder;

public class MainActivity extends Activity implements ICallback
{	
	MyService myService  = new MyService();	//Eine Instanz von MyService
	
	//Eine Service Connection überwacht den Status eines Services
	private ServiceConnection myServiceConnection = new ServiceConnection() {
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			myService.myBinder = (MyBinder) service;
			myService.myBinder.setCallback(MainActivity.this);
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
		}
	};
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		//Den Service binden (binden = Service starten)
		bindService(new Intent(this, MyService.class), myServiceConnection, BIND_AUTO_CREATE);
	}
	
	
	@Override
	protected void onDestroy() {
		unbindService(myServiceConnection);
	}

	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	
	public void countUpdate(int counter) {
		TextView textView = (TextView) findViewById(R.id.textView1);
		textView.setText("Counter: " + counter);	//String.valueOf(counter));
	}
	
	
	public void onClickCount(View button) {
		myService.myBinder.count();	//inkrementiert counter
	}
}
```


```
package com.example.boundservicekommunikation;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

//MyService Klasse
public class MyService extends Service {
	
	MyBinder myBinder = new MyBinder();	//Eine Instanz des Binders
	private ICallback myICallback;	//Eine Referenz vom Typ ICallback
	private int counter = 0;	//Der Counter
	
	
	@Override
	public IBinder onBind(Intent intent) {
		return myBinder;
	}
	
	
	public interface ICallback {
		public void countUpdate(int counter);
	}
	
	
	//Innere Klasse MyBinder
	public class MyBinder extends Binder {
		void setCallback(ICallback myICallback)
		{
			MyService.this.myICallback = myICallback;
		}
		
		//inkrementiert den counter
		void count() {
			counter++;
			myICallback.countUpdate(counter);
		}
		
		//counter wird zurückgegeben
		int getCounter() {
			return counter;
		}
	}
}
```


```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="113dp"
        android:text="0" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="37dp"
        android:onClick="onClickCount"
        android:text="Count" />

</RelativeLayout>
```


```
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.boundservicekommunikation"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.boundservicekommunikation.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.boundservicekommunikation.MyService" />
    </application>

</manifest>
```


Ich weiß überhaupt nicht an was das liegen könnte! Kann mir jemand helfen?


----------



## dzim (6. Jan 2014)

Warum registrierst du einen Service als Activity? Das könnte schon der Grund sein.


----------

