为什么Android位置服务没有运行?

时间:2014-12-27 06:42:05

标签: android json location android-service

我想使用服务更新我在服务器上的位置,但我不明白为什么我的服务没有运行。这是代码:

package com.example.friendlocation;

import java.util.ArrayList; 
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.Service; 
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Criteria; 
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 UpdateLocationService extends Service {

String url = "http://coders-hub.com/googlemap/new/location.php";
JSONParser jsonParser = new JSONParser();
String id;

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

    if(intent==null || intent.getAction()==null)
        return START_NOT_STICKY;

    Toast.makeText(getApplicationContext(), "In onCreate()!", Toast.LENGTH_SHORT).show();

    SharedPreferences sp=getSharedPreferences("location", Activity.MODE_PRIVATE);
    id=sp.getString("id", null);
    if(id!=null)
    {
        Toast.makeText(getApplicationContext(), "In id!", Toast.LENGTH_SHORT).show();
         LocationManager lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
         Criteria c=new Criteria();
         String provider=lm.getBestProvider(c, false);
         lm.requestLocationUpdates(provider, 0, 0, new LocationListener() {

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

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

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

            @Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("id", id));
                params.add(new BasicNameValuePair("lat", location.getLatitude()+""));
                params.add(new BasicNameValuePair("longi", location.getLongitude()+""));
                JSONObject json=jsonParser.makeHttpRequest(url,"POST", params);
                Toast.makeText(getApplicationContext(), "In onLocationChanged()!", Toast.LENGTH_SHORT).show();
                try {
                    int success = json.getInt("success");
                    if (success == 1) 
                        Toast.makeText(getApplicationContext(), "Updated in Service!", Toast.LENGTH_SHORT).show();
                    else
                        Toast.makeText(getApplicationContext(), "Not updated in Service!", Toast.LENGTH_SHORT).show();
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    //e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "Error in JsonException Service!", Toast.LENGTH_SHORT).show();
                }

            }
        });
     }

    return START_NOT_STICKY;
}

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

 @Override
   public void onDestroy() {
      super.onDestroy();
      Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
   }

 }

我在AndroidManifast.xml文件中添加了权限,并按照以下方式声明服务:

<service android:name="com.example.friendlocation.UpdateLocationService">
    </service>

从侦听器类调用它:

Intent i=new Intent(context, UpdateLocationService.class);
context.startService(i);

但是服务还没有开始。

0 个答案:

没有答案