Created
December 12, 2014 23:51
-
-
Save eneim/2022a4d2c21d73f042eb to your computer and use it in GitHub Desktop.
Sample Acitivty that uses Location API, Activity Recognition API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2013 The Android Open Source Project | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package im.ene.lab.hakkify; | |
import android.app.Activity; | |
import android.app.PendingIntent; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.location.Location; | |
import android.os.Bundle; | |
import android.os.Message; | |
import android.util.Log; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import com.google.android.gms.common.ConnectionResult; | |
import com.google.android.gms.common.api.GoogleApiClient; | |
import com.google.android.gms.location.ActivityRecognition; | |
import com.google.android.gms.location.LocationListener; | |
import com.google.android.gms.location.LocationRequest; | |
import com.google.android.gms.location.LocationServices; | |
import pl.droidsonroids.gif.GifDrawable; | |
import pl.droidsonroids.gif.GifImageView; | |
/** | |
* This the app's main Activity. It provides buttons for requesting the various features of the | |
* app, displays the current location, the current address, and the status of the location client | |
* and updating services. | |
* <p/> | |
* {@link #getLocation} gets the current location using the Location Services getLastLocation() | |
* function. {@link #getAddress} calls geocoding to get a street address for the current location. | |
* {@link #startUpdates} sends a request to Location Services to send periodic location updates to | |
* the Activity. | |
* {@link #stopUpdates} cancels previous periodic update requests. | |
* <p/> | |
* The update interval is hard-coded to be 5 seconds. | |
*/ | |
public class MainActivity extends Activity implements | |
GoogleApiClient.ConnectionCallbacks, | |
GoogleApiClient.OnConnectionFailedListener, | |
LocationListener, GifHandler.OnHandleMessageCallback, | |
ActivityRecognitionBroadcastReceiver.BroadcastListener { | |
private final String TAG = "MyAwesomeApp"; | |
private ActivityRecognitionBroadcastReceiver mReceiveFromIntentService; | |
private TextView mLocationView; | |
private GifImageView mAvatar; | |
private GoogleApiClient mGoogleApiClient; | |
private LocationRequest mLocationRequest; | |
private GifHandler mGifPlayer; | |
private GifDrawable mGifDrawable; | |
private PendingIntent mReceiveRecognitionIntent; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mAvatar = (GifImageView) findViewById(R.id.image_avatar); | |
mLocationView = (TextView) findViewById(R.id.text_location); | |
mGoogleApiClient = new GoogleApiClient.Builder(this) | |
.addApi(LocationServices.API) | |
.addApi(ActivityRecognition.API) | |
.addConnectionCallbacks(this) | |
.addOnConnectionFailedListener(this) | |
.build(); | |
mGifDrawable = (GifDrawable) mAvatar.getDrawable(); | |
mGifPlayer = new GifHandler(this); | |
mReceiveFromIntentService = new ActivityRecognitionBroadcastReceiver(this); | |
// IntentService から Broadcast される認識結果を受け取るための Receiver を登録しておく | |
registerReceiver(mReceiveFromIntentService, new IntentFilter("receive_recognition")); | |
} | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
// Connect the client. | |
mGoogleApiClient.connect(); | |
// playGif(10000); | |
} | |
@Override | |
protected void onStop() { | |
// Disconnecting the client invalidates it. | |
mGoogleApiClient.disconnect(); | |
super.onStop(); | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
try { | |
unregisterReceiver(mReceiveFromIntentService); | |
} catch (Exception er) { | |
er.printStackTrace(); | |
} | |
} | |
@Override | |
public void onConnected(Bundle bundle) { | |
mLocationRequest = LocationRequest.create(); | |
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); | |
mLocationRequest.setInterval(1000); // Update location every second | |
LocationServices.FusedLocationApi.requestLocationUpdates( | |
mGoogleApiClient, mLocationRequest, this); | |
mLocationView.setText("Service connected"); | |
Intent intent = new Intent( | |
MainActivity.this, ActivityRecognitionReceiverIntentService.class); | |
mReceiveRecognitionIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); | |
ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mGoogleApiClient, 2500, mReceiveRecognitionIntent); | |
} | |
@Override | |
public void onConnectionSuspended(int i) { | |
Log.i(TAG, "GoogleApiClient connection has been suspend"); | |
} | |
@Override | |
public void onConnectionFailed(ConnectionResult connectionResult) { | |
Log.i(TAG, "GoogleApiClient connection has failed"); | |
} | |
@Override | |
public void onLocationChanged(Location location) { | |
mLocationView.setText("Location received: " + location.toString()); | |
} | |
@Override | |
public void handleMessageImpl(Message msg) { | |
long delay = refreshGif(); | |
playGif(delay); | |
} | |
private void playGif(long delay) { | |
final Message message = mGifPlayer.obtainMessage(GifHandler.PLAY_GIF); | |
mGifPlayer.removeMessages(GifHandler.PLAY_GIF); | |
mGifPlayer.sendMessageDelayed(message, delay); | |
} | |
private long refreshGif() { | |
// mGifDrawable.reset(); | |
// Toast.makeText(this, mGifDrawable.getCurrentPosition() + "", Toast.LENGTH_SHORT).show(); | |
return 10000; | |
} | |
@Override | |
public void onBroadcastReceived(Context context, Intent intent) { | |
final int activityType = intent.getIntExtra("activity_type", 0); | |
Log.d("activityType", activityType + ""); | |
Toast.makeText(this, mReceiveFromIntentService.getNameFromType(activityType), Toast.LENGTH_SHORT).show(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment