Example usage for android.content Context LOCATION_SERVICE

List of usage examples for android.content Context LOCATION_SERVICE

Introduction

In this page you can find the example usage for android.content Context LOCATION_SERVICE.

Prototype

String LOCATION_SERVICE

To view the source code for android.content Context LOCATION_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.location.LocationManager for controlling location updates.

Usage

From source file:com.google.android.apps.paco.ExperimentExecutorCustomRendering.java

public void onStatusChanged(String provider, int status, Bundle extras) {
    unregisterLocationListener();/* www  . j  a  va2 s  .c o m*/
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    getBestProvider(lm);
}

From source file:activities.GatewayActivity.java

@Override
public void locationMode(boolean on) {
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    boolean networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    if (on) {/*from   w w  w .  j a va2 s .c o m*/
        if (!gpsEnabled || !networkEnabled) {
            new LocationDialog().show(getSupportFragmentManager(), "locationdialog");
            configFragment.updateLocationView(false);
            sApp.isLocationOn = false;
        } else {
            if (!sApp.locationService.isRunning())
                sApp.locationService.start();
            sApp.isLocationOn = true;
            showToast(getString(R.string.location_enabled));
        }
    } else {
        if (sApp.locationService.isRunning())
            sApp.locationService.stop();
        showToast(getString(R.string.toast_stopped_location_service));
        sApp.isLocationOn = false;
    }
    configFragment.updateLocationView(sApp.isLocationOn);
}

From source file:edu.missouri.bas.service.SensorService.java

@SuppressWarnings("deprecation")
@Override//from w  w w.j a va  2 s  .  com
public void onCreate() {

    super.onCreate();
    Log.d(TAG, "Starting sensor service");
    mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
    soundsMap = new HashMap<Integer, Integer>();
    soundsMap.put(SOUND1, mSoundPool.load(this, R.raw.bodysensor_alarm, 1));
    soundsMap.put(SOUND2, mSoundPool.load(this, R.raw.voice_notification, 1));

    serviceContext = this;

    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    bluetoothMacAddress = mBluetoothAdapter.getAddress();
    mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    //Get location manager
    mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    activityRecognition = new ActivityRecognitionScan(getApplicationContext());
    activityRecognition.startActivityRecognitionScan();

    mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);

    serviceWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SensorServiceLock");
    serviceWakeLock.acquire();

    //Initialize start time
    stime = System.currentTimeMillis();

    //Setup calendar object
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(stime);

    /*
     * Setup notification manager
     */

    notification = new Notification(R.drawable.icon2, "Recorded", System.currentTimeMillis());
    notification.defaults = 0;
    notification.flags |= Notification.FLAG_ONGOING_EVENT;
    notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    Intent notifyIntent = new Intent(Intent.ACTION_MAIN);
    notifyIntent.setClass(this, MainActivity.class);

    /*
     * Display notification that service has started
     */
    notification.tickerText = "Sensor Service Running";
    PendingIntent contentIntent = PendingIntent.getActivity(SensorService.this, 0, notifyIntent,
            Notification.FLAG_ONGOING_EVENT);
    notification.setLatestEventInfo(SensorService.this, getString(R.string.app_name),
            "Recording service started at: " + cal.getTime().toString(), contentIntent);

    notificationManager.notify(SensorService.SERVICE_NOTIFICATION_ID, notification);

    // locationControl = new LocationControl(this, mLocationManager, 1000 * 60, 200, 5000);   

    IntentFilter activityResultFilter = new IntentFilter(XMLSurveyActivity.INTENT_ACTION_SURVEY_RESULTS);
    SensorService.this.registerReceiver(alarmReceiver, activityResultFilter);

    IntentFilter sensorDataFilter = new IntentFilter(SensorService.ACTION_SENSOR_DATA);
    SensorService.this.registerReceiver(alarmReceiver, sensorDataFilter);
    Log.d(TAG, "Sensor service created.");

    try {
        prepareIO();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    prepareAlarms();

    Intent startSensors = new Intent(SensorService.ACTION_START_SENSORS);
    this.sendBroadcast(startSensors);

    Intent scheduleCheckConnection = new Intent(SensorService.ACTION_SCHEDULE_CHECK);
    scheduleCheck = PendingIntent.getBroadcast(serviceContext, 0, scheduleCheckConnection, 0);
    mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + 1000 * 60 * 5, 1000 * 60 * 5, scheduleCheck);
    mLocationClient = new LocationClient(this, this, this);
}

From source file:com.google.android.apps.paco.ExperimentExecutorCustomRendering.java

private void unregisterLocationListener() {
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (lm != null) {
        lm.removeUpdates(this);
    }//from ww  w. j a  va  2  s. c  om
}

From source file:net.evecom.androidecssp.activity.event.EventInfoActivity.java

/**
 * /*from w w  w  .java 2s . co m*/
 *  
 * 
 * @author Mars zhang
 * @created 2015-12-1 11:45:22
 */
