我有这样的code:
意图publishIntent =新意图(HistoryDoneVsPlanned.this,MyIntentService.class);publishIntent.putExtra(HistoryMap.KEY_WORKOUT_ID,workoutId);其中HistoryDoneVsPlanned是我目前的活动和MyIntetService是IntentService。
和我的目标是通过活动的当前上下文在intentService是这样的:
publishIntent.putExtra(背景,HistoryDoneVsPlanned.this);,但是不可能的。
是一个好的做法?我需要做的面包和通知到另一个类(不活动),我需要活动的背景。
解决方案是一个好的做法?我需要做的面包和通知成 另一个类(不活动),我需要活动的背景。
的没有。这是不是一个好的做法。你应该从服务显示通知。但是这取决于应用程序的行为也可以做。的
案例1:的如果服务/意图服务的运行和做任务到后台。
的你应该表现出通知,而不是面包。阅读的How显示从后台服务通知?
案例2:的如果服务/意图服务的运行和做任务到背景,但返回结果给活动(这意味着用户是present为您的应用程序)
的您可以在此情况下,显示吐司。阅读的从服务当前活动显示敬酒
的 注意:的 不要通过意图到 MyIntentService传递上下文。你可以得到上下文到 MyIntentService 也做 MyIntentService.this 。这是因为所有的Android 组件覆盖上下文类,它们是 IntentService 。的
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.
解决方案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.
更多推荐
通过上下文的IntentService
发布评论