Android

I am trying to import an android.content.Context file into AIDL, but eclipse does not recognize it.

here is my code:

package nsip.net; import android.content.Context; // error couldn't find import for class ... interface IMyContactsService{ void printToast(Context context, String text); } 

Can anybody help me?

+4
source share
1 answer

Using android.content.Context will not work as it does not implement android.os.Parcelable .

However, if you have a class ( MyExampleParcelable for example) that you want to transfer to the AIDL interface (& actually implementing Parcelable ), you create a .aidl file, MyExampleParcelable.aidl , in which you write:

 package the.package.where.the.class.is; parcelable MyExampleParcelable; 


Now, if you desperately don't want to talk about processes, you have to consider local services. Edit (a little more useful):

Is this a local service (i.e. will only be used in your own application and process)? In these cases, it is usually simply best to implement a binder and return it directly.

 public class SomeService extends Service { .... .... public class SomeServiceBinder extends Binder { public SomeService getSomeService() { return SomeService.this; } } private final IBinder mBinder = new SomeServiceBinder(); @Override public IBinder onBind(Intent intent) { return mBinder; } public void printToast(Context context, String text) { // Why are you even passing Context here? A Service can create Toasts by it self. .... .... } // And all other methods you want the caller to be able to invoke on // your service. } 

Basically, when an Activity tied to your service, it simply translates the resulting IBinder into SomeService.SomeServiceBinder , calls SomeService.SomeServiceBinder#getSomeService() - and bang , access to the executable instance of Service +, you can call the material in your API.

+7
source

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


All Articles