29GPS代码的移植

创建一个GPSService继承Service类,在后台不断监听位置的变化。

package com.ustc.mobilemanager.service;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;

public class GPSService extends Service {

	// 得到位置服务
	private LocationManager lm;
	private MyLocationListener listener;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		// 实例化
		lm = (LocationManager) getSystemService(LOCATION_SERVICE);
		// List<String> providers = lm.getAllProviders();
		// for (String l : providers) {
		// System.out.println(l);
		// }

		listener = new MyLocationListener();
		// 注册监听位置服务
		// 给内容提供者设置条件
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		String provider = lm.getBestProvider(criteria, true);
		lm.requestLocationUpdates(provider, 0, 0, listener);
	}

	class MyLocationListener implements LocationListener {

		/**
		 * 当位置改变的时候回调这个方法
		 * 
		 */
		@Override
		public void onLocationChanged(Location location) {

			// 经度
			String longitude = "jingdu:" + location.getLongitude() + "\n";
			// 纬度
			String latitude = "weidu:" + location.getLatitude()+ "\n";
			// 精度
			String accuracy = "accuracy:" + location.getAccuracy()+ "\n";
			
			
			//发短信给安全号码
			
			
			
			SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
			
			Editor editor = sp.edit();
			
			editor.putString("lastlocation", longitude +latitude + accuracy);
			
			editor.commit();
			
			

			// TextView textView = new TextView(MainActivity.this);
			// textView.setText(longitude + "\n" + latitude + "\n" + accuracy);
			// setContentView(textView);
		}

		/**
		 * 
		 * 当状态发生改变的时候
		 * 
		 */
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {

		}

		/**
		 * 
		 * 
		 * 某一个位置提供者可以使用了回调
		 */
		@Override
		public void onProviderEnabled(String provider) {

		}

		/**
		 * 
		 * 
		 * 某一个位置提供者不可以使用了回调
		 */
		@Override
		public void onProviderDisabled(String provider) {

		}

	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		// 取消监听位置服务
		lm.removeUpdates(listener);
		listener = null;
	}

}

使用SharedPreferences保存最后一次的位置,在SMSReceiver中获取位置,然后作比较。


if ("#*location*#".equals(body)) {
					// 得到手机的GPS
					Log.i(TAG, "得到手机的GPS");
					//启动服务
					Intent i = new Intent(context,GPSService.class);
					context.startService(i);
					
					SharedPreferences sp = context.getSharedPreferences("config", context.MODE_PRIVATE);
					String lastlocation = sp.getString("lastlocation", null);
					if (TextUtils.isEmpty(lastlocation)) {
						//位置没有得到
						
						SmsManager.getDefault().sendTextMessage(sender, null, "正在获取位置中...", null, null);
						
					}else {
						//得到位置了
						SmsManager.getDefault().sendTextMessage(sender, null, lastlocation, null, null);
						
					}
					
					// 把这个广播终止掉
					abortBroadcast();
				} 




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值