ServieConnection Not Working At All Android - android

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);
}

Related

I want the instance of the Background service which is running

In this, the service is been create repeated times but I need to get the instance of running service so that I can start the timer task. The object of this service can get from any activity. The basic aim is to start the timer task after the particular button click.
public class MainActivity extends Activity {
MyService mService;
boolean mBound = false;
int b=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_start_timer).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mBound) {
mService.runTimerTask(++b);
}
}
});
}
#Override
protected void onStart() {
super.onStart();
// Bind to LocalService
Intent intent = new Intent(this, MyService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
#Override
protected void onStop() {
super.onStop();
unbindService(mConnection);
mBound = false;
}
/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className,
IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
MyService.LocalBinder binder = (MyService.LocalBinder) service;
mService = binder.getService();
mBound = true;
}
#Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
}
You could create a ServiceManager class and route all methods through that and store it in the application:
public class YourApplication extends Application {
...
private ServiceManager serviceManager = ...;
public ServiceManager getServiceManager() {
return serviceManager;
}
}
In MainActivity:
ServiceManager serviceManager = ((YourApplication)getApplication()).getServiceManager();
MyService runningService = serviceManager.getRunningService();
The ServiceManager would handle unbinding etc and all methods that involve the state of the service. So you just ask the ServiceManager for the running service. Where you have ServiceConnection::onServiceConnected doing the work, delegate that work to the ServiceManager so it can keep state and share it with the rest of the application:
public void onServiceConnected(ComponentName className,
IBinder service) {
((YourApplication)getApplication()).getServiceManager().bind(service);
}
It's a general technique for sharing object state across an application but you'd need to tailor it to fit.

Why Service Connection is not called in onResume?

I am working on background service where i am updating one Arraylist in the service, from the Arraylist i am getting the index and playing the songs.
So what's the issue now is if i'll Intent to another activity and inside that activity i am updating the Arraylist to the service but when i am finishing the activity and coming back to MainActivity it's not updating the Arraylist again.
I am using realm arraylist for storing the data.
#Override
protected void onStart() {
super.onStart();
songConnection();
if (playIntent == null) {
playIntent = new Intent(this, MusicService.class);
bindService(playIntent, musicServiceConn, Context.BIND_AUTO_CREATE);
startService(playIntent);
}
}
#Override
protected void onResume() {
super.onResume();
songConnection();
}
public void songConnection() {
musicServiceConn = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
MusicService.MusicBinder binder = (MusicService.MusicBinder) service;
musicSrv = binder.getService();
musicSrv.setList(musicRealmResults);
musicBound = true;
}
#Override
public void onServiceDisconnected(ComponentName name) {
musicBound = false;
}
};
}
Here the musicRealmResults is the Realm Arraylist.
In Albums.java
musicRealmResults = realm.where(Music.class).equalTo("albums", albums).findAllSorted("albums", Sort.ASCENDING);
In MainActivity.java
musicRealmResults = realm.where(Music.class).findAll();
So in both activity the Arraylist size will be different and as per the size it should update to the Service.
So please tell me why it's not updating in onResume when the activity finish.
Please kindly go through my post and suggest me some solution.
change your code like this:
#Override
protected void onStart() {
songConnection();// call this method before calling super
super.onResume();
}
public void songConnection() {
musicServiceConn = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
MusicService.MusicBinder binder = (MusicService.MusicBinder) service;
musicSrv = binder.getService();
musicSrv.setList(musicRealmResults);
musicBound = true;
}
#Override
public void onServiceDisconnected(ComponentName name) {
musicBound = false;
}
};
if (playIntent == null) {
playIntent = new Intent(this, MusicService.class);
bindService(playIntent, musicServiceConn, Context.BIND_AUTO_CREATE);
startService(playIntent);
}
}
now your your service when will called correctly But make sure you stop the Service at some point of time before it'
s started again. ThankYou I hope this was helpful.

inApp ServiceConnection doesn't respond

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

Can't Access My Service Methods

