簡體   English   中英

定位服務未發送響應

[英]Location Service doesn't send the response

我要檢查設備位置。 因此,我創建了一個名為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>

順便說一句,謝謝! 干杯!

為了使該問題有答案,並為發現此問題的任何人提供更多信息,請檢查此Stackoverflow問題

在這種情況下,解決方案是需要將位置發送到仿真器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM