我目前正在尝试在用户开始或停止步行时唤醒我的应用程序。这是代码。我使用了待处理的意图,以便即使我的应用程序被杀死也可以触发。
代码起作用,并且挂起的意图将事件发送到广播接收器。但是,几个小时后,它停止工作。
我已读取未决意图,该意图存储在RAM中。操作系统是否会偶尔清除这些未决事件以节省内存?如果那样的话,如何确保我的待定意图始终有效?
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);
}
});
}
答案 0 :(得分:0)
这取决于您手机中安装的ROM。许多人拥有节电助手配置文件,这些配置文件会杀死您的应用程序以及它们留下的进程,例如您的意图。
该网站上有避免这种情况的指示:https://dontkillmyapp.com/