Android associate a service with an activity

I saw several similar examples here, but I can not get my service to contact activity.

I get an error

"android.os.binderproxy cannot be passed to IC_CommissaryService".

My service looks like this:

public class IC_CommissaryService extends Service { @Override public IBinder onBind(Intent intent) { return mBinder; } private final IBinder mBinder = new LocalBinder(); public class LocalBinder extends Binder { IC_CommissaryService getService() { return IC_CommissaryService.this; } } public int onStartCommand(Intent intent, int flags, int startId) { } private boolean SendOrderToServer(int orderID) { /* do stuff*/ } 

}

and my activity is as follows:

 public class SubmitOrders extends Activity { IC_CommissaryService ICservice; @Override public void onCreate(Bundle savedInstanceState) { Intent serviceintent = new Intent(this, IC_CommissaryService.class); serviceintent.putExtra("binded", true); bindService(serviceintent, mConnection, Context.BIND_AUTO_CREATE); } private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName className, IBinder service) { Log.e("TEST", "SERVICE CONNECTED"); try { ICservice =(IC_CommissaryService.LocalBinder)service).getService(); for(int i = 0; i < Submitorders.size(); i++) { ICservice.SendOrderToServer(Submitorders.get(i).intValue()); } } catch(Exception ex) { Log.e("Error", "Error connecting service: " + ex.getMessage()); } } @Override public void onServiceDisconnected(ComponentName className) { } }; } 

I get an error in my activity in the line ICservice =(IC_CommissaryService.LocalBinder)service).getService();

I think I did the same as the people who have already been suggested in other posts to help you?

thanks

+4
source share
3 answers

I had the same problem. I just figured it out today. Please view the parts annotated with <<===== below. I hope this helps.

 public class PracticeServiceBindingActivity extends ListActivity { private MyService.MyBinder service; <<==== .... private ServiceConnection connection = new ServiceConnection( ){ public void onServiceConnected (ComponentName name, IBinder service) { setService(MyService.MyBinder) service; <<==== .... } } public void onCreate(....) { ... public MyService.MyBinder getService(){ <<===== return service; } public void setService(MyService.MyBinder service) { <<===== this.service = service; } } } 
+1
source

Quote from the Bound Services documentation:

If your service is private to your own application and runs in the same process as the client (which is shared), you must create your interface by extending the Binder class and returning an instance of this from onBind ().

Remove the android: process attribute in AndroidManifest.ml to start the service in the same process. Today I had the same problem as the trick.

0
source

These are the abstract classes that I use to solve this problem: https://gist.github.com/frenchie4111/6086c6e4327d7936364a

Just extend both of these classes to your service and activity (you can easily change a fragment from a fragment to activity). And make sure that in your onCreate service / snippet you set serviceClass as follows:

 public void onCreate( Bundle savedInstance ) { super.onCreate( savedInstance ); this.serviceClass = IC_CommissaryService.class; } 
0
source

Source: https://habr.com/ru/post/1403468/


All Articles