将服务绑定到多个活动

时间:2015-06-04 16:02:35

标签: android android-service android-service-binding

我的服务正确绑定到我的第一个活动,但是当我尝试将其绑定到第二个活动时,它不起作用 这是我的第一个活动的onresume和暂停的代码

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

我对第二个活动做了同样的事情,但是当我尝试使用该服务时,它总是为空 这是我的第二个活动的代码:

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

这是我服务的代码:

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

}

这是我的清单文件:

<?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>

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:4)

您必须在两个活动中使用Service调用的相同对象。最好的方法是扩展Application类,在那里你必须编写代码来启动服务并停止。然后你可以从任何活动访问服务。

    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(){
    }
    }

现在从活动中获取服务

AppController.getInstance().getService();