我的服务已正确绑定(bind)到我的第一个 Activity ,但是当我尝试将它绑定(bind)到第二个 Activity 时它不起作用 这是我的第一个 Activity 的 onresume 和 on pause 的代码
@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);
}
}
我对我的第二个 Activity 做了同样的事情,但是当我尝试使用该服务时,它始终为空 这是我的第二个 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);
}
}
那是我的服务代码:
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;
}
};
}
这是我的 list 文件:
<?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>
预先感谢您的帮助!
最佳答案
您必须在两个 Activity 中使用相同的服务调用对象。最好的方法是扩展 Application 类,您必须在其中编写代码来启动和停止服务。然后您可以从任何 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(){
}
}
现在从 Activity 中获取服务
AppController.getInstance().getService();
关于android - 将服务绑定(bind)到多个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30648766/