आप बाड़ घोषित करने के लिए Google के FenceApi का उपयोग कर सकते हैं। उदाहरण के लिए, यह एक ड्राइविंग बाड़ का पता लगाने के लिए एक कोड है।
हालांकि यह दृष्टिकोण अच्छा लगता है, लेकिन मुझे इस तथ्य का सामना करना पड़ा कि घटनाओं के दौरान कभी-कभी मुझे यह नहीं बताया गया था, और कभी-कभी मुझे चलने/चलाने के बाद काफी समय लगा जब एपी ने मुझे बताया वह घटना
ए। अपने ऐप की build.gradle फ़ाइल पर निर्भरता शामिल करें:
compile 'com.google.android.gms:play-services-location:+'
compile 'com.google.android.gms:play-services-contextmanager:+'
बी। प्रकट परिभाषाएँ:
<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: आप एक महत्वपूर्ण 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(), "Active");
break;
case FenceState.FALSE:
Log.i(fenceState.getFenceKey(), "Not Active");
break;
}
}
}
}
यह नमूना है केवल पता लगाने के राज्य ड्राइविंग, लेकिन, आप 'createFence' जैसे अन्य गतिविधि के तरीकों के साथ कॉल कर सकते हैं के लिए:
createFence(DetectedActivityFence.TILTING, "TiltingFence");
createFence(DetectedActivityFence.WALKING, "WalkingFence");
createFence(DetectedActivityFence.ON_FOOT, "OnFootFence");
createFence(DetectedActivityFence.RUNNING, "RunningFence");
यह अच्छा रहेगा. वहाँ किसी भी न्यूनतम API अपेक्षित स्तर, या सिर्फ खेलने सेवाओं lib काम करेंगे है? –
@MayurMore इसकी एंड्रॉयड 2.2 के बाद से समर्थित ... –
दोनों लिंक अब –