My service is correctly binded to my first activity but when I try to bind it to a second activity it does not work Here is the code of onresume and on pause of my first activity
@Override
protected void onResume() {
super.onResume();
connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
service = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
shareInfos.this.service = (IService) service;
}
};
bindService(new Intent(this, shareInfos.class), connection,
Context.BIND_AUTO_CREATE);
}
@Override
protected void onPause() {
super.onPause();
if (service != null) {
service = null;
unbindService(connection);
}
}
I did the same to my second activity but when I try to use the service it is always null Here is the code of my second activity:
@Override
protected void onResume() {
super.onResume();
connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
service = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
shareInfos.this.service = (IService) service;
}
};
bindService(new Intent(this, shareInfos.class), connection,
Context.BIND_AUTO_CREATE);
}
@Override
protected void onPause() {
super.onPause();
if (service != null) {
service = null;
unbindService(connection);
}
}
Thats is the code of my service:
public class ExampleService extends AbstractService {
private static final String SERVICE_NAME = "ExampleService";
public ExampleService() {
super(SERVICE_NAME);
}
@Override
public AbstractRegistration getRegistration() {
return new AbstractRegistration() {
@Override
public String getApplicationName() {
return getResources().getString(R.string.application_name);
}
@Override
public String getApplicationDescription() {
return getResources().getString(R.string.application_description);
}
@Override
public PendingIntent getApplicationSettings() {
return PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), ExampleActivity.class), 0);
}
@Override
public boolean requiresStorage() {
return true;
}
@Override
public boolean requiresQueries() {
return true;
}
@Override
public boolean requiresRecognition() {
return true;
}
};
}
}
Here is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="eu.gambas.example.android" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="15" />
<uses-permission android:name="eu.gambas.permission.ACCESS" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
<application
android:icon="@drawable/icon"
android:label="@string/application_name" >
<activity
android:name="com.example.egm.exampleandroid.ExampleActivity"
android:label="@string/application_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.egm.exampleandroid.ResultActivity" />
<activity android:name="com.example.egm.exampleandroid.shareInfos" />
<activity android:name="com.example.egm.exampleandroid.testActivity" />
<service
android:name="com.example.egm.exampleandroid.ExampleService"
android:exported="true" >
<intent-filter>
<action android:name="eu.gambas.action.start" />
</intent-filter>
<intent-filter>
<action android:name="eu.gambas.action.stop" />
</intent-filter>
<intent-filter>
<action android:name="eu.gambas.action.result" />
</intent-filter>
</service>
</application>
Thanks in advance for your help!
Bound service runs as long as some other application component is bound to it. Many components can bind to one service at a time, but once they all unbind, the service will destroy. To bind an application component to the service, bindService() is used.
A bound service is the server in a client-server interface. It allows components (such as activities) to bind to the service, send requests, receive responses, and perform interprocess communication (IPC).
In Android, seeing as only one Activity can bind to a Service at a time, and only one Activity can be shown at a time, there is no real reason to want to bind 2 Activities at a time. But, if you'd like, the best solution is to bind the Service in the onResume() method, and unbind it in the onPause() method.
You have to use the same object of the Service call in both the activities. The best way to do it will be to extend the Application class where you have to write code to start service and to stop.Then you can access the service from any activity.
public class AppController extends Application {
private static AppController mInstance;
private ExampleService service;
public static synchronized AppController getInstance() {
return mInstance;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
public void startService(){
//start your service
}
public void stopService(){
//stop service
}
public ExampleService getService(){
}
}
Now to get the service from activity
AppController.getInstance().getService();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With