Android Awareness API有recently announced新功能爲您的用例提供了一個簡單的解決方案。實現你想要做的事情的方法是創建並註冊一個相對於日出/日落指定的TimeFence。
例如:
// Create TimeFence
AwarenessFence sunriseFence =
TimeFence.aroundTimeInstant(TimeFence.TIME_INSTANT_SUNRISE,
0, 5 * ONE_MINUTE_MILLIS);
// Register fence with Awareness.
Awareness.FenceApi.updateFences(
mGoogleApiClient,
new FenceUpdateRequest.Builder()
.addFence("fenceKey", sunriseFence, myPendingIntent)
.build())
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
if (status.isSuccess()) {
Log.i(TAG, "Fence was successfully registered.");
} else {
Log.e(TAG, "Fence could not be registered: " + status);
}
}
});
你會得到回調時柵欄在日出的計算結果爲TRUE,而當它日出後5分鐘評估回FALSE。
請檢查Fence API code snippets文檔瞭解如何添加自定義的應用程序邏輯。
來源
2017-09-01 02:33:31
MRC
這是不可能的。請參閱http://stackoverflow.com/a/9621425/908621以獲取部分答案。 – fishinear