private void requestLocationUpdate() {
    //  
    mMyLocation = new MyOverlay(EventInfoActivity.this, null);
    // 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        //            toast("", 0);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, mMyLocation);
    } else if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 1, mMyLocation);
        //            toast("", 0);
    } else {
        //            toast("", 0);
    }
}

From source file:com.luke.lukef.lukeapp.fragments.MapViewFragment.java

/**
 * Sets up the Google Map Fragment/*from   w w  w  .  j  a v a2 s . c  o  m*/
 */
private void setupGoogleMap() {
    LocationManager lm = (LocationManager) getMainActivity().getSystemService(Context.LOCATION_SERVICE);
    //check for permissions and get last known location of the device
    if (ActivityCompat.checkSelfPermission(getMainActivity(),
            Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(getMainActivity(),
                    Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        getMainActivity().checkPermissions();
        this.lastKnownLoc = lm.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
    } else {
        this.lastKnownLoc = lm.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
    }
}

From source file:edu.drake.research.android.lipswithmaps.activity.MapsActivity.java

/**
 * Gets the current location//from  ww w .ja v  a  2  s.  c  om
 */
private void getCurrentLongLat() {
    mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    //Check permission
    // Register the listener with the Location Manager to receive location updates
    if (ContextCompat.checkSelfPermission(this, locationPermission) == PackageManager.PERMISSION_GRANTED) {
        mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, mLocationListener);
        wifiScan();
    } else {
        askPermission();
    }

}

From source file:carsharing.starter.automotive.iot.ibm.com.mobilestarterapp.Home.CarBrowse.java

private void getAccurateLocation2(final GoogleMap googleMap) {
    final FragmentActivity activity = getActivity();
    if (activity == null) {
        return;//from w  ww  .j av a 2 s.  c o  m
    }
    final ConnectivityManager connectivityManager = (ConnectivityManager) activity
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
            && (networkInfo != null && networkInfo.isConnected())) {
        if (simulatedLocation == null) {
            if (ActivityCompat.checkSelfPermission(activity.getApplicationContext(),
                    Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    && ActivityCompat.checkSelfPermission(activity.getApplicationContext(),
                            Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }

            locationManager = (LocationManager) activity.getApplicationContext()
                    .getSystemService(Context.LOCATION_SERVICE);

            final List<String> providers = locationManager.getProviders(true);
            Location finalLocation = null;

            for (String provider : providers) {
                if (ActivityCompat.checkSelfPermission(activity.getApplicationContext(),
                        Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                        && ActivityCompat.checkSelfPermission(activity.getApplicationContext(),
                                Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }

                final Location suggestedLocation = locationManager.getLastKnownLocation(provider);

                if (suggestedLocation == null) {
                    continue;
                }
                Log.i("Location Provider",
                        "provider=" + suggestedLocation.getProvider() + " time="
                                + new SimpleDateFormat("yyyy MMM dd HH:mm:ss")
                                        .format(suggestedLocation.getTime())
                                + " accuracy=" + suggestedLocation.getAccuracy() + " lat="
                                + suggestedLocation.getLatitude() + " lng=" + suggestedLocation.getLongitude());

                if (finalLocation == null) {
                    finalLocation = suggestedLocation;
                } else if (suggestedLocation.getTime() - finalLocation.getTime() > 1000) {
                    // drop more than 1000ms old data
                    finalLocation = suggestedLocation;
                } else if (suggestedLocation.getAccuracy() < finalLocation.getAccuracy()) {
                    // picks more acculate one
                    finalLocation = suggestedLocation;
                }
            }

            location = finalLocation;
        } else {
            location = simulatedLocation;
        }
        if (location != null) {
            final LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());

            mMap.addMarker(new MarkerOptions().position(userLocation).title("Your Location"));

            mMap.moveCamera(CameraUpdateFactory.newLatLng(userLocation));
            mMap.animateCamera(CameraUpdateFactory.zoomTo(14), 2000, null);

            getCars(location);
        } else {
            Log.e("Location Data", "Not Working!");

            //                Toast.makeText(getActivity().getApplicationContext(), "Please activate your location settings and restart the application!", Toast.LENGTH_LONG).show();
            getAccurateLocation(mMap);
        }
    } else {
        if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Toast.makeText(activity.getApplicationContext(), "Please turn on your GPS", Toast.LENGTH_LONG)
                    .show();

            final Intent gpsIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivityForResult(gpsIntent, GPS_INTENT);

            if (networkInfo == null) {
                networkIntentNeeded = true;
            }
        } else {
            if (networkInfo == null) {
                Toast.makeText(activity.getApplicationContext(), "Please turn on Mobile Data or WIFI",
                        Toast.LENGTH_LONG).show();

                final Intent settingsIntent = new Intent(Settings.ACTION_SETTINGS);
                startActivityForResult(settingsIntent, SETTINGS_INTENT);
            }
        }
    }
}

From source file:ca.ualberta.cs.cmput301w15t04team04project.MainActivity.java

/**
 * Will be called when user clicked add Location button, it will pop a
 * dialog and let user choose the method of location which go to the
 * osmMainAcitivity//from w  w  w . j  a v a 2  s  . c o  m
 * 
 * @param v
 *            View passed to the activity to check which button was pressed.
 */
public void goToMapAction(View v) {
    AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
    if (user.getHomelocation() != null) {
        adb.setMessage("Current Home Location is " + user.getHomelocation().getLatitude()
                + user.getHomelocation().getLongitude() + "\nChoose the HomeLocation Way");

    } else {
        adb.setMessage("Choose the HomeLocation Way");
    }
    adb.setNegativeButton("GPS", new OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            homeLocation = location;
            user.setHomelocation(location);
        }
    });

    adb.setPositiveButton("Map", new OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent(MainActivity.this, osmMainActivity.class);
            startActivity(intent);
        }
    });

    adb.setCancelable(true);
    adb.show();

}

