2013-03-14 27 views
3

我实现了一个机器人的服务,开机启动: MyService.java的Android服务LocationListener的未激活

package com.example.testservicelogging; 

import com.example.testservicelogging.MyBroadcastReceiver; 

import android.app.PendingIntent; 
import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.widget.Toast; 


public class MyService extends Service implements LocationListener { 

    LocationManager lm; 
    PendingIntent pendingIntent; 
    LocationListener locationListener; 

    public MyService() { 
    } 

    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
     public int onStartCommand(Intent intent, int flags, int startId) { 
     //TODO do something useful 
     //Toast.makeText(getBaseContext(), "Got in!!!", Toast.LENGTH_SHORT).show(); 
     System.out.println("Got in"); 

     Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();   

      //---use the LocationManager class to obtain locations data--- 
      lm = (LocationManager) 
        getSystemService(Context.LOCATION_SERVICE); 


      Intent i = new Intent(this, MyBroadcastReceiver.class); 
      pendingIntent = PendingIntent.getBroadcast(
        this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT); 

      //---request for location updates using GPS--- 
      lm.requestLocationUpdates(
        LocationManager.GPS_PROVIDER, 
        6000, 
        5, 
        pendingIntent);  

     return START_STICKY; 
     } 


    @Override 
    public void onDestroy() { 
     //---remove the pending intent--- 
     lm.removeUpdates(pendingIntent); 

     super.onDestroy(); 
     Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();   
    } 

    @Override 
    public void onLocationChanged(Location arg0) { 
     // TODO Auto-generated method stub 
     Toast.makeText(this, "onLocationChanged", Toast.LENGTH_LONG).show(); 

    } 

    @Override 
    public void onProviderDisabled(String arg0) { 
     // TODO Auto-generated method stub 
     Toast.makeText(this, "onProviderDisabled", Toast.LENGTH_LONG).show(); 

    } 

    @Override 
    public void onProviderEnabled(String arg0) { 
     // TODO Auto-generated method stub 
     Toast.makeText(this, "onProviderEnabled", Toast.LENGTH_LONG).show(); 

    } 

    @Override 
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
     // TODO Auto-generated method stub 
     Toast.makeText(this, "onStatusChanged", Toast.LENGTH_LONG).show(); 

    } 


} 

BroadcastReceiver类:

package com.example.testservicelogging; 

import java.io.IOException; 

import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationManager; 
import android.util.Log; 
import android.widget.Toast; 


public class MyBroadcastReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 


     Intent t=new Intent(context, MyService.class); 
     t.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startService(t); 

     String locationKey = LocationManager.KEY_LOCATION_CHANGED; 
     String providerEnabledKey = LocationManager.KEY_PROVIDER_ENABLED; 

     //Toast.makeText(context, "INSIDE!!!", Toast.LENGTH_SHORT).show(); 
     System.out.println("INSIDE"); 

     if (intent.hasExtra(providerEnabledKey)) { 
      if (!intent.getBooleanExtra(providerEnabledKey, true)) { 
       Toast.makeText(context, 
         "Provider disabled", 
         Toast.LENGTH_SHORT).show();    
      } else { 
       Toast.makeText(context, 
         "Provider enabled", 
         Toast.LENGTH_SHORT).show(); 
      } 
     } 

     if (intent.hasExtra(locationKey)) { 
      Location loc = (Location)intent.getExtras().get(locationKey); 
      Toast.makeText(context, 
        "Location changed : Lat: " + loc.getLatitude() + 
        " Lng: " + loc.getLongitude(), 
        Toast.LENGTH_SHORT).show(); 



      //do something with the coordinates returned 


     } 

    } 


} 

我面临的一些问题:

  1. (也是最重要的):引导时的服务负载,但即使我看到t他GPS图标闪烁,它永远不会返回坐标!我做错了什么?
  2. 我想在预定义的时间间隔内获取坐标(例如,每天从08:00至18:00每15分钟一次)。我怎样才能实现呢?
  3. 即使我提高了100000毫秒和500的距离来激活requestLocationUpdates,它有时会每隔2-5秒运行一次,即使我保持静止!我怎样才能克服?

非常感谢您的帮助!

回答

1

您正在请求位置更新并提供PendingIntent,该位置更新发生时应该广播。不过,你的服务也是implements LocationListener,所以看起来你可能有点困惑。我不确定你在这里试图做什么,但开始我只是让你的服务直接得到回调。在这种情况下,你不需要BroadcastReceiver,你应该改变这样的:

//---request for location updates using GPS--- 
lm.requestLocationUpdates(
    LocationManager.GPS_PROVIDER, 6000, 5, pendingIntent); 

这样:

//---request for location updates using GPS--- 
lm.requestLocationUpdates(
    LocationManager.GPS_PROVIDER, 6000, 5, this); 

这将导致定位服务以定期调用回你的方法”已经在您的服务类中实现(onLocationChanged(),onProviderEnabled()等)

另外,您为minTime和minDistance提供的参数非常小。 minTime 6000为6秒,minDistance为5米。这会导致GPS不断耗尽电量,因此您可能需要重新考虑这些参数。

我建议你试试看,并添加一些日志记录,看看会发生什么。我会建议使用日志框架(即:Log.v()Log.e()等),并看看logcat,而不是尝试使用吐司。敬酒调试相当不可靠。

此外,所有设备上的位置服务实施都不同。每个制造商都提供自己的实施方案,并且在可靠性,稳健性和操作特性方面差异很大。无法在特定时间间隔或特定时间可靠地坚持GPS更新。你所能做的就是向操作系统提供合适的提示关于你想要的东西。无论您指定的参数如何,位置框架都会在您想要时回拨您。你只需要相应地理解和编码。

如果您想每15分钟获取一次更新,您可以指定一个15分钟的minTime(值为15 * 60 * 1000),或者您可以每隔15分钟与警报管理器安排一次唤醒警报,当警报熄灭时,您可以立即请求位置更新(尽管您可能需要一些时间才能获取它们)。

希望我已经为您提供了足够的材料,以便您可以在此取得一些进展。如果您需要更多帮助,请随时添加评论。

1

确保任一一个被添加到清单的基础上,你需要什么:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>