gpt4 book ai didi

android - 如何以节能的方式使用 Android 接收驾驶启动和停止 Activity ,甚至可以离线工作?

转载 作者:太空狗 更新时间:2023-10-29 13:07:38 24 4
gpt4 key购买 nike

我想收到有关驾驶开始和停止的 Android 位置回调。我已经尝试过 Google 的 Activity Detection API,但它似乎不够可靠。此外,我研究过使用 Neura 的 API 来检测位置事件,但它仅在设备在线时有效,这对我来说还不够。

最佳答案

您可以使用谷歌的 FenceApi 来声明驾驶围栏。

虽然这种方法看起来不错,但我遇到了这样一个事实,即这个 api 有时没有告诉我用户何时开始/结束驾驶,有时在我开始驾驶后过了很长时间 api 才告诉我事件。

一个。包括对您应用的 build.gradle 文件的依赖:

compile 'com.google.android.gms:play-services-location:+'

compile 'com.google.android.gms:play-services-contextmanager:+'

list 定义:

 <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >

<meta-data
android:name="com.google.android.awareness.API_KEY"
android:value="PUT_YOUR_AWARENESS_KEY_HERE" />

<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
PUT_YOUR_AWARENESS_KEY_HERE : You need to generate a key here.

您的 MainActivity 类 - 代码附带的解释:

public class MainActivity extends Activity {

private GoogleApiClient mGoogleApiClient;
private PendingIntent mPendingIntent;
private FenceReceiver mFenceReceiver;

// The intent action which will be fired when your fence is triggered.
private final String FENCE_RECEIVER_ACTION = BuildConfig.APPLICATION_ID + "FENCE_RECEIVER_ACTION";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(Awareness.API).build();
mGoogleApiClient.connect();
// Set up the PendingIntent that will be fired when the fence is triggered.
mPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(FENCE_RECEIVER_ACTION), 0);
// The broadcast receiver that will receive intents when a fence is triggered.
mFenceReceiver = new FenceReceiver();
registerReceiver(mFenceReceiver, new IntentFilter(FENCE_RECEIVER_ACTION));
createFence(DetectedActivityFence.IN_VEHICLE, "InVehicleFence");
}

@Override
public void onDestroy() {
try {
unregisterReceiver(mFenceReceiver); //Don't forget to unregister the receiver
} catch (Exception e) {
e.printStackTrace();
}
super.onDestroy();
}

private void createFence(int detectedActivityFence, final String fenceKey) {
AwarenessFence fence = DetectedActivityFence.during(detectedActivityFence);
// Register the fence to receive callbacks.
Awareness.FenceApi.updateFences(
mGoogleApiClient, new FenceUpdateRequest.Builder().addFence(fenceKey, fence, mPendingIntent)
.build()).setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
if (status.isSuccess()) {
Log.i(getClass().getSimpleName(), "Successfully registered.");
} else {
Log.e(getClass().getSimpleName(), "Could not be registered: " + status);
}
}
});
}

// Handle the callback on the Intent.
public class FenceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
FenceState fenceState = FenceState.extract(intent);
switch (fenceState.getCurrentState()) {
case FenceState.TRUE:
Log.i(fenceState.getFenceKey(), "Driving");
break;
case FenceState.FALSE:
Log.i(fenceState.getFenceKey(), "Not driving");
break;
}
}
}

关于android - 如何以节能的方式使用 Android 接收驾驶启动和停止 Activity ,甚至可以离线工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45739938/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com