将少数活动绑定到一项服务 - class 连接被视为服务 - 无法破坏 activity

Binding few activities to one service - class which connected treated like service - can't destroy activity

我将从我的方法开始 我想要一项服务 运行 即使应用程序屏幕的 none 对用户可见。该服务将扫描信标。 应用程序的每个屏幕都需要访问服务方法,因此我在这里使用绑定到服务。 我设计了 serviceconnector class,它将活动与服务连接起来,这个 class 看起来像这样。

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;



public class ServiceConnector {
    Context context;
    BeaconScanningService scanningService;
    boolean mBound = false;

    public ServiceConnector(Context context) {
        LogShower.printLogs("Service Connector created");
        this.context = context;
        createBinding();
    }

    public void createBinding()
    {
        Intent intent = new Intent(context, BeaconScanningService.class);

        if(scanningService.isBeaconScanningServiceRunning())
        {
            LogShower.printLogs("Service is already running.");
            context.bindService(intent, mConnection, 0);
        }
        else
        {
            LogShower.printLogs("Service is not running yet.");
            context.startService(new Intent(context, BeaconScanningService.class));
            context.bindService(intent, mConnection, 0);
        }
    }

    public void startScanning()
    {
        LogShower.printLogs("Start Scanning.");

        scanningService.startBeaconScanner();
    }

    public void stopScanning()
    {
        LogShower.printLogs("Stop Scanning.");

        scanningService.stopBeaconScanner();
    }

    public void destroyBinding()
    {
        if (mBound) {
            scanningService.unbindService(mConnection);
            mBound = false;
        }
    }



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

        @Override
        public void onServiceConnected(ComponentName className,
                                       IBinder service)
        {
            LogShower.printLogs("onServiceConnected");
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            BeaconScanningService.LocalBinder binder = (BeaconScanningService.LocalBinder) service;
            scanningService = binder.getService();
            mBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            LogShower.printLogs("onServiceDisconnected");

            mBound = false;
        }
    };
}

这是activity,当我绑定并尝试解除绑定时

import android.app.Activity;
import android.os.Bundle;

public class StartScreen extends Activity
{
    ServiceConnector serviceConnector ;


    @Override
    protected void onCreate (Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.starting_screen_layout);
        serviceConnector = new ServiceConnector(this);

    }

    @Override
    protected void onDestroy ()
    {
        serviceConnector.destroyBinding();
        super.onDestroy();
    }
}

服务名称是 BeaconScanningService,这是我的清单

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bka.tog.ole" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".ZooBeacon"
            android:label="@string/app_name" >

        </activity>
        <service android:enabled="true" android:name=".beaconeserviceandconnector.BeaconScanningService">

        </service>
        <activity
            android:name=".StartScreen"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

问题是我不知道为什么我的 ServiceConnector class 在解除绑定过程中被解释为服务,或者我无法分析这个 logcat。

3405-3405/com.bka.tog.ole E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.bka.tog.ole, PID: 3405
java.lang.RuntimeException: Unable to destroy activity {com.bka.tog.ole/com.bka.tog.ole.StartScreen}: java.lang.IllegalArgumentException: Service not registered: com.bka.tog.ole.beaconeserviceandconnector.ServiceConnector@41d2f8d0
        at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3647)
        at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3665)
        at android.app.ActivityThread.access00(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1299)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5212)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IllegalArgumentException: Service not registered: com.bka.tog.ole.beaconeserviceandconnector.ServiceConnector@41d2f8d0
        at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:973)
        at android.app.ContextImpl.unbindService(ContextImpl.java:1671)
        at android.content.ContextWrapper.unbindService(ContextWrapper.java:536)
        at com.bka.tog.ole.beaconeserviceandconnector.ServiceConnector.destroyBinding(ServiceConnector.java:59)
        at com.bka.tog.ole.StartScreen.onDestroy(StartScreen.java:27)
        at android.app.Activity.performDestroy(Activity.java:5412)
        at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1118)
        at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3634)
            at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3665)
            at android.app.ActivityThread.access00(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1299)
            at android.os.Handler.dispatchMessage(Handler.java:106)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5212)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
            at dalvik.system.NativeStart.main(Native Method)

这种行为的原因是什么?

在我的 ServiceConnector class 中应该是:

context.unbindService(mConnection);

代替这一行:

scanningService.unbindService(mConnection);

我不知道这个错误编码是怎么来的,但我浪费了几个小时,所以一定要从 activity 的上下文中解除绑定。

通常我会删除这个问题,但是有人把它收藏了,所以我想告诉我错误的原因是什么。