android - 将服务绑定(bind)到多个 Activity

标签 android android-service android-service-binding

我的服务已正确绑定(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/

相关文章:

android - 如何获取 APN 名称

java - AlertDialog.Builder 中的 Android stopService()

android - 如何访问 MediaBrowserServiceCompat 服务实例?

android - Activity 被销毁时服务停止

android - 自定义系统服务上下文错误

android - Asterisk :Don't allow to call if SIP peer is not registered

android - 弹出对话框在android中显示两次

android - 在 camera2 api 中预览拉伸(stretch)

android - 如何在不停止服务或断开 ble 连接的情况下建立使用服务连接的 BLE 连接以跨 Activity 使用?

用于 getRunningServices 的 Android O 替换