mekCoder
mekCoder

Reputation: 15

Activity Recognition and Location Updates using Google play services

I am trying to receive the locations based on activity of the user,i.e., if the user is still the location updates are less frequent and if he is walking or driving the updates are quicker. I have written a service to receive the location updates and placed a broadcast receiver in the onCreate() method which receives the intents broadcasted from main activity. These broadcasted intents carry the string that tell my broadcast receiver the "activity" of the user. But this receiver never receives the intents, so i am unable to set locationRequest timings, based on which I will pass the appropriate locationRequest to the services.

Can any body tell and help why is the broadcast reciever in the onCreate of services might not be getting called. Thanks.

public class MyActivityRecognition extends Activity implements
        GooglePlayServicesClient.ConnectionCallbacks,
        GooglePlayServicesClient.OnConnectionFailedListener {

    private ActivityRecognitionClient arclient;
    private PendingIntent pIntent;
    private BroadcastReceiver receiver;
    private TextView tvActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvActivity = (TextView) findViewById(R.id.tvActivity);

        int resp = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (resp == ConnectionResult.SUCCESS) {
            arclient = new ActivityRecognitionClient(this, this, this);
            arclient.connect();

        } else {
            Toast.makeText(this, "Please install Google Play Service.",
                    Toast.LENGTH_SHORT).show();
        }

        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String u = intent.getStringExtra("Activity");
                String v = "Activity :" + intent.getStringExtra("Activity")
                        + " " + "Confidence : "
                        + intent.getExtras().getInt("Confidence") + "\n";
                tvActivity.setText(v);


                Intent activityIntent = new Intent();
                intent.setAction("com.example.useractivity");
                intent.putExtra("ACTIVITY", u);
                sendBroadcast(activityIntent);
            }
        };

The service class is as follows :

public class LocationService extends Service implements
        GooglePlayServicesClient.ConnectionCallbacks,
        GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {

    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;

    private static final int MILLISECONDS_PER_SECOND = 1000;

    private static final int FASTEST_INTERVAL_IN_SECONDS = 5;

    private static final long FASTEST_INTERVAL = MILLISECONDS_PER_SECOND
            * FASTEST_INTERVAL_IN_SECONDS;


    LocationRequest mLocationRequest;
    LocationClient mLocationClient;
    boolean mUpdatesRequested;
    String mActivity="Still";
    private BroadcastReceiver myReceiver;

    @Override
    public void onCreate() {
        super.onCreate();

        myReceiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {

                String action = intent.getAction();
                if(action.equals("com.example.useractivity")){
                    mActivity = intent.getExtras().getString("ACTIVITY");

                }

            }
        };

        IntentFilter filter = new IntentFilter();
        filter.addAction("com.example.useractivity");
        registerReceiver(myReceiver, filter);

        mLocationRequest = LocationRequest.create();
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        if (mActivity.equalsIgnoreCase("Still")
                || mActivity.equalsIgnoreCase("Tilting")
                || mActivity.equalsIgnoreCase("Unknown")) {
            mLocationRequest.setInterval(30*1000);

        } else if (mActivity.equalsIgnoreCase("On Foot")
                || mActivity.equalsIgnoreCase("On Bicycle")) {
            mLocationRequest.setInterval(20*1000);

        } else if (mActivity.equalsIgnoreCase("In Vehicle")) {
            mLocationRequest.setInterval(10*1000);

        }
        mLocationRequest.setFastestInterval(FASTEST_INTERVAL);

        mLocationClient = new LocationClient(getApplicationContext(), this, this);
        mUpdatesRequested = true;

        mLocationClient.connect();
    }

    @Override
    public void onLocationChanged(Location location) {

        String latitude = Double.toString(location.getLatitude());
        String longitude = Double.toString(location.getLongitude());

        String msg = "Updated Location: " + latitude + "," + longitude;
        Toast.makeText(getApplicationContext(), msg,
                Toast.LENGTH_SHORT).show();
        System.out.println(Double.toString(location.getLatitude()) + ","
                        + Double.toString(location.getLongitude()));

        SaveData sd = new SaveData(getApplicationContext());
        sd.save(mActivity, latitude, longitude);
    }

    @Override
    public void onConnected(Bundle arg0) {
        if (mUpdatesRequested) {

            mLocationClient.requestLocationUpdates(mLocationRequest, this);

        }
    }

Upvotes: 1

Views: 1622

Answers (1)

code_incomplete
code_incomplete

Reputation: 36

I know I'm very late this question but I thought I'd answer anyway.

In MyActivityRecognition you seem to be using the 'intent' variable passed to onReceive where I think you want to be setting the action and extra on activityIntent. See below.

Intent activityIntent = new Intent();
intent.setAction("com.example.useractivity");
intent.putExtra("ACTIVITY", u);
sendBroadcast(activityIntent);

Perhaps you mean this:

Intent activityIntent = new Intent();
activityIntent.setAction("com.example.useractivity");
activityIntent.putExtra("ACTIVITY", u);
sendBroadcast(activityIntent);

By not using activityIntent your service will not receive the extra and action and thus not do what you want it to do.

I hope this helps, Thanks :) (It's my first answer attempt, please mark it correct if it is.)

Upvotes: 2

Related Questions