草庐IT

intentService

全部标签

android - 如何从 IntentService 创建 toast ?它卡在屏幕上

我正在尝试让我的IntentService显示Toast消息,但是当从onHandleIntent消息发送它时,toast显示但卡住了,屏幕永远不会离开。我猜是因为onHandleIntent方法不会发生在主服务线程上,但是我该如何移动它呢?有人遇到过这个问题并解决了吗? 最佳答案 在onCreate()中初始化一个Handler然后从你的线程发布到它。privateclassDisplayToastimplementsRunnable{StringmText;publicDisplayToast(Stringtext){mText

android - IntentService 不会显示 Toast

我创建的这个IntentService将在onStartCommand()和onDestroy()中显示Toast,但不会在onHandleIntent()中显示。我是否遗漏了有关IntentService限制的一些信息?publicclassMyServiceextendsIntentService{privatestaticfinalStringTAG="MyService";publicMyService(){super("MyService");}@OverrideprotectedvoidonHandleIntent(Intentintent){cycle();}@Overr

android - IntentService 的 onHandleIntent(Intent) 获取空参数

我正在使用IntentService在android上为我的应用程序运行后台服务。奇怪的是,我收到很多崩溃报告,其中传递给onHandleIntent的Intent为空。我什至不确定这是怎么可能的,而且看起来非常奇怪。谁能说明为什么会发生这种情况?STACK_TRACEjava.lang.NullPointerExceptionatcom.example.MyService.onHandleIntent(MyService.java:466)atandroid.app.IntentService$ServiceHandler.handleMessage(IntentService.ja

android - 从 Activity 启动 IntentService 并在 IntentService 完成时刷新 Activity

在我的Android应用程序中,我有一个带有适配器的简单ListView。有一个繁重的查询是用数据填充ListView。所以我把它放到另一个线程中运行的IntentService中。IntentService通常单独运行,单独运行,只是为了查询一些数据并将其插入SQLite数据库。但现在我想有以下可能:Activity使用startService()启动IntentService。IntentService完成了繁重的工作。IntentService完成后,应将结果通知Activity,以便刷新Activity以显示新数据。这可能吗?我在StackOverflow上阅读了很多关于这个主

android - 在 IntentService 中创建的 Toast 永远不会消失

我有一个IntentService可以下载一些文件。问题是我像这样在IntentService中创建了一个ToastToast.makeText(getApplicationContext(),"somemessage",Toast.LENGTH_SHORT).show();如果我退出应用程序,Toa​​st将永远不会消失事件。销毁它的唯一方法是终止进程。我做错了什么? 最佳答案 问题是IntentService没有在主应用程序线程上运行。您需要为主线程(在onCreate()中)获取Handler并将Toast作为Runnable

android - IntentService 错误 : No default Constructor

我是Android新手,我正在尝试使用IntentService,但我收到一个错误,即它没有默认构造函数。我试过重启AndroidStudio还是不行。packagecom.example.hitesh.kuchbhi;importandroid.app.IntentService;importandroid.app.NotificationManager;importandroid.app.PendingIntent;importandroid.bluetooth.BluetoothAdapter;importandroid.content.Context;importandroid

android - 从 IntentService 获取位置时向死线程上的 Handler 发送消息

我的应用需要定期进行位置修复,即使手机没有唤醒也是如此。为此,我将IntentService与Commonsware慷慨提供的模式一起使用。https://github.com/commonsguy/cwac-wakeful为了获得位置修复,我依赖于名为Fedor的成员提供的以下代码。Whatisthesimplestandmostrobustwaytogettheuser'scurrentlocationonAndroid?.我稍微修改它以返回null而不是获取最后一个已知位置它们在不结合时都可以正常工作:我可以在Activity中从Fedor的类中获取位置修复,并且可以使用Comm

android - 如何将 IntentService 的结果返回到 Activity?

我正在使用IntentService通过JSON处理与服务器的网络通信。JSON/服务器部分工作正常,但我无法将结果返回到需要的地方。以下代码显示了我如何从onClick()内部启动Intent服务,然后让服务更新全局变量以将结果传递回主Activity。publicclassGXActivityextendsActivity{privatestaticfinalStringTAG="GXActivity";@OverridepublicvoidonCreate(BundlesavedInstanceState){//===calledwhentheactivityisfirstcre

android - 如何通过 Activity 的取消按钮强制 IntentService 立即停止?

我有一个从Activity启动的IntentService,我希望能够使用Activity中的“取消”按钮立即从该Activity停止该服务。一旦按下“取消”按钮,我希望服务停止执行代码行。我发现了许多与此类似的问题(即here、here、here、here),但没有好的答案。Activity.stopService()和Service.stopSelf()立即执行Service.onDestroy()方法,然后让中的代码onHandleIntent()在销毁服务之前一直完成。由于显然没有保证立即终止服务线程的方法,我能找到的唯一推荐解决方案(here)是在服务中有一个bool成员变量

android - 我应该为我的应用程序使用 AsyncTask 还是 IntentService?

我一直在阅读有关与Android的互联网连接的信息,并注意到有不同的方法来处理这个问题,即AsyncTask和IntentService.但是,我仍然不确定要使用哪一个。我的应用程序基本上是一个带有谷歌地图的位置/路径查找器。我的互联网连接将用于在map的某个半径范围内查找最近的路径。因此,每次用户将map移动或滑动到新位置时,它都会更新最近的路线。它还将添加一条新路径,并允许用户对路径进行评分。AsyncTask是否足够,或者我应该使用IntentService? 最佳答案 它们可以用于不同的目的。AsyncTask是一个方便的线