I am struggling with the ServiceConnection for inapt purchases. Neither methods on ServiceConnected or onServiceDisconnected get called.
Logcat is empty!
What am I doing wrong?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inapp);
mServiceConn = new ServiceConnection() {
#Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
showtouser("Disconnection from server");
}
#Override
public void onServiceConnected(ComponentName name,
IBinder service) {
mService = IInAppBillingService.Stub.asInterface(service);
bindService(new
Intent("com.android.vending.billing.InAppBillingService.BIND"),
mServiceConn, Context.BIND_AUTO_CREATE);
showtouser("");
}
};
Ofc it does nothing, because callback method onServiceConnected() is called after successful bind. Now you are calling bindService() after it has been bounded. This code has no action. Call it e.g. after setContentView(). Check docs http://developer.android.com/guide/components/bound-services.html
Related
Good Day.I have the simplest scenario which could ever be!I have simple service connection which taken from google in app purchases example and this is my own code
public class MainActivity extends Activity {
IInAppBillingService mService;
ServiceConnection mServiceConn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mServiceConn = new ServiceConnection() {
#Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
Toast.makeText(MainActivity.this,"disconnected",Toast.LENGTH_LONG).show();
}
#Override
public void onServiceConnected(ComponentName name,
IBinder service) {
mService = IInAppBillingService.Stub.asInterface(service);
Toast.makeText(MainActivity.this,"connected",Toast.LENGTH_LONG).show();
}
};
}
#Override
public void onDestroy() {
super.onDestroy();
if (mService != null) {
unbindService(mServiceConn);
}
}
}
For first run it worked and it toasted.But afterwards it will never connect will never trigger onserviceconnected and onservicedisconnected.Doing researches in google i did not found any solution but only questions of familiar without any responsible answers.Thank you beforehand and please tell me what the heck to do?
You did not call bind Service:
create intent and call bindService
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mServiceConn = new ServiceConnection() {
#Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
Toast.makeText(MainActivity.this,"disconnected",Toast.LENGTH_LONG).show();
}
#Override
public void onServiceConnected(ComponentName name,
IBinder service) {
mService = IInAppBillingService.Stub.asInterface(service);
Toast.makeText(MainActivity.this,"connected",Toast.LENGTH_LONG).show();
}
};
IntentFilter filter1 = new IntentFilter("IntentFilterName");
bindService(filter1, mConnection, Context.BIND_AUTO_CREATE);
}
Code:
#Override
public void onResume() {
super.onResume();
Intent intent = new Intent(getActivity(), UserAPIService.class);
getActivity().bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
mService.fetchUserInfo();
}
private ServiceConnection mConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className,
IBinder service) {
UserAPIService.LocalBinder binder = (UserAPIService.LocalBinder) service;
mService = binder.getService();
mBound = true;
}
#Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
The user info is attempted to be fetched, but the service is null because it hasn't been bound yet. What is the best way to go about this? I could make the API call in the onServiceConnected method, but there has to be a better way.
You can call the
mService.fetchUserInfo();
inside the method
onServiceConnected(ComponentName className,IBinder service)
You cannot get the service instance unless your service is bound and it may require some time . onStart() is better place to bind a service.
I start my service in onCreate() (if onCreate is called the first time) and call bindService in onStart(). The service probaply works, but after calling bindService my local instance of the service is still null. Furthermore, is seems to be that getService() is not called.?
Here is some code:
#Override
protected void onCreate(Bundle savedInstanceState) {
...
if(savedInstanceState == null){
final Intent i = new Intent(this, HostService.class);
startService(i);
}
}
protected void onStart(){
super.onStart();
bindService(new Intent(this, StartGameActivity.class), connection, Context.BIND_AUTO_CREATE);
}
private ServiceConnection connection = new ServiceConnection(){
#Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
HostBinder binder = (HostBinder) arg1;
hostService = binder.getService();
isBound = true;
}
#Override
public void onServiceDisconnected(ComponentName arg0) {
isBound = false;
}
};
and in HostService:
...
private HostBinder binder = new HostBinder();
...
public class HostBinder extends Binder{
HostService getService(){
Log.d(TAG, "getService");
return HostService.this;
}
}
#Override
public IBinder onBind(Intent arg0) {
return binder;
}
Why is hostService still null, after onStart() is called and why is getService is not getting called ("getService" is not print in LogCat)?
thx & regards
You don't need to call startService(); it will be started by bindService().
In your onStart() method, you're creating an intent to launch StartGameActivity.class. Was that what you wanted, or did you mean to launch HostService.class?
If neither of those are your problem, we need more to go on. Can you put a logging statement inside your onServiceConnected() and onBind() methods so you can be sure they're called?
Any logcat messages that look interesting?
Are you talking to your service via the binder, or via messaging?
I got a problem with binding a service:
I use following code:
protected void onCreate(){
...
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
if(mService != null)
Log.e(TAG, "is instantiated (onClick)!");
});
}
protected void onStart(){
super.onStart();
bindService(new Intent(this, HostService.class), connection, Context.BIND_AUTO_CREATE);
if(mService == null)
Log.e(TAG, "is null (onStart)!");
}
...
After starting my activity "is null (onStart)!" is printed in LogCat.
When I click the button "is instantiated (onClick)!" is printed.
Why bindService() doesnt instantiate my service-instance mService directly and how can I solve the problems referring to this?
thx & regards
Why bindService() doesn't instantiate my service-instance mService directly
bindService() is asynchronous, like many operations in Android, so it occurred after your test in onStart.
how can I solve the problems referring to this?
Put your business logic in onServiceConnected() of your ServiceConnection, instead of immediately after the bindService() call.
Here is an example of the ServiceConnection code:
private ServiceConnection sConn;
private Messenger messenger;
...
#Override
protected void onCreate(Bundle savedInstanceState) {
// Service Connection to handle system callbacks
sConn = new ServiceConnection() {
#Override
public void onServiceDisconnected(ComponentName name) {
messenger = null;
}
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
// We are conntected to the service
messenger = new Messenger(service);
}
};
// We bind to the service
bindService(new Intent(this, ConvertService.class), sConn,
Context.BIND_AUTO_CREATE);
}
This is from Francesco Azzola's good example.
i have a problem with bindService. In my Activity i have the following code:
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {
mService = IPrimary.Stub.asInterface(service);
}
public void onServiceDisconnected(ComponentName className) {
mService = null;
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.login);
mApi = new ApiRequest(SIGNIN_METHOD);
boolean isConnected = bindService(new Intent(IPrimary.class.getName()),
mConnection, Context.BIND_AUTO_CREATE);
But isConnected is equals to false every time.
In my manifest file i have:
<service android:name=".DownloaderService">
<intent-filter>
<action android:name=".IPrimary" />
si i don't understand the problem. In logcat appears:
I/ActivityManager( 52): Displayed activity com.touristeye.code/.LogIn: 485918 ms (total 913151 ms)
Thank you
Expand that action:name to be the full value in the <action> element. It may be that the dot-prefix shorthand only works for the component element (e.g., <service>).
You shouldn't do that:
boolean isConnected = bindService(new Intent(IPrimary.class.getName()), mConnection, Context.BIND_AUTO_CREATE);
Please put the code when you handle service in private ServiceConnection mConnection = new ServiceConnection() {} ... I calls back and you have the service to handle there
We're not sure when the service is actually bond until we got callback from ServiceConnection
Here is the flow
Create your intent to call a service. You can either startService() or BindService() with BIND_AUTO_CREATE
Once the service is bond, it will create a tunnel to talk with it clients which is the IBinder
Interface. This is used by your AIDL Interface implementation and return the IBinder in
private final MyServiceInterface.Stub mBinder = new MyServiceInterface.Stub() {
public int getNumber() {
return new Random().nextInt(100);
}
};
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "Service OnBind()", Toast.LENGTH_LONG).show();
return mBinder;
}
Once it returns the mBinder, ServiceConnection that you created in the client will be called back and you will get the service interface by using this
mConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
mService = MyServiceInterface.Stub.asInterface(service);
};
Now you got the mService interface to call and retreive any service from that