How to get Android Local Service instanceIs there have some trick to get Service instance if I only allow to use startService()?Is there a way to run Python on Android?How to save an Android Activity state using save instance state?How to call a SOAP web service on AndroidStrange out of memory issue while loading an image to a Bitmap objectFling gesture detection on grid layoutClose/hide the Android Soft KeyboardWhy is the Android emulator so slow? How can we speed up the Android emulator?How to have Android Service communicate with ActivityIs there a unique Android device ID?Proper use cases for Android UserManager.isUserAGoat()?
If your plane is out-of-control, why does military training instruct releasing the joystick to neutralize controls?
Why isn't there research to build a standard lunar, or Martian mobility platform?
Making polynomials representing frequency of a character in a list
How to properly say "bail on somebody" in German?
During copyediting, journal disagrees about spelling of paper's main topic
For a hashing function like MD5, how similar can two plaintext strings be and still generate the same hash?
Use of って when quotation doesn't make sense
How to evolve human-like eyes that can stare at the sun without protection?
Why was hardware diversification an asset for the IBM PC ecosystem?
How would vampires avoid contracting diseases?
Why isn't pressure filtration popular compared to vacuum filtration?
How can I get a player to accept that they should stop trying to pull stunts without thinking them through first?
How to convert a file with several spaces into a tab-delimited file?
How can a dictatorship government be beneficial to a dictator in a post-scarcity society?
Terry Pratchett book with a lawyer dragon and sheep
Probability of sum of 2 random dice out of a 3d6 pool
Received a dinner invitation through my employer's email, is it ok to attend?
What is a solution?
Find The One Element In An Array That is Different From The Others
What steps should I take to lawfully visit the United States as a tourist immediately after visiting on a B-1 visa?
Is Trump personally blocking people on Twitter?
Integer Lists of Noah
Why didn't Nick Fury expose the villain's identity and plans?
Why didn't Thanos kill all the Dwarves on Nidavellir?
How to get Android Local Service instance
Is there have some trick to get Service instance if I only allow to use startService()?Is there a way to run Python on Android?How to save an Android Activity state using save instance state?How to call a SOAP web service on AndroidStrange out of memory issue while loading an image to a Bitmap objectFling gesture detection on grid layoutClose/hide the Android Soft KeyboardWhy is the Android emulator so slow? How can we speed up the Android emulator?How to have Android Service communicate with ActivityIs there a unique Android device ID?Proper use cases for Android UserManager.isUserAGoat()?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm starting a service in my application using startService.
I do not want to use bindService as I want to handle the service life time myself.
How can I get an instance to the service started if I do not use bindService? I want to be able to get a handler I've created in the service class to post messages from the activity.
Thanks.
/ Henrik
add a comment |
I'm starting a service in my application using startService.
I do not want to use bindService as I want to handle the service life time myself.
How can I get an instance to the service started if I do not use bindService? I want to be able to get a handler I've created in the service class to post messages from the activity.
Thanks.
/ Henrik
add a comment |
I'm starting a service in my application using startService.
I do not want to use bindService as I want to handle the service life time myself.
How can I get an instance to the service started if I do not use bindService? I want to be able to get a handler I've created in the service class to post messages from the activity.
Thanks.
/ Henrik
I'm starting a service in my application using startService.
I do not want to use bindService as I want to handle the service life time myself.
How can I get an instance to the service started if I do not use bindService? I want to be able to get a handler I've created in the service class to post messages from the activity.
Thanks.
/ Henrik
asked Jun 11 '10 at 18:33
HenrikHenrik
1,0193 gold badges18 silver badges45 bronze badges
1,0193 gold badges18 silver badges45 bronze badges
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
I do not want to use bindService as I
want to handle the service life time
myself.
That does not mean you have to avoid bindService(). Use both startService() and bindService(), if needed.
How can I get an instance to the
service started if I do not use
bindService?
Either use bindService() with startService(), or use a singleton.
By "using a singleton" you mean that I should declare my methods static in the service class?
– Henrik
Jun 11 '10 at 18:47
Worked as a charm with both bindService and startService. Thank you!
– Henrik
Jun 11 '10 at 19:04
4
@Henrik: FWIW, by singleton, I meant that you'd have a static reference to your service, put there by the service'sonCreate(), removed in the service'sonDestroy(). That runs the risk of memory leaks, so binding is recommended wherever possible.
– CommonsWare
Jun 11 '10 at 19:16
@CommonsWare if i do bindService() from my Activity, i get service instance perfectly but problem is when i go to device home screen from my application by clicking on device back button no action trigger from Notification expanded(Status Bar) bigView (RemoteView). I want to trigger play/pause, next froward button (My app is Music player app) action.
– Helal Khan
Mar 23 '14 at 10:00
add a comment |
Here's another approach:
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service
private Binder binder;
@Override
public void onCreate()
super.onCreate();
binder = new Binder();
@Override
public IBinder onBind(Intent intent)
return binder;
public class Binder extends android.os.Binder
public MyService getService()
return MyService.this;
onServiceConnected(...) can cast its argument to MyService.Binder and call getService() on it. This avoids the potential memory leak from having a static reference to the service. Of course, you still have to make sure your activity isn't hanging onto a reference.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3025299%2fhow-to-get-android-local-service-instance%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I do not want to use bindService as I
want to handle the service life time
myself.
That does not mean you have to avoid bindService(). Use both startService() and bindService(), if needed.
How can I get an instance to the
service started if I do not use
bindService?
Either use bindService() with startService(), or use a singleton.
By "using a singleton" you mean that I should declare my methods static in the service class?
– Henrik
Jun 11 '10 at 18:47
Worked as a charm with both bindService and startService. Thank you!
– Henrik
Jun 11 '10 at 19:04
4
@Henrik: FWIW, by singleton, I meant that you'd have a static reference to your service, put there by the service'sonCreate(), removed in the service'sonDestroy(). That runs the risk of memory leaks, so binding is recommended wherever possible.
– CommonsWare
Jun 11 '10 at 19:16
@CommonsWare if i do bindService() from my Activity, i get service instance perfectly but problem is when i go to device home screen from my application by clicking on device back button no action trigger from Notification expanded(Status Bar) bigView (RemoteView). I want to trigger play/pause, next froward button (My app is Music player app) action.
– Helal Khan
Mar 23 '14 at 10:00
add a comment |
I do not want to use bindService as I
want to handle the service life time
myself.
That does not mean you have to avoid bindService(). Use both startService() and bindService(), if needed.
How can I get an instance to the
service started if I do not use
bindService?
Either use bindService() with startService(), or use a singleton.
By "using a singleton" you mean that I should declare my methods static in the service class?
– Henrik
Jun 11 '10 at 18:47
Worked as a charm with both bindService and startService. Thank you!
– Henrik
Jun 11 '10 at 19:04
4
@Henrik: FWIW, by singleton, I meant that you'd have a static reference to your service, put there by the service'sonCreate(), removed in the service'sonDestroy(). That runs the risk of memory leaks, so binding is recommended wherever possible.
– CommonsWare
Jun 11 '10 at 19:16
@CommonsWare if i do bindService() from my Activity, i get service instance perfectly but problem is when i go to device home screen from my application by clicking on device back button no action trigger from Notification expanded(Status Bar) bigView (RemoteView). I want to trigger play/pause, next froward button (My app is Music player app) action.
– Helal Khan
Mar 23 '14 at 10:00
add a comment |
I do not want to use bindService as I
want to handle the service life time
myself.
That does not mean you have to avoid bindService(). Use both startService() and bindService(), if needed.
How can I get an instance to the
service started if I do not use
bindService?
Either use bindService() with startService(), or use a singleton.
I do not want to use bindService as I
want to handle the service life time
myself.
That does not mean you have to avoid bindService(). Use both startService() and bindService(), if needed.
How can I get an instance to the
service started if I do not use
bindService?
Either use bindService() with startService(), or use a singleton.
answered Jun 11 '10 at 18:41
CommonsWareCommonsWare
799k145 gold badges1934 silver badges1978 bronze badges
799k145 gold badges1934 silver badges1978 bronze badges
By "using a singleton" you mean that I should declare my methods static in the service class?
– Henrik
Jun 11 '10 at 18:47
Worked as a charm with both bindService and startService. Thank you!
– Henrik
Jun 11 '10 at 19:04
4
@Henrik: FWIW, by singleton, I meant that you'd have a static reference to your service, put there by the service'sonCreate(), removed in the service'sonDestroy(). That runs the risk of memory leaks, so binding is recommended wherever possible.
– CommonsWare
Jun 11 '10 at 19:16
@CommonsWare if i do bindService() from my Activity, i get service instance perfectly but problem is when i go to device home screen from my application by clicking on device back button no action trigger from Notification expanded(Status Bar) bigView (RemoteView). I want to trigger play/pause, next froward button (My app is Music player app) action.
– Helal Khan
Mar 23 '14 at 10:00
add a comment |
By "using a singleton" you mean that I should declare my methods static in the service class?
– Henrik
Jun 11 '10 at 18:47
Worked as a charm with both bindService and startService. Thank you!
– Henrik
Jun 11 '10 at 19:04
4
@Henrik: FWIW, by singleton, I meant that you'd have a static reference to your service, put there by the service'sonCreate(), removed in the service'sonDestroy(). That runs the risk of memory leaks, so binding is recommended wherever possible.
– CommonsWare
Jun 11 '10 at 19:16
@CommonsWare if i do bindService() from my Activity, i get service instance perfectly but problem is when i go to device home screen from my application by clicking on device back button no action trigger from Notification expanded(Status Bar) bigView (RemoteView). I want to trigger play/pause, next froward button (My app is Music player app) action.
– Helal Khan
Mar 23 '14 at 10:00
By "using a singleton" you mean that I should declare my methods static in the service class?
– Henrik
Jun 11 '10 at 18:47
By "using a singleton" you mean that I should declare my methods static in the service class?
– Henrik
Jun 11 '10 at 18:47
Worked as a charm with both bindService and startService. Thank you!
– Henrik
Jun 11 '10 at 19:04
Worked as a charm with both bindService and startService. Thank you!
– Henrik
Jun 11 '10 at 19:04
4
4
@Henrik: FWIW, by singleton, I meant that you'd have a static reference to your service, put there by the service's
onCreate(), removed in the service's onDestroy(). That runs the risk of memory leaks, so binding is recommended wherever possible.– CommonsWare
Jun 11 '10 at 19:16
@Henrik: FWIW, by singleton, I meant that you'd have a static reference to your service, put there by the service's
onCreate(), removed in the service's onDestroy(). That runs the risk of memory leaks, so binding is recommended wherever possible.– CommonsWare
Jun 11 '10 at 19:16
@CommonsWare if i do bindService() from my Activity, i get service instance perfectly but problem is when i go to device home screen from my application by clicking on device back button no action trigger from Notification expanded(Status Bar) bigView (RemoteView). I want to trigger play/pause, next froward button (My app is Music player app) action.
– Helal Khan
Mar 23 '14 at 10:00
@CommonsWare if i do bindService() from my Activity, i get service instance perfectly but problem is when i go to device home screen from my application by clicking on device back button no action trigger from Notification expanded(Status Bar) bigView (RemoteView). I want to trigger play/pause, next froward button (My app is Music player app) action.
– Helal Khan
Mar 23 '14 at 10:00
add a comment |
Here's another approach:
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service
private Binder binder;
@Override
public void onCreate()
super.onCreate();
binder = new Binder();
@Override
public IBinder onBind(Intent intent)
return binder;
public class Binder extends android.os.Binder
public MyService getService()
return MyService.this;
onServiceConnected(...) can cast its argument to MyService.Binder and call getService() on it. This avoids the potential memory leak from having a static reference to the service. Of course, you still have to make sure your activity isn't hanging onto a reference.
add a comment |
Here's another approach:
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service
private Binder binder;
@Override
public void onCreate()
super.onCreate();
binder = new Binder();
@Override
public IBinder onBind(Intent intent)
return binder;
public class Binder extends android.os.Binder
public MyService getService()
return MyService.this;
onServiceConnected(...) can cast its argument to MyService.Binder and call getService() on it. This avoids the potential memory leak from having a static reference to the service. Of course, you still have to make sure your activity isn't hanging onto a reference.
add a comment |
Here's another approach:
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service
private Binder binder;
@Override
public void onCreate()
super.onCreate();
binder = new Binder();
@Override
public IBinder onBind(Intent intent)
return binder;
public class Binder extends android.os.Binder
public MyService getService()
return MyService.this;
onServiceConnected(...) can cast its argument to MyService.Binder and call getService() on it. This avoids the potential memory leak from having a static reference to the service. Of course, you still have to make sure your activity isn't hanging onto a reference.
Here's another approach:
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service
private Binder binder;
@Override
public void onCreate()
super.onCreate();
binder = new Binder();
@Override
public IBinder onBind(Intent intent)
return binder;
public class Binder extends android.os.Binder
public MyService getService()
return MyService.this;
onServiceConnected(...) can cast its argument to MyService.Binder and call getService() on it. This avoids the potential memory leak from having a static reference to the service. Of course, you still have to make sure your activity isn't hanging onto a reference.
answered Jul 2 '14 at 21:47
Brodo FragginsBrodo Fraggins
4634 silver badges12 bronze badges
4634 silver badges12 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3025299%2fhow-to-get-android-local-service-instance%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown