onServiceConnected never called after bindService method
I can't make up the exact problem out of your description, so I'm going to guess here!
How can bindService()
throw a NullPointerException
? The only way this could (/should) happen is when you don't supply a Service
or a ServiceConnection
listener.
bindService()
can't throw a NullPointerException
because onServiceConnected()
isn't called. The call to onServiceConnected()
is a product of bindService()
.
So I guess you are calling a AIDL
method, before the Service
has actually bonded?
onBind() and onServiceConnected() are not called after bindService() in onCreate()
It turns out that the Service isn't created until the UI thread is clear of code from the Activity, therefore I couldn't access it until after the onStart()
method of my Activity. That is what the onServiceConnected()
callback method is for. At that point the Service has been created and the Activity has access to the Service.
Android: onServiceConnected not called after bindService
You need to add a <service>
element, as a peer of your <activity>
element, as a child of <application>
:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tek15.cellemrmonitor"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="NetworkQueryService" />
</application>
My example element assumes that NetworkQueryService
is really com.tek15.cellemrmonitor.NetworkQueryService
— otherwise, substitute in the proper fully-qualified class name.
onServiceConnected() not called
After some more research, I discovered this is a known issue in Android.
The second activity I was talking about was an activity which is used as content within a TabActivity
.
The way to fix this was to call bindService(...)
on the application context, instead of on the activity context using getApplicationContext().bindService(...)
onserviceconnected never called: android
You can't call mService.send()
until after you get the onServiceConnected()
callback. That means you can't do both bindService()
and mService.send()
in onCreate()
. You need to move the mService.send()
call into either onResume()
or into onServiceConnected()
or somewhere else.
ServiceConnection.onServiceConnected() never called after binding to started service
ServiceConnection's onServiceConnected() is called, but nobody guarantees that it will be called before onCreate continues execution. So, what happens here - you successfuly bind to the service (that's why onBind returns true), but you're not fully connected - onServiceConnected() has not yet been called, so your local mProgressService object is not yet initalized, and therefore you get the NullPointerException.
Solution:
Move these two lines:
mProgressBarList = mProgressService.getProgressBarList();
mStaffNameList = mProgressService.getStaffNameList();
from onCreate() to onServiceConnected() function (use the service object after it is initialized in onServiceConnected()).
OnServiceConnected not getting called
I rewrote the service class so that onBind() returns IBinder as follwos.
public class PodService extends Service {
@Override
public IBinder onBind(Intent intent) {
Log.d("bound", "bound");
return mBinder; // returns IBinder object
}
private final IBinder mBinder = new PodSeviceStub(this);
static class PodSeviceStub extends IPodService.Stub {
WeakReference<PodService> mService;
public PodSeviceStub(PodService service) {// added a constructor for Stub here
mService = new WeakReference<PodService>(service);
}
//here i implemented unimplemented methods
}
}
and now it works.
Related Topics
Opencv Image Comparison in Android
How to Get the Current Dns Servers for Android
Push Up Content When Clicking in Edit Text
How to Use Data Binding and Kotlin in Android Studio 3.0.0
Android Device Is Not Connected to Usb for Debugging (Android Studio)
Android 4.4 Giving Err_Cache_Miss Error in Onreceivederror for Webview Back
Changing the Screen Brightness System Setting Android
Cannot Deserialize Instance of Object Out of Start_Array Token in Spring Webservice
Proguard Warnings "Can't Write Resource [Meta-Inf/Manifest.Mf] (Duplicate Zip Entry)"
How to Use "Goasync" for Broadcastreceiver
How to Use Getlistview() in Activity
How to Include a Library Module Dependency in an Android Studio Project
Scoping States in Jetpack Compose
Kitkat Kills: Not Allowed to Load Local Resource: File:///Android_Asset/Webkit/Android-Weberror.Png
Lint Error "Do Not Treat Position as Fixed; Only Use Immediately..."
Gesture Detection and Scrollview Issue
Java.Io.Ioexception: Unexpected End of Stream on Connection in Android