Linking a service to multiple actions

My service is correctly tied to my first activity, but when I try to associate it with the second action, it does not work. Here is the onresume code and the pause of my first action.

@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 with my second action, but when I try to use the service, it is always zero. Here is the code for my second action:

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

This 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!

+5
source share
1 answer

You must use the same service call object in both actions. The best way to do this is to extend the application class where you need to write code to start the service and 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 a service out of activity

 AppController.getInstance().getService(); 
+4
source

All Articles