结合多重活动服务 [英] binding a service to multiple activities

查看:267
本文介绍了结合多重活动服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务已正确绑定到我的第一个活动,但是当我尝试将其绑定到一个次活动不工作
这里是onresume和我的第一次活动暂停的code

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

我做同样的我的第二个活动,但是当我尝试使用它的服务始终是空
这里是我的第二个活动的code:

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

那是我服务的code:

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

}

下面是我的清单文件:

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

在此先感谢您的帮助!

推荐答案

您必须使用服务调用同一个对象在这两个活动。要做到这一点,最好的办法将是扩大在那里你必须写code启动服务和stop.Then你可以从任何活动访问该服务的应用程序类。

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 startService(){
    //start your service

    }
    public stopService(){
    //stop service
    }
    public ExampleService getService(){
    }
    }

现在从活动得到服务

AppController.getInstance().getService();

这篇关于结合多重活动服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