i'm at my wits end, following the examples on the android devguide to creating a bound service here: http://developer.android.com/guide/topics/fundamentals/bound-services.html
i created a bound service to play media files however, every time i try to access any of it's public methods from my activity, i get the force close error with a NullPointerException on logcat.
MediaService.java
public class MediaService extends Service {
private MediaPlayer mediaPlayer;
private SeekBar seeker;
private Boolean curRdy;
private Boolean paused;
private Cursor cursor;
private int columIndex;
private int position;
public class LocalBinder extends Binder {
MediaService getService() {
return MediaService.this;
}
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
#Override
public void onCreate() {
super.onCreate();
mediaPlayer = new MediaPlayer();
curRdy = false;
paused = false;
}
#Override
public IBinder onBind(Intent intent) {
return (IBinder) new LocalBinder();
}
PlayerTab.java
public class PlayerTab extends Activity {
private Intent intent;
private SeekBar seeker;
private MediaService serv;
private ServiceConnection servCon = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className, IBinder service) {
serv = ((MediaService.LocalBinder) service).getService();
}
#Override
public void onServiceDisconnected(ComponentName arg0) {
//-----
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
intent = new Intent(PlayerTab.this, MediaService.class);
seeker = (SeekBar) findViewById(R.id.seeker);
}
#Override
protected void onStart() {
super.onStart();
startService(intent);
bindService(intent, servCon, Context.BIND_AUTO_CREATE);
serv.loadPlayerSeeker(seeker);
}
serv.loadPlayerSeeker() is one of the public methods in MediaService, when ever i try to access it or any of the other methods, it just fails. the service will create and start just fine, it just doesn't seem bounded to the PayerTab activity properly.
You can't call serv.loadPlayerSeeker(seeker); until AFTER public void onServiceConnected(ComponentName className, IBinder service) { is invoked.

Null Pointer When Access Data Structure Located Within Android Service

I have a service running in my Android application which contains a HashMap that I would like to use in an Activity. So I bound the service to my activity and created a link between to two in order to use the data structure.
This is how I did it so far:
Activity (relevant code):
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
imService = ((IMService.IMBinder) service).getService();
}
public void onServiceDisconnected(ComponentName className) {
imService = null;
Toast.makeText(ViewFlipperTest.this,
R.string.local_service_stopped, Toast.LENGTH_SHORT).show();
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bindService(new Intent(ViewFlipperTest.this, IMService.class),
mConnection, Context.BIND_AUTO_CREATE);
.
.
.
}
protected void onResume() {
super.onResume();
bindService(new Intent(ViewFlipperTest.this, IMService.class),
mConnection, Context.BIND_AUTO_CREATE);
}
private void getMap(ViewFlipper flipper2) {
Map<Integer, internalChatObj> tempMap = imService.getMapwData();
}
Service (relevant code):
public class IMService extends Service implements IAppManager{
.
.
.
public Map getMapwData(){
if(getNumberOpenChats()>0){
return openChatMapwData;
}
else
return null;
}
}
Each time I try and run the method getMap(), I get a null pointer. I even put a check to see if the imService was null and it was null each time. It seems that it is trying to obtain the data from the Service but the binding to the service is null, is that right? I have a check in the Service to send null if the map is empty, so that shouldn't be a problem.
Any help?
Edit based on Will's tips
New code
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
imService = ((IMService.IMBinder) service).getService();
dialog.dismiss();
}
public void onServiceDisconnected(ComponentName className) {
imService = null;
Toast.makeText(ViewFlipperTest.this,
R.string.local_service_stopped, Toast.LENGTH_SHORT).show();
}
};
public void onCreate(Bundle savedInstanceState) {
dialog = ProgressDialog.show(getApplicationContext(), null,"Loading..", true, false);
}
Edit II
IMBinder Class (Seems standard, no?)
public class IMBinder extends Binder {
public IAppManager getService() {
return IMService.this;
}
}
Is it necessary to have IMService.this instead of this?
It really seems like a timing issue; you shouldn't call the getMap function unless you are certain the imService variable exists, which is after a call to onServiceConnected.

Categories

Resources