3

I have this code:

Intent publishIntent = new Intent(HistoryDoneVsPlanned.this, MyIntentService.class);
publishIntent.putExtra(HistoryMap.KEY_WORKOUT_ID, workoutId);

where HistoryDoneVsPlanned is my current Activity and MyIntetService is a IntentService.

and my goal is pass the current context of the activity at the intentService like this:

publishIntent.putExtra("CONTEXT", HistoryDoneVsPlanned.this);

but is not possible.

Is a good practice? I need make the Toast and notification into another class (not Activity) and I need the context of the activity.

Dino Rino
  • 55
  • 1
  • 1
  • 8
  • 2
    The `IntentService` class is a subclass of `Context`, that way you can use `this` where you need a `Context` in your service. – pshegger Nov 29 '13 at 09:10
  • 1
    Why exactly do you need Activity's context in a Service? – waqaslam Nov 29 '13 at 09:10
  • I need Activity context in a Service beacause This service construct the objects and this objects call methods to do toast and notification and toast and notification if I dont understand bad need Activity Context – Dino Rino Nov 29 '13 at 09:16

2 Answers2

4

Is a good practice? I need make the Toast and notification into another class (not Activity) and I need the context of the activity.

No. This is not a good practice. You should show notification from Services. But it depends on application behavior you can do also.

Case 1: If Service/ Intent Service running and doing task into background..

You should show notification instead of Toast. Read How to show notification from background service?

Case 2: If Service/ Intent Service running and doing task into background, but returns result to an Activity (Which means User is present for your app)

You can show Toast in this case. Read Show toast at current Activity from service

Note : And do not pass context via Intent to MyIntentService. You can get Context into MyIntentService also by doing MyIntentService.this. And this is because all Android Components overrides Context class, on of them is IntentService.

Community
  • 1
  • 1
Pankaj Kumar
  • 78,055
  • 25
  • 161
  • 180
  • Sorry but I cant show notification from the service beacause the service call the methods (of a class not Activity) e into the methods I need to call notification. So If I understand well I do call notification into the class extern at the Service. – Dino Rino Nov 29 '13 at 10:02
  • 1
    If that method s of another class, then pass Context into parameter of that method. like method would be `yourMethod(Context mContext, otherParms)` and you should call this method from Service as `thatClassObject.yourMethod(MyIntentService.this, otherParams)`. Let me know if you find any confusion about.. – Pankaj Kumar Nov 29 '13 at 10:18
  • Yes I'm a little confused sorry...I think: 1)pass the context of the service at my class 2)with this context use notification but I dont knkow if it is correct – Dino Rino Nov 29 '13 at 10:52
  • The logic which you were discussing was correct :D. No problem to sen d Context object to a method as param. – Pankaj Kumar Nov 29 '13 at 13:48
1

When you are using the context in cross-activity usage, you should not bound the Activity context to the action, since then even if the activity is destroyed, it will not be garbage-collected, as it is still referenced from the running task. In those cases, you should use the Application context. For more look at the android developer blog.

Pradip
  • 2,993
  • 3
  • 20
  • 27
  • I dont understand the difference beetwen context and application context. – Dino Rino Nov 29 '13 at 09:31
  • 1
    take a look at this so# http://stackoverflow.com/questions/4128589/difference-between-activity-context-and-application-context – Pradip Nov 29 '13 at 09:34
  • 1
    Read at http://stackoverflow.com/questions/4128589/difference-between-activity-context-and-application-context and http://t.co/9R0bPWiKc5 . Last one is very good resource to know about Contexts. – Pankaj Kumar Nov 29 '13 at 09:35