0

我想POST位置PushReceived當屏幕被lock'd。 我的代碼在上的上有效。 激活鎖定似乎不工作...的Android - 喚醒鎖定 - 推送通知POST位置接收時屏幕lock'd(parse.com)

當從Parse.com接收PUSH通知首次屏幕將自動ON,我可以看到GPS圖標彈出。數據庫更新。

但是第二次我得到PUSH通知onLocationCanged沒有被觸發,並且POST永遠不會得到sendt。爲什麼是這樣 ?但屏幕打開,並顯示GPS圖標。

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); 

某種非同步tastk問題莫比的:

Evrytime我在這一點上它的工作原理設置調試?

這裏是我的代碼:

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); 
} 

} 

回答

0

刪除此:

 //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); 

,並添加

locationManager.removeUpdates(PushNotificationReceiver.this); 
wakeLock.release(); 

onLocationChanged,像這樣:

@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(); 
}