我有一个IntentService,它将需要对我的Web服务器进行的Web服务调用进行排队.因此,每个Intent都是要进行的Web服务调用.
我想设置一些东西,我的应用程序可以询问这个IntentService,如果它有任何含有特定数据的Intent(IE:"你是否已经在等待向云询问x数据?或者我是否需要告诉你做到了吗?").
关于如何扩展IntentService来做这个有什么建议吗?是否可以遍历IntentService的Intent队列?或者我需要获取IntentService代码并进行更改吗?
我唯一的另一个想法是向数据库添加一个表并记录哪些调用在队列中,每个日志在完成后从表中删除.
如果我错了,请纠正我:
1)甲服务被用来在后台执行长的任务.服务在UI线程中运行,因此如果有长任务,那么它可能会冻结我们的UI.只要我们告诉它停止,服务将继续独立于应用程序运行.
2)一种IntentService另一方面用于在单独的线程中执行短任务.它在完成任务时自动终止.
我该怎么做:
1)每5秒检查一次位置
2)如果位置发生变化,请将其发送到服务器并使用新的位置值更新UI
让我困惑的是:
我应该使用Service或IntentService,因为我需要在5秒后连续执行它,并且不希望我的UI线程无响应.
此应用程序将用于跟踪车辆.
在我与同事一起开发的当前应用程序中,我们使用IntentServices和Volley调用来处理RESTful API网络请求.它只是简单的JSON字符串数据和一些小图像.
我对那些在处理网络请求方面经验丰富的问题是这样的:那里有更合适或更清洁的东西吗?
根据我的理解,使用IntentService的优势在于它在主线程的后台运行,并且通常是Android OS杀死的最后一件事.缺点是IntentServices按顺序运行.
我一直在阅读很多关于RxJava和Retrofit的内容,并且觉得我们的需求可以通过这种组合得到更好的服务.改造本身就足够了,但我真的很感激第三方洞察力.
android android-networking rx-java retrofit android-intentservice
我想请某人解释一下,HandlerThread和IntentService之间的主要区别是什么,以及主要的用例场景是什么?
我知道HandlerThread包含一个Looper,它管理messageQueue,由Handler提供.据我所知,你可以推送HandlerThread的任务,它将执行.使用非UI相关的长时间运行操作非常棒,您可以通过runOnUiThread()将结果推送回UI .
相比之下,IntentService适用于长时间运行,非uUI相关的操作,可以按顺序执行任务,当它完成调用selfStop()的作业以完成关闭时.如果一个IntentService正在处理一个任务,当一个新请求到达它时,它会添加到队列并处理第二个,当它完成第一个任务时.
从我的观点来看,他们以同样的方式做同样的工作.假设我有一个应用程序,用户点击按钮,我开始下载文件.如果用户多次点击,则新任务将排队,仅在第1次完成时启动第2次.我该怎么用?IntentService还是HandlerThread?
我已经实现JobIntentService了一些后台任务,可以在较旧的Android设备上运行(Android O之前).我看到意图立即处理,但在Android O设备上JobIntentService.onHandleWork()执行之前有一些延迟.我知道意图是串行处理的,但即使队列中没有处理意图,我也会看到延迟.是因为作业调度是在Android O内部处理的吗?
在这里,Android文档说
"当作为pre-O服务运行时,排队工作的行为通常会立即启动服务,无论设备是在打瞌睡还是在其他条件下.当作为Job运行时,它将遵循标准的JobScheduler策略使用
setOverrideDeadline(long)0的作业:当设备打瞌睡时,作业将无法运行,如果设备处于强大的内存压力下并且需要大量运行作业,则可能会延迟服务.
即使我的应用程序针对API 25但在OS Android O上运行,上述语句是否有效?如果是这样,是否有任何解决方法可以立即在Android O上启动服务/作业?
我目前的实施:
public class MySampleService extends JobIntentService {
private static final int JOB_ID = 1000;
public MySampleService() {
}
/**
* Convenience method for enqueuing work in to this service.
*/
public static void enqueueWork(Context context, Intent work) {
enqueueWork(context, MySampleService.class, JOB_ID, work);
}
/**
* Interpret the incoming intent actions and handle it appropriately.
* @param workIntent
*/
@Override
protected void onHandleWork(@NonNull …Run Code Online (Sandbox Code Playgroud) android android-intent android-intentservice android-8.0-oreo
在Android文档中,服务的" onStartCommand()"的意图是a param,根据文档:
"在Intent供给startService(Intent),给定的.如果它的进程已经走了之后,正在重新启动该服务,它以前返回任何东西,除了这可能是零START_STICKY_COMPATIBILITY."
但是,返回值START_REDELIVER_INTENT应该在重新启动服务时返回原始意图.
任何人都可以解释为什么一个intent可以为null,即使flag被设置为START_REDELIVER_INTENT?
service android android-intent android-service android-intentservice
final Handler handler = new Handler();
LOG.d("delay");
handler.postDelayed(new Runnable() {
@Override public void run() {
LOG.d("notify!");
//calling some methods here
}
}, 2000);
Run Code Online (Sandbox Code Playgroud)
"延迟"确实显示在日志中,但根本不显示.并且在其中调用的方法run()也根本不被调用.任何人都可以帮助解释为什么会发生这种情况,我做错了吗?
具有此代码的类扩展了IntentService,这会是一个问题吗?
============================
更新:我将此代码放在扩展的类中IntentService.我发现它唯一有用的地方是构造函数.但我需要把它放在onHandleIntent方法中.所以我检查了文档onHandleIntent,它说:
在工作线程上调用此方法并处理请求.一次只处理一个Intent,但处理发生在独立于其他应用程序逻辑运行的工作线程上.因此,如果此代码需要很长时间,它将阻止对同一个IntentService的其他请求,但它不会阻止其他任何内容.处理完所有请求后,IntentService会自行停止,因此您不应该调用stopSelf.
所以基于我得到的结果,我觉得我不能postDelayed在"工作线程"中使用.但是,任何人都可以解释这一点,比如为什么这不适用于工作线程?提前致谢.
android postdelayed intentservice android-handler android-intentservice
我有一个应用程序,您应该可以使用我将在此问题中发布的代码完全且非常轻松地重新创建.这是清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcasttest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.broadcasttest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.example.broadcasttest.TestReceiver"
android:label="@string/app_name"
android:enabled="true" >
</receiver>
<intentservice
android:name="com.example.broadcasttest.MonitorService"
android:enabled="true" >
<intent-filter>
<action android:name="com.example.broadcasttest.MonitorService" />
</intent-filter>
</intentservice>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
如您所见,它包含一个活动,一个(唤醒)广播接收器和一个intentservice,都在同一个包中.活动在启动时开始,这是代码:
package com.example.broadcasttest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); …Run Code Online (Sandbox Code Playgroud) 我正在尝试将我的应用程序从后台带到前台。在onHandleIntent()我的自定义IntentService课程中,我有:
Intent intent = new Intent();
intent.setClass(getApplicationContext(), MainActivity.class); // Also tried with "this" instead of getApplicationContext()
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
现在这段代码乍一看可以工作,但我发现了一个不起作用的场景。如果您打开了应用程序并通过主页按钮将其置于后台并startActivity()在大约 5 秒内执行,则应用程序到达前台之前将会有延迟。这是一个已知的实现,您可以在 stackoverflow 上找到讨论的主题。在这种情况下,应用程序成功从后台转到前台。
如果您重复上面相同的实验,但不要等待应用程序进入前台,而是在手机上浏览(滚动、滑动等)(我正在浏览谷歌游戏商店)。结果是,它将startActivity()被调用,但应用程序不会进入前台。
我不是在寻求解决方案,而是更多地解释为什么会发生这种情况。这是有意的行为吗?
android background android-intent start-activity android-intentservice
以下FetchAddressIntentService实现IntentService(在 kotlin 中):
class FetchAddressIntentService //Constructor of this service
: IntentService(INTENTTAG) {
//Receiver where results are forwarded from this service
protected var resultReceiver: ResultReceiver? = null
private val TAG by lazy { this::class.java.simpleName }
//Intent Service handler
override fun onHandleIntent(intent: Intent?) { //Errormessages
var errorMessage = ""
if (intent != null) {
resultReceiver =
intent.getParcelableExtra(RECEIVER)
}
//Checks if receiver was properly registered
if (resultReceiver == null) {
Log.wtf(
TAG,
"No reciever received. There is nowhere to …Run Code Online (Sandbox Code Playgroud) android kotlin android-intentservice jobintentservice android-11
android ×10
android-11 ×1
background ×1
java ×1
kotlin ×1
postdelayed ×1
retrofit ×1
rx-java ×1
service ×1