android - binding a service to multiple activities -
my service correctly binded first activity when try bind second activity not work here code of onresume , on pause of 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); } }
i did same second activity when try use service null here code of 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); } }
thats code of 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; } }; }
}
here manifest file:
<?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>
thanks in advance help!
you have use same object of service call in both activities. best way extend application class have write code start service , stop.then can access service 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 service } public void stopservice(){ //stop service } public exampleservice getservice(){ } }
now service activity
appcontroller.getinstance().getservice();
Comments
Post a Comment