将少数活动绑定到一个服务 - 连接处理类似服务的类 - 不能破坏活动

时间:2015-09-10 20:33:46

标签: java android android-service android-service-binding

我将从我的方法开始我希望有一个服务正在运行,即使用户看不到任何应用程序屏幕。此服务将扫描信标。 每个应用程序屏幕都需要访问服务方法,所以我在这里使用绑定服务。 我设计了serviceconnector类,它将使用服务连接Activities,这个类看起来像这样。

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;
        }
    };
}

当我进行绑定并尝试解除绑定时,这是活动

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类在解除绑定过程中被解释为Service,或者我无法对此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$1@41d2f8d0
        at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3647)
        at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3665)
        at android.app.ActivityThread.access$1400(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$1@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.access$1400(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)

这种行为的原因是什么?

1 个答案:

答案 0 :(得分:1)

在我的ServiceConnector类中应该是:

context.unbindService(mConnection);

而不是这一行:

scanningService.unbindService(mConnection);

我不知道这个错误编码是如何产生的,但是我失去了几个小时,所以请确保你解除活动背景。

通常情况下我会删除这个问题,但有一个人会喜欢它,所以我想说明我的错误是什么原因。