簡體   English   中英

將服務綁定到多個活動

[英]binding a service to multiple activities

我的服務已正確綁定到我的第一個活動,但是當我嘗試將其綁定到第二個活動時,它不起作用這是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);
    }
}

我對第二個活動執行了相同的操作,但是當我嘗試使用該服務時,它始終為null這是第二個活動的代碼:

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

在此先感謝您的幫助!

您必須在兩個活動中使用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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM