Reputation: 177
I am trying to start a service using pendingintent and this is how i am trying
btnSave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
InfoLayout.setVisibility(LinearLayout.VISIBLE);
mIntentService = new Intent(MainActivity.this, LocationService.class);
mPendingIntent = PendingIntent.getService(
MainActivity.this, 1, mIntentService, 0);
locationrequest = LocationRequest.create();
locationrequest.setInterval(10000 * 6);
locationClient.requestLocationUpdates(locationrequest,
mPendingIntent);
Toast.makeText(MainActivity.this, "Starting service",
Toast.LENGTH_SHORT).show();
}
});
I tried this code on 2.3.5 and it works fine, but when i tried this code on 4.4.2 my service is not at all starting. I am not understanding why this is happening. can some one please explain what is the wrong I am doing.
Thanks in advance:)
EDIT
public void onStart(Intent intent, int startId) {
// Toast.makeText(this, "in service", Toast.LENGTH_LONG).show();
Location location = intent
.getParcelableExtra(LocationClient.KEY_LOCATION_CHANGED);
db = new PhoneDatasource(this);
db.open();
contactlist = db.getAllContacts();
db.close();
if (location != null) {
add = null;
Constants.latitude = location.getLatitude();
Constants.longitude = location.getLongitude();
}
Upvotes: 4
Views: 965
Reputation: 67019
The answer might be on this page ("Location settings changes on Android 4.4"): https://support.google.com/nexus/answer/3467281
That is, in Android 4.4, you have to go to the new Location settings and explicitly turn it on (and configure the mode), or else no apps will be allowed to get any location information. Also, you need to be aware of the different location modes and how they will affect the ability of apps to get location information. Reading the linked page is crucial in 4.4.
Upvotes: 2