From source file:com.example.devesh.Coride.DriverRegistration.java

@TargetApi(Build.VERSION_CODES.M)
public void sendData(View view) throws MalformedURLException {
    Log.e("TEST LOG", "Entered the loop");
    Toast.makeText(this, "coride", Toast.LENGTH_SHORT).show();
    Master master = new Master();
    String url_string = master.url + "driverdata";
    EditText n_edittext;/*from  w  ww .j a  va 2 s  .  c  om*/
    EditText mob_edittext;
    EditText confrm_edittext;
    EditText age_edittext;
    EditText lnumber;
    EditText pnumber;
    EditText vnumber;
    EditText mnumber;
    EditText capacity;
    String name;
    String mobile;
    String conpassword;
    String password;
    String address;
    String email;
    String slnumber;
    String spnumber;
    String svnumber;
    String smnumber;
    String scapacity;
    int count = 0;
    n_edittext = (EditText) findViewById(R.id.name);
    mob_edittext = (EditText) findViewById(R.id.mobileno);
    confrm_edittext = (EditText) findViewById(R.id.confirmpassword);
    mPasswordView = (EditText) findViewById(R.id.password);
    mEmailView = (AutoCompleteTextView) findViewById(R.id.email);
    age_edittext = (EditText) findViewById(R.id.address);
    lnumber = (EditText) findViewById(R.id.lnumber);
    pnumber = (EditText) findViewById(R.id.pnumber);
    vnumber = (EditText) findViewById(R.id.vnumber);
    mnumber = (EditText) findViewById(R.id.mnumber);
    capacity = (EditText) findViewById(R.id.capacity);
    name = n_edittext.getText().toString();
    mobile = mob_edittext.getText().toString();
    master.mobile1 = mobile;
    password = mPasswordView.getText().toString();
    conpassword = confrm_edittext.getText().toString();
    address = age_edittext.getText().toString();
    email = mEmailView.getText().toString();
    slnumber = lnumber.getText().toString();
    spnumber = pnumber.getText().toString();
    svnumber = vnumber.getText().toString();
    smnumber = mnumber.getText().toString();
    ;
    scapacity = capacity.getText().toString();

    regId = registerGCM();
    System.out.println(regId);
    //        String reg=regId.toString();
    count = validate(name, email, mobile, password, conpassword, address);
    if (count == 0) {
        count = email_validation(mEmailView, getBaseContext());
    }
    if (count == 0) {
        count = password_matcher(password, conpassword);
    }

    if (count == 0) {

        String json = "[{name:\"" + name + "\"},{mobile:\"" + mobile + "\"},{password:\"" + password
                + "\"},{email:\"" + email + "\"},{address:\"" + address + "\"},{license:\"" + slnumber
                + "\"},{pan:\"" + spnumber + "\"},{vehicle:\"" + svnumber + "\"},{model:\"" + smnumber
                + "\"},{capacity:\"" + scapacity + "\"},{regid:\"" + regId + "\"}]";
        // snd.addNameValuePair("name", name);
        //snd.addNameValuePair("mobile", mobile);
        //snd.addNameValuePair("password", password);
        // snd.addNameValuePair("email",email);
        //snd.addNameValuePair("age", age);
        SendDataTask snd = new SendDataTask(json); //Go to sendData class fromhere
        URL url = new URL(url_string);
        snd.execute(url);

    }

    System.out.println("i am hear");
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (locationManager.isProviderEnabled(locationManager.NETWORK_PROVIDER)
            || locationManager.isProviderEnabled(locationManager.GPS_PROVIDER)) {
        Log.e("Coride", "Provider enable");
        callLocationService();
    } else {
        Log.e("Coride", "provider disabled");
        showSettingsAlert("NETWORK_PROVIDER");

    }

    LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {

        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

            callLocationService();
        }

        @Override
        public void onProviderDisabled(String s) {
            showSettingsAlert("NETWORK_PROVIDER");
        }
    };
    //   dialog.cancel();

    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, 123);
        return;

    }
    locationManager.requestLocationUpdates(locationManager.NETWORK_PROVIDER, 0, 0, locationListener);

}