Android 安卓:停止服务
我正在尝试一个演示,在其中的开始按钮,我开始一项服务。此服务在后台运行,获取GPS位置并将其发送到服务器。代码如下:Android 安卓:停止服务,android,service,android-service,android-location,android-wake-lock,Android,Service,Android Service,Android Location,Android Wake Lock,我正在尝试一个演示,在其中的开始按钮,我开始一项服务。此服务在后台运行,获取GPS位置并将其发送到服务器。代码如下: public void closeMyService() { context.unregisterReceiver(receiver); locationManager.removeUpdates(this); } 主要活动 public class MainActivity extends Activity implements OnClickListener
public void closeMyService() {
context.unregisterReceiver(receiver);
locationManager.removeUpdates(this);
}
主要活动
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = (Button) findViewById(R.id.button_start);
Button stop = (Button) findViewById(R.id.button_stop);
start.setOnClickListener(this);
stop.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.button_start){
Intent intent = new Intent(this,AndroidLocationServices.class);
startService(intent);
} else if(v.getId() == R.id.button_stop){
Intent intent = new Intent(this,AndroidLocationServices.class);
stopService(intent);
}
}
}
Android位置监听器
public class AndroidLocationServices extends Service {
WakeLock wakeLock;
private LocationManager locationManager;
String lat,longi;
public AndroidLocationServices() {
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
PowerManager pm = (PowerManager) getSystemService(this.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DoNotSleep");
Log.e("Google", "Service Created");
}
public void onStart(Intent intent, int startId) {
Log.e("Google", "Service Started");
locationManager = (LocationManager) getApplicationContext()
.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
5000, 5, listener);
}
private LocationListener listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.e("Google", "Location Changed");
if (location == null)
return;
if (isConnectingToInternet(getApplicationContext())) {
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
try {
Log.e("latitude", location.getLatitude() + "");
Log.e("longitude", location.getLongitude() + "");
lat = String.valueOf(location.getLatitude());
longi = String.valueOf(location.getLongitude());
jsonObject.put("latitude", location.getLatitude());
jsonObject.put("longitude", location.getLongitude());
jsonArray.put(jsonObject);
Log.e("request", jsonArray.toString());
new LocationWebService().execute();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
@Override
public void onDestroy() {
super.onDestroy();
wakeLock.release();
}
public static boolean isConnectingToInternet(Context _context) {
ConnectivityManager connectivity = (ConnectivityManager) _context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}
public class LocationWebService extends AsyncTask<String, String, Boolean> {
public LocationWebService() {
}
@Override
protected Boolean doInBackground(String... arg0) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("userLang", longi));
nameValuePairs.add(new BasicNameValuePair("userLat", lat));
nameValuePairs.add(new BasicNameValuePair("device_type", "Android"));
Log.d("Lat",lat);
Log.d("Lan",longi);
// Service Handler to call PHP URL
ServiceHandler serviceHandler = new ServiceHandler();
// Creating service handler class instance
String jsonStr = serviceHandler.makeHttpRequest("URL", "GET", nameValuePairs);
return null;
}
}
}
public class ServiceHandler {
// Global Declaration.
static String json = "";
public final static int GET = 1;
public final static int POST = 2;
/*
* Constructor.
*/
public ServiceHandler() {
}
/*
* Making service call
* @url - url to make request
* @method - http request method
* */
public String makeHttpRequest(String url, String method, List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
// adding post params
if (params != null) {
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
json = EntityUtils.toString(httpEntity);
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
json = EntityUtils.toString(httpEntity);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return json;
}
}
请建议我现在做什么。首先:
@Override
public void onDestroy() {
wakeLock.release();
stopMyService();
super.onDestroy();
}
第二:
在停止服务之前,您必须先注销侦听器(位置1),然后才能停止:
stopSelf();
取消注册侦听器是一种很好的做法,如下所示:
public void closeMyService() {
context.unregisterReceiver(receiver);
locationManager.removeUpdates(this);
}
在destroy方法中将WakeLock对象检查为null,因为有时服务无法启动,然后在onDestroy()方法中,我们需要检查它
if( wakeLock.isHeld())
{
wakeLock.release();
}
onStart()方法已被弃用,因此可以改用onStartCommand()。
希望它能达到您的目的现在检查问题。@ManojFegde我已使用isHeld()函数在上述模式中编辑了我的答案检查wakeLock对象,错误现已解决