2016-07-30 55 views
1

我正在使用位置管理器来接收位置更新我的要求是,我必须听取连续的位置更新,但是我面临的问题是一旦它断开连接,我不知道如何到reatablish GPS连接,而且,在某些设备一次设备睡IM无法接收任何位置更新,请提供任何解决方案,以实现这一目标的任何帮助表示赞赏..请求的位置更新在设备休眠期间不起作用

public void setup(MainActivity activity) { 
     if (!setup) { 
      this.activity = activity; 
      Intent locationIntent = new Intent(this.activity, LocationIntentService.class); 
      mLocationPendingIntent = PendingIntent.getService(this.activity, 0, locationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
      Intent detectedIntent = new Intent(this.activity, ActivityDetectionIntentService.class); 
      mDetectedActivityPendingIntent = PendingIntent.getService(this.activity, 0, detectedIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
      googleApiClient = new GoogleApiClient.Builder(activity) 
        .addConnectionCallbacks(activity) 
        .addOnConnectionFailedListener(activity) 
        .addApi(ActivityRecognition.API) 
        .build(); 
      setup = true; 
     } 
    } 
**LocationIntentService.java** 
public class LocationIntentService extends IntentService { 


    public LocationIntentService() { 
     super("LocationServices"); 
    } 

    public LocationIntentService(String name) { 
     super("LocationServices"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     if (intent != null) { 
      Location location = intent.getParcelableExtra(LocationManager.KEY_LOCATION_CHANGED); 
      if (location != null) { 
       Intent localIntent = new Intent(HWUtil.LOCAL_RECEIVER); 
       localIntent.addCategory(Intent.CATEGORY_DEFAULT); 
       localIntent.putExtra(HWUtil.LOCATION, location); 
       localIntent.putExtra(HWUtil.FROM, HWUtil.LOCATION_SERVICE); 
       sendBroadcast(localIntent); 
      } 
     } 
    } 

} 

和IM发送该位置更新Broadcastrecciver

+0

我正面临圣母院问题 –

+0

您应该在我认为的后台服务上实施位置管理器。如果您将其绑定到某项活动上,则该功能无效,因为您在活动的停止位置 – HendraWD

回答

2

请注意,连续使用纯GPS作为位置省ider在移动设备上非常耗能。一旦是说,我会按照如下步骤进行你的任务:

  1. 我会用一个(背景)service将togheter与移动应用的合作。即,移动应用程序将开始执行此服务(检查startForeground()功能,以便您的服务几乎可以不中断地运行,并且包括可以链接到您的活动的statusBar通知)。
  2. 该服务(或任何内部类)将实现LocationListener接口,并且将是实际将作为采样位置的接口。一旦你得到一个位置,你将处理它(取决于你的需求,你可能想在另一个线程中处理它,因为创建的服务的默认线程与活动UI相同)。
  3. 一旦处理完毕,youw会提供对Android活动的响应,也就是说,您可以调用您的活动的公开方法,或者与处理程序等实现更复杂的通信策略。
  4. 关于连续位置采样,我强烈建议您使用AlarmManager服务,以便您可以安排下一个读数(您可以按照确切的重复时间间隔进行设置,请参阅official developer's documentation)。
  5. 当(且仅当)的位置更新的处理是重或耗时(例如,你必须把它传送给服务器),你可以获取并保持WakeLock用于避免设备落入睡眠模式;不要忘记在完成处理之后释放WakeLock,因为它是移动应用程序中能量汇的主要原因,所以要非常小心。

希望它可以帮助你

0

据我所知,wake lock是做最简单的方法。 PowerManager.WakeLock

唤醒锁定是一种机制,用于指示您的应用程序需要使设备保持开启状态。

任何使用WakeLock的应用程序都必须请求应用程序清单元素中的android.permission.WAKE_LOCK权限。致电newWakeLock(int, String)获取唤醒锁。

调用acquire()获取唤醒锁并强制设备保持在创建唤醒锁时请求的级别。

你应该AQUIRE唤醒锁:

//in onCreate of your service 
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
cpuWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, 
"gps_service"); 
cpuWakeLock.acquire(); 

// Release in onDestroy of your service 
if (cpuWakeLock.isHeld()) 
cpuWakeLock.release(); 
and add this to your manifest: 

https://developer.android.com/reference/android/Manifest.permission.html 以上为好,如果你需要不断位置更新。

+0

@d上停止位置更新。 datul1990它不会加热设备? –