通过上下文的IntentService

编程入门 行业动态 更新时间:2024-10-26 18:18:41
本文介绍了通过上下文的IntentService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这样的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 。这是因为所有的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

本文发布于:2023-11-23 02:19:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619724.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上下文   IntentService

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!