Android擷取手機位置的實現代碼_Android

來源:互聯網
上載者:User

本文執行個體為大家分享了Android擷取手機位置的方法,供大家參考,具體內容如下

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;     @Override   public IBinder onBind(Intent intent) {    // TODO Auto-generated method stub    return null;   }     @Override   public void onCreate() {    // TODO Auto-generated method stub    super.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);   }     @Override   public void onDestroy() {    super.onDestroy();    // 取消監聽位置服務    lm.removeUpdates(listener);    listener = null;   }     class MyLocationListener implements LocationListener {      /**     * 當位置改變的時候回調     */      @Override    public 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();      }      /**     * 當狀態發生改變的時候回調 開啟--關閉 ;關閉--開啟     */    @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      }     }    } 

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); 

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.