gpt4 book ai didi

Android服务绑定(bind)麻烦

转载 作者:行者123 更新时间:2023-11-30 03:59:47 30 4
gpt4 key购买 nike

你好社区我有以下问题。

我的 list 文件如下所示。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.someCo.test"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.location.network" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-feature android:name="android.hardware.wifi" />
<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" >
<service android:name = ".BackgroundService" android:exported="true" />
</application>

</manifest>

我的 Activity 的 onCreate 具有以下代码行。

this.context.startService(new Intent(this.context, com.someCo.test.BackgroundService.class));
this.context.bindService(new Intent(this.context, com.someCo.test.BackgroundService.class), serviceConnection, Context.BIND_AUTO_CREATE);

我的 Activity 还有下面的私有(private)类。

private ServiceConnection serviceConnection = new ServiceConnection() {

public static final String TAG = "Service Connection Class";

public void onServiceConnected(ComponentName className, IBinder service) {

try {
com.someCo.test.BackgroundService.MyBinder binder = (com.someCo.test.BackgroundService.MyBinder)service;
backgroundService = binder.getService();

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


//backgroundService = ((BackgroundService.MyBinder) service).getService();

//Toast.makeText(context,"service connected", Toast.LENGTH_LONG);
Log.i(TAG, "onServiceConnected");


}


//@Override
public void onServiceDisconnected(ComponentName className) {
backgroundService = null;
}
};

我的服务有以下内容。

    private final IBinder binder = new MyBinder();

public class MyBinder extends Binder{

private static final String TAG = "MyBinder";


BackgroundService getService(){
Log.i(TAG, "get service");
return BackgroundService.this;
}
}

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Log.i(TAG, "onBind destroyed");
return binder;

}

@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "onUnbind destroyed");
return super.onUnbind(intent);
}

在这一行,我在代码下方得到了异常。

(com.someCo.test.BackgroundService.MyBinder)service;
backgroundService = binder.getService();

java.lang.classCastException: android.os.BinderProxy.

谁能指出我的错误,我似乎无法弄清楚。请原谅非常冗长的代码,因为我在调试这个问题时试图尽可能明确。

谢谢,

最佳答案

我不知道你为什么要用两种方式(StartService和BindService)编写启动服务的代码?

这是 example for the Bind Service .你可以引用这个博客。

关于Android服务绑定(bind)麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12684577/

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