我的应用程序有一个后台service运行,可以让用户保持最新状态location并每隔五分钟将其更新一次.要location连续运行此更新过程,我使用警报管理器从其service自身设置下一个执行时间.然而,当我在安装应用程序Nokia 6运行Android 8.1它工作了一段时间,如果我保持手机闲置一段时间后,我service将得到与应用程序的下一个警报也被从系统中清除杀死alarm manager.我的猜测是空闲时间使手机进入doze mode.但是,我不明白警报管理员为何被清除.根据我的理解,doze mode应定期打开维护窗口以执行任何待处理的任务.
为了缓解这个问题,我试图应用JobScheduler service之上AlarmManager,其中每15分钟运行.这样做的目的jobscheduler是重新启动service其中的alarmmanager内容,因此即使它被杀死并且警报被清除,jobscheduler也会重新启动service.
在我测试了这个补丁并保持一段时间之后idle mode,它导致了两者JobScheduler Service并且Service其中的警报被预定的作业和警报从系统中清除而被杀死.
在Android文档中我们可以使用它JobScheduler来减轻其后台执行限制.为了测试这个,我services在测试应用程序时强制杀死了两个,但是已经预定的作业没有被清除,并且它使service警报再次成功运行.我不明白这种行为的原因,虽然Evernote的家伙给出的解释可以在这里安排这个场景Android Job by Evernote
对这种异常行为的任何想法?
测试环境细节
Nokia 6 (TA-1021)Android 8.1.0我正在尝试在我的项目中实现双向绑定。
但是在布局中使用条件语句时出现错误。
我创建了模型类 model.java,其中定义了 getter setter。
模型.java
public class Model {
public String name;
public string id;
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.name = website;
}
public int getId() {
return name;
}
public void setId(String id) {
this.id = id;
}
}
Run Code Online (Sandbox Code Playgroud)
fragment_details.java 中的编辑文本
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".view.fragment.CompanyDetailsFragment">
<data>
<variable
name="modelData"
type=".Model" />
</data>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@={modelData.id}"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:text="@={modelData.website ?? "NA"}"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)
详细信息Fragment.java …
任何人都JobIntentservice为O oreo和pre O.我JobIntentService从developer.android.com上读到了更多关于它的内容, 但他们没有提到完整的例子.
如何使用改造上传多个文件,
我尝试使用以下代码上传文件:
ApiService:
@Multipart
@POST("uploadData.php")
Call<ServerResponse> uploadFile(@Part MultipartBody.Part file,
@Part("name") RequestBody name,
@Part MultipartBody.Part img,
@Part("imgname") RequestBody imgname);
Run Code Online (Sandbox Code Playgroud)
和上传方法:
private void uploadFile() {
File file = new File(Environment.getExternalStorageDirectory() + "/Download/audio2.wav");
File file2 = new File(Environment.getExternalStorageDirectory() + "/Download/Salty.png");
RequestBody mFile = RequestBody.create(MediaType.parse("audio/*"), file);
MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", file.getName(), mFile);
RequestBody filename = RequestBody.create(MediaType.parse("text/plain"), file.getName());
RequestBody mFile2 = RequestBody.create(MediaType.parse("image/*"), file2);
MultipartBody.Part fileToUpload2 = MultipartBody.Part.createFormData("file", file2.getName(), mFile2);
RequestBody filename2 = RequestBody.create(MediaType.parse("text/plain"), file2.getName());
ApiService uploadImage = ApiClient.getClient().create(ApiService.class);
Call<ServerResponse> fileUpload = uploadImage.uploadFile(fileToUpload, …Run Code Online (Sandbox Code Playgroud) I'm trying to use Firebase Cloud Messaging to send a command to my Android app that prompts it to determine its current location. This is done in the class FCMService.
The class SingleShotLocationProvider then performs the actual work by requesting location updates using the FusedLocationProviderClient. However, the callback fusedTrackerCallback is never called ever, although the necessary permissions are granted and GPS is switched on. Why?
FCMService class
public class FCMService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage …Run Code Online (Sandbox Code Playgroud) 我的应用程序中有一个类似于以下的代码
class MyFragment : Fragment(), CoroutineScope by MainScope() {
override fun onDestroy() {
cancel()
super.onDestroy()
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
doSomething()
}
private fun doSomething() = launch {
val data = withContext(Dispathers.IO) {
getData()
}
val pref = context!!.getSharedPreferences("mypref", MODE_PRIVATE)
pref.edit().putBoolean("done", true).apply()
}
}
Run Code Online (Sandbox Code Playgroud)
在生产中,我得到很多NPEs的doSomething(),而访问context。
我的假设是coroutine调用cancel()in后被取消onDestroy(),所以我没有费心检查context空值。但看起来continues即使在cancel()被调用之后也能执行。我认为这会发生,如果cancel()在完成后调用withContext和恢复之前coroutines。
所以我替换doSomething()了以下内容。
private fun doSomething() = launch …Run Code Online (Sandbox Code Playgroud) 当我第一次开始Android Studio打开现有项目,然后单击时Run,我收到错误消息Error running app, No target device found.我Samsung插电了ADB debugging enabled.它正确显示adb devices命令和ADBshell工作正常.我试着adb start-server没有快乐.奇怪的是,如果我先启动'Debug app'然后'Run'它运行正常,但我不明白为什么这个烦人的步骤是必要的.其他StackOverflow答案说做"编辑配置",但没有显示建议的选项.我跑AS V3.01.任何线索?
为什么我的材质日历视图无法解析,这是我的模块:app
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.project.cms"
minSdkVersion 22
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android- optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.0.0-beta1'
implementation 'com.android.support:design:26.0.0-beta1'
implementation 'com.android.support:support-v4:26.0.0-beta1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso- core:3.0.2'
implementation 'com.github.ybq:Android-SpinKit:1.2.0'
implementation 'com.android.support:cardview-v7:26.0.0-beta1'
implementation 'com.android.support:recyclerview-v7:26.0.0-beta1'
implementation 'com.github.prolificinteractive:material-calendarview:2.0'
}
Run Code Online (Sandbox Code Playgroud)
这是我的构建文件
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
// …Run Code Online (Sandbox Code Playgroud) android ×7
java ×2
adb ×1
alarmmanager ×1
calendarview ×1
coroutine ×1
data-binding ×1
fusedlocationproviderclient ×1
gps ×1
kotlin ×1
location ×1
php ×1
retrofit2 ×1