1

我正在尝试自动更新我的位置,并在位置更改时让云相关代码的其他部分工作。这是我的服务代码:

    package com.salesforce.samples.templateapp;

import java.util.HashMap;

import org.json.JSONArray;

import com.salesforce.androidsdk.app.ForceApp;
import com.salesforce.androidsdk.rest.RestClient;
import com.salesforce.androidsdk.rest.RestRequest;
import com.salesforce.androidsdk.rest.RestResponse;
import com.salesforce.androidsdk.rest.RestClient.AsyncRequestCallback;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Criteria;
import android.location.GpsStatus.Listener;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class MyServices extends Service {
    RestClient client;
    double plat;
    double plong;
    int Two_Min=2*60*1000;

    // TextView infoText;

    public int onStartCommand(Intent intent, int flags, int startId) {

        Toast.makeText(this, "Location Updation has started", Toast.LENGTH_LONG)
                .show();
        Log.v("X","Response:in onStartCommand()");
        //Intent in = new Intent().setClass(MyServices.this, GPSUpdate.class);
            //in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //startActivity(in);
        detectLocation();
                return START_STICKY;


    }

    private void detectLocation() {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Inside detectlocation()", Toast.LENGTH_SHORT)
                .show();
        LocationManager lm1 = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll1 = new MyLocationListetner();

        Criteria crit = new Criteria();
        crit.setAccuracy(Criteria.ACCURACY_FINE);
        String best = lm1.getBestProvider(crit, false);
       // lm1.requestLocationUpdates(best, 0, 1, ll1);
        Log.v("X",
                "Response:After creating lm and ll ");

    //  boolean b1=lm1.addGpsStatusListener((Listener) ll1);
        //Log.v("X",
            //  "Response: "+b1);

        lm1.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll1);
        Log.v("X",
                "Response:After lm1.requestLocationUpdates ");


        /*Location loc = null;
        loc.getLatitude();
        loc.getLongitude();
        Log.v("X","Response:"+loc);
        ll1.onLocationChanged(loc);*/
    }

    class MyLocationListetner implements LocationListener {


        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            Log.d("X","Response:inside onLocationChanged ");
            Toast.makeText(getApplicationContext(), "inside onLocationChanged()", Toast.LENGTH_LONG).show();
            //Log.v("X","Response:"+location);
            if (location != null) {
                plat = location.getLatitude();
                plong = location.getLongitude();
                Log.d("X",
                        "Response:Location " + Double.toString(plat)+Double.toString(plong));

                String objectType = "akshayg__User__c";
                String objectId = "a02900000089fK3";
                HashMap<String, Object> fields = new HashMap<String, Object>();
                fields.put("Name", "Ashish");
                // fields.put("akshayg__Donor_Location__Latitude__s",
                // Double.toString(plat));
                // fields.put("akshayg__Donor_Location__Longitude__s",
                // Double.toString(plong));
                RestRequest request = null;

                try {
                    request = RestRequest.getRequestForUpdate(
                            getString(R.string.api_version), objectType,
                            objectId, fields);
                    // Toast.makeText(this, "Location Updation has started",
                    // Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    // printHeader("Could not build update request");
                    printException(e);
                    return;
                }

                client.sendAsync(request, new AsyncRequestCallback() {

                    @Override
                    public void onSuccess(RestRequest request,
                            RestResponse result) {

                        // Toast.makeText(this,
                        // ""+Double.toString(plat)+","+Double.toString(plong),
                        // Toast.LENGTH_LONG).show();

                        try {
                             //Toast.makeText(this, "Location Updated",Toast.LENGTH_LONG).show();

                            Log.v("X",
                                    "Response:inside onSuccess() " + result.toString());
                            /*JSONArray records = result.asJSONObject()
                                    .getJSONArray("records");
                            for (int i = 0; i < records.length(); i++) {
                                // listAdapter.add(records.getJSONObject(i).getString("Name"));
                                // listAdapter.add(records.getJSONObject(i).getString("akshayg__Phone_Number__c"));
                                // listAdapter.add(records.getJSONObject(i).getString("akshayg__Donor_Location__Latitude__s"));
                                // listAdapter.add(records.getJSONObject(i).getString("akshayg__Donor_Location__Longitude__s"));
                            }
                        */ 
                        }
                        catch (Exception e) {
                            onError(e);
                        }
                    }

                    @Override
                    public void onError(Exception exception) {
                        Log.v("X",
                                "Response: " + exception.toString());

                        // Toast.makeText(MainActivity.this,
                        // MainActivity.this.getString(ForceApp.APP.getSalesforceR().stringGenericError(),
                        // exception.toString()),
                        // Toast.LENGTH_LONG).show();
                    }

                });
            }

        }

        private void printException(Exception e) {
            String err = "Error: " + e.getClass().getSimpleName();
            Toast.makeText(getApplicationContext(), err, Toast.LENGTH_LONG)
                    .show();
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub


        }

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

        }

    }

    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Location Updation has stoped", Toast.LENGTH_LONG)
                .show();
    }

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

弹出“内部检测位置”之前的祝酒词并记录条目直到

04-04 00:42:34.687: V/X(10217): Response:After lm1.requestLocationUpdates 

但除此之外,代码是无法访问的......即使 GPS 标志显示在操作栏中,但显示位置的 toast 没有弹出......请帮助......!!!

4

1 回答 1

0

requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, Two_Min, ll1)您将 Two_Min(您已设置为 5*60*1000 = 300000)传递到 minDistance 参数中,该参数应该是米数。尝试将 minDistance 设置为 0。

于 2013-04-03T19:47:52.283 回答