歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android獲取系統隱藏服務實現鎖屏

Android獲取系統隱藏服務實現鎖屏

日期:2017/3/1 11:09:06   编辑:Linux編程

獲取手機經緯度有 gps , network , 基站 三種方式,我們可以根據定位的條件,獲取一個最好的定位方式。然後將獲取到經緯度信息發送到指定的手機號碼中。

  1. /*
  2. * 單態只允許存在一個實例.
  3. * 獲取手機的gps信息
  4. */
  5. public class GPSInfoService {
  6. private Context context;
  7. private LocationManager manager;
  8. SharedPreferences sp ;
  9. //私有化構造方法
  10. private GPSInfoService(Context context){
  11. this.context= context;
  12. manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
  13. sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
  14. }
  15. private static GPSInfoService mGPSService;
  16. public synchronized static GPSInfoService getInstance(Context context){
  17. if(mGPSService==null)
  18. mGPSService = new GPSInfoService(context);
  19. return mGPSService;
  20. }
  21. /*
  22. * 當前你的手機 所支持的定位方式獲取出來
  23. * 有多種定位方式 gps network ,基站, passive
  24. * 可以根據定位的條件 ,獲取 一個最好的定位方式http://www.linuxidc.com
  25. */
  26. public void registerLocationUpdates(){
  27. Criteria criteria = new Criteria();
  28. // 設置定位的精度
  29. criteria.setAccuracy(Criteria.ACCURACY_COARSE); //獲取大體的位置
  30. criteria.setAltitudeRequired(false); // 海拔信息
  31. criteria.setCostAllowed(true); //允許產生費用
  32. criteria.setPowerRequirement(Criteria.POWER_LOW); //低功耗
  33. //獲取一個最符合查詢條件的位置提供者
  34. String provider =manager.getBestProvider(criteria, true);
  35. // 位置改變就會調用Linster的監聽器 獲取經度緯度
  36. manager.requestLocationUpdates(provider, 60000, 0, getLinster());
  37. }
  38. public void cancleLocationUpdates(){
  39. manager.removeUpdates(getLinster());
  40. }
  41. private static MyGPSLinster myGPSLinser;
  42. private MyGPSLinster getLinster(){
  43. if(myGPSLinser==null)
  44. myGPSLinser = new MyGPSLinster();
  45. return myGPSLinser;
  46. }
  47. private class MyGPSLinster implements LocationListener{
  48. // 用戶位置改變的時候 的回調方法
  49. public void onLocationChanged(Location location) {
  50. //獲取到用戶的緯度
  51. double latitude= location.getLatitude();
  52. //獲取到用戶的經度
  53. double longitude = location.getLongitude();
  54. //進行封裝寫入到文件中
  55. String locationstr = "jing du "+ longitude + " weidu :"+latitude;
  56. Editor editor = sp.edit();
  57. editor.putString("lastlocation", locationstr);
  58. editor.commit();
  59. }
  60. // 狀態改變
  61. public void onStatusChanged(String provider, int status, Bundle extras) {
  62. // TODO Auto-generated method stub
  63. }
  64. //gps ,打開
  65. public void onProviderEnabled(String provider) {
  66. // TODO Auto-generated method stub
  67. }
  68. //關閉
  69. public void onProviderDisabled(String provider) {
  70. // TODO Auto-generated method stub
  71. }
  72. }
  73. /**
  74. * 獲取手機的最後一次位置
  75. * @return
  76. */
  77. public String getLastPosition(){
  78. return sp.getString("lastlocation", "");
  79. }
  80. }

獲取短信的經緯度並將獲取到的經緯度發送到指定的號碼上:

  1. //獲取當前手機的經緯度.
  2. GPSInfoService.getInstance(context).registerLocationUpdates();
  3. //把經緯度的信息發送到安全號碼 ,獲取到短信發送器,將短信發送到指定的號碼
  4. SmsManager smsManager = SmsManager.getDefault();
  5. smsManager.sendTextMessage("15287978798", null, GPSInfoService.getInstance(context).getLastPosition() , null, null);
Copyright © Linux教程網 All Rights Reserved