2012-05-12 32 views
0

我有一个BrodcastReceiver运行的服务,它运行并提供我请求的GPS数据,并且在我完成了想要完成的任务后停止了它,但是我注意到它并没有提供我所需的数据,没有停止过,任何机构可以帮助我找出我的问题 的代码如下,服务在真实设备测试:服务不起作用?

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

public class GpsService extends Service implements LocationListener{ 
    static LocationManager mlocManager; 

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

    } 

    @Override 
    public void onDestroy() { 

    } 

    @Override 
    public void onStart(Intent intent, int startid) { 
     mlocManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE); 
     mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0, 0,this); 

    } 

     @Override 
    public void onLocationChanged(Location loc) 
    { 
    loc.getLatitude(); 
    loc.getLongitude(); 
    String Text = "My current location is: " +"Latitud = " + loc.getLatitude() +"Longitud = " + loc.getLongitude(); 
    Toast.makeText(getApplicationContext(),Text,Toast.LENGTH_SHORT).show(); 
    // Here to stop the service and make it finish its task 
    SystemClock.sleep(40000); 
    // stop the Gps system by this application 
    mlocManager.removeUpdates(this); 
    //Here to stop the service by itself 
    stopSelf(); 

    } 
    @Override 
    public void onProviderDisabled(String provider) 
    { 
    Toast.makeText(getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onProviderEnabled(String provider) 
    { 
    Toast.makeText(getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show(); 
    } 
    @Override 

    public void onStatusChanged(String provider, int status, Bundle extras) 
    { 
    } 


} 
+0

Husam这不是使用SystemClock.sleep(40000)的好习惯; 等待。你会使用Timer来获得更好的结果 –

回答

0
  1. 你对清单中的正确的权限?
  2. 你为什么要让这项服务睡眠很久?该服务在UI线程上工作,并且这样的事情会导致服务在大约5秒后被系统自动终止,因为ui线程不响应。
  3. 如果您希望延迟停止和/或显示敬酒,请使用Handler(和postDelayed),或使用asyncTask或您自己的东西。
  4. 你打开GPS吗?你有没有考虑使用假的位置应用程序或模拟器来完成这项任务?
  5. 祝你好运。