android - Hello World Android IntentService - IntentService 的 enqueueWork() 函数不接受参数 MyIntentService.class
问题描述
我只想写一个“Hello World IntentService App”。不幸的是,我做不到。
问题:
MyIntentService.enqueueWork() 方法不起作用。
IntentService 的 enqueueWork() 函数不接受参数 MyIntentService.class
我在谷歌和 YouTube 上搜索了很多,但找不到任何有用的东西。非常感谢您提前。
资料来源:
https://developer.android.com/training/run-background-service/create-service
https://developer.android.com/training/run-background-service/send-request
https://developer.android.com/reference/android/support/v4/app/JobIntentService#enqueuework
第 1 步:创建一个新的 Android 项目
第二步:新建Service File -> New -> Service -> Service (IntentService) (只留下名字:MyIntentService)
// Step 3:
// Open: MyIntentService.java
// Just add a Toast in the method onHandleIntent() :
Toast.makeText(this, "Hello World!", Toast.LENGTH_SHORT).show();
// Step 4:
// Open: MainActivity.java.
// Add below code in an appropriate method:
Intent mServiceIntent = new Intent();
mServiceIntent.putExtra("name", "Harun");
int JOB_ID = 1000;
MyIntentService.enqueueWork(getApplicationContext(), MyIntentService.class, JOB_ID, mServiceIntent);
// 在运行前给出这个错误 enqueueWork 的错误 -> "Cannot resolve method 'enqueueWork(android.content.Context, java.lang.Class, int, android.content.Intent)'"
// 第 5 步:现在,我运行应用程序,并给出以下错误:
编译失败;有关详细信息,请参阅编译器错误输出。
错误:找不到符号方法 enqueueWork(Context, Class <- MyIntentService ->, int, Intent)
IntentService 的 enqueueWork() 函数不接受参数 MyIntentService.class
非常感谢!
解决方案
代替:
MyIntentService.enqueueWork()
它应该是:
JobIntentService.enqueueWork(getApplicationContext(), MyIntentService.class, JOB_ID, mServiceIntent);
推荐阅读
- html - 查看图像重叠导航栏的问题
- google-cast - v3 Google Cast 接收器是否会自动解析来自 hls 主播放列表的替代音轨,还是我必须在发送器中定义它们?
- python-3.x - 在 Tkinter 中,如何在定义中使用画布?
- php - 如果 css 中的位置不能移动元素怎么办?
- c++ - 特征矩阵 + Boost::序列化 / C++17
- c# - C# 将日期构建为带有前导 0 且没有分隔符的字符串
- azure-cosmosdb - 跟随边谁的属性与 Gremlin 在源顶点上匹配一个
- angularjs - 在 Internet Explorer 11 中查看 PDF
- python - 如何从 json 查询中获取和打印特定数据?
- java - 如何合并两个ArrayList