2013-06-20 33 views
0

我想检查设备的位置。所以,我制作了一个名为GeoService的服务,您将它传递给了一个意图,并向您发送响应。关键是答复永远不会回来,我不知道为什么。位置服务不发送回应

public class GeoService extends IntentService implements LocationListener{ 
    public static final int GET_DATA=1; 
    public static final int LOCATION_ACTUALIZED=2; 
    private static int WAITING_TIME=90000; 
    private static LocationManager locationManager; 
    private static Location place=null; 
    private static ResultReceiver recev=null; 

    public GeoService() { 
     super("GeoService"); 
    } 

    @Override 
    public void onCreate(){ 
     super.onCreate(); 
     locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this); 
    } 

     @Override 
     protected void onHandleIntent(Intent intent) { 
     int command=intent.getIntExtra("CMD", -1); 
     Bundle bun=new Bundle(); 

     switch(command){ 
     case GeoService.GET_DATA:{ 
      ResultReceiver receiver=intent.getParcelableExtra("receiver"); 
      getGeoData(receiver,bun); 
      break; 
     } 


     case GeoService.LOCATION_ACTUALIZED:{ 

    if(recev!=null){ 
     double longi=intent.getDoubleExtra("longitude", -1); 
     double lati=intent.getDoubleExtra("latitude", -1); 

long utcT=intent.getLongExtra("utc", -1); 
       bun.putDouble("longitude", longi); 
       bun.putDouble("latitude", lati); 
       bun.putLong("utc", utcT); 
       Log.d("geoService","sending response"); 
       recev.send(ConstantsIntents.STATUS_OK, bun); 
      } 

    } 




default:{ 


     ResultReceiver receiver=intent.getParcelableExtra("receiver"); 
      receiver.send(ConstantsIntents.INVALID_COMMAND, bun); 
     } 

    } 


    } 

    protected void getGeoData(ResultReceiver receiver, Bundle b){ 
     int elapsedTime=WAITING_TIME; 
     recev=receiver; 



    } 

    @Override 
    public void onLocationChanged(Location location) { 
     Log.d("geoService","onLocationChanged"); 
     Intent inte= new Intent(getBaseContext(), GeoService.class); 
     inte.putExtra("CMD", GeoService.LOCATION_ACTUALIZED); 
     inte.putExtra("longitude", location.getLongitude()); 
     inte.putExtra("latitude", location.getLatitude()); 
     inte.putExtra("utc", location.getTime());   
      place=location; 
    } 
      @Override 
      public void onProviderDisabled(String provider) { 
       // TODO Auto-generated method stub 
      } 



@Override 
        public void onProviderEnabled(String provider) { 
         locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 1, this); 
        } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
      // TODO Auto-generated method stub 

     } 

    } 

上面的代码有一些没有实现的方法,因为我不需要实现它们,但我需要它们(或它不会编译)。 这是在MainActivity当我拨打服务:

 public class MainActivity extends Activity { 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 

Intent intent=new Intent(Intent.ACTION_SYNC,null,this,GeoService.class); 
intent.putExtra("CMD", GeoService.GET_DATA); 
intent.putExtra("receiver",new ResultReceiver(new Handler()){ 
    @Override 
    protected void onReceiveResult(int resultCode, Bundle resultData) { 
     super.onReceiveResult(resultCode, resultData); 
     Log.d(TAG,"Response received"); 
     if (resultCode == ConstantsIntents.STATUS_OK) { 
     double latitud=resultData.getDouble("latitude"); 
     double longitud=resultData.getDouble("longitude"); 
     long utc=resultData.getLong("utc"); 
        } 
     } 
    }); 

     startService(intent); 

     } 

在这里,它的清单文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.hikokibest" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
    android:minSdkVersion="15" 
    android:targetSdkVersion="17" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
    <application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="hikoki.services.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <service android:name="hikoki.services.FlightStatusService"></service> 
    <service android:enabled="true" android:name="hikoki.services.NotificationsService"></service> 
    <service android:name="hikoki.services.ExperiencesService"></service> 
    <service android:name="hikoki.services.FlightDealService"></service> 
    <service android:name="hikoki.services.GeoService"></service> 
</application> 

顺便说一句,谢谢! 干杯!

+0

你看到“onLocationChanged”在你的logcat中吗? – paul

+0

@paul不,我没有看到logcat中的“onLocationChanged” – Mitodina

+0

您是否在启用了GPS的设备上测试这个?或模拟器? – paul

回答

0

因此,这个问题有一个答案,并提供更多的信息,任何人找到这个...检查此Stackoverflow question

这种情况下的解决方案需要发送一个位置到模拟器。

相关问题