Android擷取手機位置代碼實現

來源:互聯網
上載者:User

標籤:擷取手機位置代碼實現

1、項目Src下建立...service包,然後建立GPSService類

package com.zebra.mobilesafe.service;import java.io.IOException;import java.io.InputStream;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;import android.util.Log;public class GPSService extends Service {private static final String TAG = "GPSService";// 用到位置服務private LocationManager lm;private MyLocationListener listener;@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;}@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();Log.i(TAG, "====進入GPS==");lm = (LocationManager) getSystemService(LOCATION_SERVICE);// List<String> provider = lm.getAllProviders();// for(String l: provider){// System.out.println(l);// }listener = new MyLocationListener();// 註冊監聽位置服務// 給位置提供者設定條件Criteria criteria = new Criteria();criteria.setAccuracy(Criteria.ACCURACY_FINE);// 設定參數細化:// criteria.setAccuracy(Criteria.ACCURACY_FINE);//設定為最大精度// criteria.setAltitudeRequired(false);//不要求海拔資訊// criteria.setBearingRequired(false);//不要求方位資訊// criteria.setCostAllowed(true);//是否允許付費// criteria.setPowerRequirement(Criteria.POWER_LOW);//對電量的要求String proveder = lm.getBestProvider(criteria, true);lm.requestLocationUpdates(proveder, 0, 0, listener);}@Overridepublic void onDestroy() {super.onDestroy();// 取消監聽位置服務lm.removeUpdates(listener);listener = null;}class MyLocationListener implements LocationListener {/** * 當位置改變的時候回調 */@Overridepublic void onLocationChanged(Location location) {String longitude = "j:" + location.getLongitude() + "\n";String latitude = "w:" + location.getLatitude() + "\n";String accuracy = "a" + location.getAccuracy() + "\n";// 發簡訊給安全號碼// 把標準的GPS座標轉換成火星座標//InputStream is;//try {//is = getAssets().open("axisoffset.dat");//ModifyOffset offset = ModifyOffset.getInstance(is);//PointDouble double1 = offset.s2c(new PointDouble(location//.getLongitude(), location.getLatitude()));//longitude ="j:" + offset.X+ "\n";//latitude =  "w:" +offset.Y+ "\n";////} catch (IOException e) {//// TODO Auto-generated catch block//e.printStackTrace();//} catch (Exception e) {//// TODO Auto-generated catch block//e.printStackTrace();//}SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);Editor editor = sp.edit();editor.putString("lastlocation", longitude + latitude + accuracy);editor.commit();}/** * 當狀態發生改變的時候回調 開啟--關閉 ;關閉--開啟 */@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {// TODO Auto-generated method stub}/** * 某一個位置提供者可以使用了 */@Overridepublic void onProviderEnabled(String provider) {// TODO Auto-generated method stub}/** * 某一個位置提供者不可以使用了 */@Overridepublic void onProviderDisabled(String provider) {// TODO Auto-generated method stub}}}

2、AndroidManiFest.xml添加類

注意這個是service類型,很容易出錯,即<service  android:name="com.zebra.mobilesafe.service.GPSService"/>

3、添加許可權

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/><uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>

4、根據操作啟動服務
Intent i = new Intent(context,GPSService.class);context.startService(i);




Android擷取手機位置代碼實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.