通过上下文的IntentService [英] Pass a Context an IntentService

查看:210
本文介绍了通过上下文的IntentService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code:

 意图publishIntent =新意图(HistoryDoneVsPlanned.this,MyIntentService.class);
publishIntent.putExtra(HistoryMap.KEY_WORKOUT_ID,workoutId);

其中HistoryDoneVsPlanned是我目前的活动和MyIntetService是IntentService。

和我的目标是通过活动的当前上下文在intentService是这样的:

  publishIntent.putExtra(背景,HistoryDoneVsPlanned.this);

,但是不可能的。

是一个好的做法?我需要做的面包和通知到另一个类(不活动),我需要活动的背景。


解决方案

  

是一个好的做法?我需要做的面包和通知成
  另一个类(不活动),我需要活动的背景。


没有。这是不是一个好的做法。你应该从服务显示通知。但是这取决于应用程序的行为也可以做。

案例1:如果服务/意图服务的运行和做任务到后台。

你应该表现出通知,而不是面包。阅读的<一个href=\"http://stackoverflow.com/questions/7701942/how-to-show-notification-from-background-service\">How显示从后台服务通知?

案例2:如果服务/意图服务的运行和做任务到背景,但返回结果给活动(这意味着用户是present为您的应用程序)

您可以在此情况下,显示吐司。阅读从服务当前活动显示敬酒

注意: 不要通过意图 MyIntentService传递上下文。你可以得到上下文 MyIntentService 也做 MyIntentService.this 。这是因为所有的Andr​​oid 组件覆盖上下文类,它们是 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