我目前正在尝试在用户启动或停止行走时唤醒我的应用程序。这是代码。我已经使用了未决意图,以便即使申请被杀死也会触发。
代码有效,待处理的意图将事件发送到广播接收器。但是,几个小时后,它停止工作。
我读到的待处理意图存储在RAM中。OS是否会清除这些悬而未决的事件以节省内存?如果那时我该如何确保我的待处理意图一直有效?
public void subscribe(Context context){
List<ActivityTransition> transitions = new ArrayList<>();
transitions.add(
new ActivityTransition.Builder()
.setActivityType(DetectedActivity.WALKING)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
.build());
transitions.add(
new ActivityTransition.Builder()
.setActivityType(DetectedActivity.WALKING)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
.build());
ActivityTransitionRequest request = new ActivityTransitionRequest(transitions);
Intent intent = new Intent(context, TransitionsReceiver.class);
intent.setAction(TransitionsReceiver.TRANSITIONS_RECEIVER_ACTION);
pendingIntent = pendingIntent.getBroadcast(context,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
Task<Void> task =
ActivityRecognition.getClient(context)
.requestActivityTransitionUpdates(request, pendingIntent);
task.addOnSuccessListener(
new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void result) {
Log.i("Shield", "Transitions Api was successfully registered.");
}
});
task.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.e("Shield", "Transitions Api could not be registered: " + e);
}
});
}
这取决于您在手机中安装的ROM。许多人都有杀死您的应用程序和剩下的流程的电池节省配置文件,例如您的意图。
本网站有说明避免以下说明:https://dontkillmyapp.com/