问题描述
我将我的方法开始我想有它运行,即使没有任何应用程序屏幕是用户可见的一项服务。这项服务将扫描的信标。
应用程序的每一个屏幕需要,所以我用的结合在这里服务以获取接入服务的方法。
我设计将与服务要连接活动serviceconnector类,这个类是这样的。
I will start from my approach I want to have one service which is running even if none of application screen is visible for user. This service will scan for beacons.Every screen of application needs to get access to method of service so I used binding to service here.I designed serviceconnector class which will be connecting Activities with service, this class look like this.
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;
}
};
}
这是活动的时候我做结合,并尝试解除
This is activity when I make binding and try to unbind
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类是在解除绑定过程间$ P $私人的服务,或只是我不能让这个logcat的分析。
The problem is that I don't know why my ServiceConnector class is interprete as Service during unbinding process or just I can't make analysis of this 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)
什么是这种行为的原因是什么?
What is the reason of this behavior?
推荐答案
在我ServiceConnector类应该是:
In my ServiceConnector class should be:
context.unbindService(mConnection);
而不是此行:
scanningService.unbindService(mConnection);
我不知道该怎么misscoding从何而来,但我失去了几个小时,因此请确保你的活动范围内解除绑定。
I don't know how this misscoding come from, but I lost few hours, so be sure that you unbind from context of activity.
通常我会删除这个问题,但一个人让喜欢的,所以我想展示什么是我的错误的原因。
Normally i would delete this question, but one person make it favorite so I want to show what was the reason of my error.
这篇关于结合一些活动,以一个服务 - 这就像连接处理服务类 - 不能破坏活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!