Binder class

  • 1 Antworten
  • Letztes Antwortdatum
znieh99

znieh99

Fortgeschrittenes Mitglied
12
Hallo Forum,
ich habe ein seltsames Problem. Ich habe die Beispiel-Anwendung von "Bound services overview  |  Android Developers" kopiert und wollte sie laufen lassen. Hat nicht funktioniert. Mit dem Debugger sehe ich, dass die Methode "onBind()" in LocalService gar nicht ausgefürt wird. Kann mir Jemand erklären was da falsch läuft?

MainActivity
Code:
package com.app.heinz.bindingactivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.app.heinz.bindingactivity.LocalService;

public class MainActivity extends AppCompatActivity {
    LocalService mService;
    boolean mBound = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onStart() {
        super.onStart();
        // Bind to LocalService
        Intent intent = new Intent(this, LocalService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        unbindService(mConnection);
        mBound = false;
    }

    /** Called when a button is clicked (the button in the layout file attaches to
     * this method with the android:onClick attribute) */
    public void onButtonClick(View v) {
        if (mBound) {
            // Call a method from the LocalService.
            // However, if this call were something that might hang, then this request should
            // occur in a separate thread to avoid slowing down the activity performance.
            int num = mService.getRandomNumber();
            Toast.makeText(this, "number: " + num, Toast.LENGTH_SHORT).show();
        }
    }

    /** Defines callbacks for service binding, passed to bindService() */
    private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className,
                                       IBinder service) {
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            LocalService.LocalBinder binder = (LocalService.LocalBinder) service;
            mService = binder.getService();
            mBound = true;
        }
        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mBound = false;
        }
    };
}

LocalService
Code:
package com.app.heinz.bindingactivity;

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

import java.util.Random;

public class LocalService extends Service {
    // Binder given to clients
    private final IBinder mBinder = new LocalBinder();
    // Random number generator
    private final Random mGenerator = new Random();

    /**
     * Class used for the client Binder.  Because we know this service always
     * runs in the same process as its clients, we don't need to deal with IPC.
     */
    public class LocalBinder extends Binder {
        LocalService getService() {
            // Return this instance of LocalService so clients can call public methods
            return LocalService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    /** method for clients */
    public int getRandomNumber() {
        return mGenerator.nextInt(100);
    }
}
 
Und sie bewegt sich doch!
Habe den Eintrag <service android:name=".LocalService"/> in der Manifest vergessen,
lg heinz
 

Ähnliche Themen

Horstmann365
Antworten
1
Aufrufe
1.171
jogimuc
J
E
Antworten
2
Aufrufe
852
ekaya999
E
R
Antworten
23
Aufrufe
3.161
RealNiclas
R
Zurück
Oben Unten