I want to POST my Location on PushReceived when the screen is lock'd. My code works when the screen is on . WakeLock does not seem to work...
When receiving a PUSH notification for the first time from Parse.com the screen turns ON and I can see that the GPS icon pops up. And the database updates.
But the second time I get a PUSH notfication the onLocationCanged is not triggered, and the POST never gets sendt. Why is this ? However the screen turns on, and the GPS icon is shown.
Evrytime I set debug at this point it works:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
Some sort of asynch tastk problem maby ?
Here is my code:
public class PushNotificationReceiver extends ParsePushBroadcastReceiver implements LocationListener{
@Override
public void onPushReceive(Context context, Intent intent) {
//WakeLock
pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock((pm.SCREEN_BRIGHT_WAKE_LOCK | pm.FULL_WAKE_LOCK | pm.ACQUIRE_CAUSES_WAKEUP), "TAG");
wakeLock.acquire();
JSONObject pushData;
String alert = null;
String title = null;
try {
pushData = new JSONObject(intent.getStringExtra(PushNotificationReceiver.KEY_PUSH_DATA));
alert = pushData.getString("alert");
title = pushData.getString("title");
} catch (JSONException e) {}
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//Getting LOCATION
getLocation(context, locationManager);
Intent cIntent = new Intent(PushNotificationReceiver.ACTION_PUSH_OPEN);
cIntent.putExtras(intent.getExtras());
cIntent.setPackage(context.getPackageName());
PendingIntent pContentIntent =
PendingIntent.getBroadcast(context, 0 /*just for testing*/, cIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder
.setSmallIcon(R.drawable.car190)
.setContentTitle(alert)
.setContentText(title)
.setContentIntent(pContentIntent)
.setAutoCancel(true);
NotificationManager myNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
myNotificationManager.notify(1, builder.build());
}
private void getLocation(final Context context, final LocationManager locationManager){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
//Stop receiving LOCATION after 5 sec
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
locationManager.removeUpdates(PushNotificationReceiver.this);
//WakeLock release
wakeLock.release();
}
}, 5000);
}
@Override
public void onLocationChanged(Location location) {
currentLat = location.getLatitude() + "";
currentLng = location.getLongitude() + "";
currentDate = new Date();
String username = tabFragment1.usernameResponse;
try {
JSONObject object = new JSONObject(username);
myusername = object.getString("ID");
} catch (JSONException e) {
e.printStackTrace();
}
//POST to Database
postLocation(mainActivity.getAppContext(), currentLat, currentLng, currentDate.toString(), myusername);
}
}
Remove this:
//Stop receiving LOCATION after 5 sec
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
locationManager.removeUpdates(PushNotificationReceiver.this);
//WakeLock release
wakeLock.release();
}
}, 5000);
and add
locationManager.removeUpdates(PushNotificationReceiver.this);
wakeLock.release();
In onLocationChanged , like this:
@Override
public void onLocationChanged(Location location) {
currentLat = location.getLatitude() + "";
currentLng = location.getLongitude() + "";
currentDate = new Date();
username = tabFragment3.userEmail;
postLocation(mainActivity.getAppContext(), currentLat, currentLng, currentDate.toString(), username);
locationManager.removeUpdates(PushNotificationReceiver.this);
wakeLock.release();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.