gpt4 book ai didi

java - 将少数 Activity 绑定(bind)到一项服务 - 连接的类被视为服务 - 不能破坏 Activity

转载 作者:行者123 更新时间:2023-11-30 01:54:21 24 4
gpt4 key购买 nike

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

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

这是我进行绑定(bind)并尝试解除绑定(bind)时的 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,这是我的 list

 <?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 类在解除绑定(bind)过程中被解释为服务,或者我无法分析这个 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)

这种行为的原因是什么?

最佳答案

在我的 ServiceConnector 类中应该是:

context.unbindService(mConnection);

代替这一行:

scanningService.unbindService(mConnection);

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

通常我会删除这个问题,但是有人把它 Collection 了,所以我想展示我错误的原因。

关于java - 将少数 Activity 绑定(bind)到一项服务 - 连接的类被视为服务 - 不能破坏 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32511128/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com