国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android8.0適配前臺定位服務service的示例代碼

2019-12-12 00:42:24
字體:
來源:轉載
供稿:網友

從Android 8.0開始系統為實現降低功耗,對后臺應用獲取用戶位置信息頻率進行了限制,每小時只允許更新幾次位置信息,詳細信息請參考官方說明。按照官方指引,如果要提高位置更新頻率,需要后臺應用提供一個前臺服務通知告知。

所以原來的單單使用locationManager獲得當前位置在后臺情況下無法使用了。于是打算使用一個前臺服務,當app在后臺時也能獲得當前位置。

查了幾篇博客說前臺服務需要在service的onStartCommand方法中調用startForeground(int, Notification)才能開啟前臺服務。

但是onStartCommand需要走startservice()的生命周期才會調用。

我改用了bindservice() 正好需要activity和service交互,當然兩個啟動方法混用也可以。但是沒有必要。

我需要的只是和控件綁定的service并且不想處理服務的結束操作。

1、activity / fragment調用 綁定服務

Intent serviceIntent = new Intent(this, ForegroundLocationService.class);bindService(serviceIntent, conn, Service.BIND_AUTO_CREATE);// 綁定服務時要求傳入一個ServiceConnection實現類的對象// 綁定服務時,會觸發服務的onBind方法,此方法會返回一個Ibinder的對象給activity / fragment的onServiceConnected(),通過這個對象可以訪問服務中的方法  ServiceConnection conn = new ServiceConnection() {    @Override    public void onServiceDisconnected(ComponentName name) {    }    @Override    public void onServiceConnected(ComponentName name, IBinder service) {    }  };

2、我在onBind()方法中調用了startForeground(int, Notification)

第一個參數是一個不為0的正整數,代表通知的id,第二個參數代表需要顯示的通知。

適配8.0的通知構建需要適配,不然會導致你的通知無法顯示(第一次調用的時候還以為是一加攔截了通知)

3、那么這時候應該已經實現了前臺服務,需要把服務獲得的位置信息傳遞給activity。(直接調用locationmanager就可以獲得,這里把位置實現隱去)

  public class MyBinder extends Binder {    public ForegroundLocationService getService(){      return ForegroundLocationService.this;    }  }  //通過binder實現調用者client與Service之間的通信  private MyBinder binder = new MyBinder();  //通過service的onBind()方法返回我們實例化的MyBinder對象,該對象可以獲的當前的Service  @Override  public IBinder onBind(Intent arg0) {    NotificationUtils notificationUtils = new NotificationUtils(this);    startForeground(111, notificationUtils.getNotification("Notice", "Continuous positioning",null));    return binder;  }

4、然后需要進行控件和服務的交互,這里就分成了三種方法

  • 在得到service的情況下act主動調用得到數據
  • 在service中設置回調,service主動傳遞數據給act
  • 通過廣播傳遞數據。
  ServiceConnection conn = new ServiceConnection() {    @Override    public void onServiceDisconnected(ComponentName name) {    }    @Override    public void onServiceConnected(ComponentName name, IBinder service) {        //通過這個方法可以得到service的實例,通過設置回調可以持續更新        ForegroundLocationService foregroundLocationService = ((ForegroundLocationService.MyBinder) service).getService();         foregroundLocationService.setLocationCallback(new ForegroundLocationService.LocationCallback() {          @Override          public void onLocation(Location location) {        }      });    }  };

在service中編寫接口,并在獲得位置的回調方法中調用。

  public interface LocationCallback {    /**    * 當前位置    */    void onLocation(Location location);  }  private LocationCallback mLocationCallback;  private class LocationListener implements android.location.LocationListener {    public LocationListener(String provider) {      Logger.e(TAG, "LocationListener " + provider);    }    @Override    public void onLocationChanged(Location location) {      Log.i("location", "onLocationChanged: " + "當前坐標:" + location.getLatitude() + " : " + location.getLongitude());      if(mLocationCallback!=null){        mLocationCallback.onLocation(location);      }    }  }

Service向Activity發送消息,可以使用廣播,當然Activity要注冊相應的接收器。比如Service要向多個Activity發送同樣的消息的話,用這種方法就更好,這里就省略不寫了。具體可以參考下面的文章。

參考文章:http://m.survivalescaperooms.com/article/123316.htm

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 金寨县| 红原县| 和顺县| 蛟河市| 竹山县| 浙江省| 尼勒克县| 通辽市| 台州市| 陇南市| 库车县| 印江| 石狮市| 娄底市| 来宾市| 永嘉县| 鸡泽县| 通道| 香港| 龙州县| 四会市| 缙云县| 阳东县| 灵武市| 永新县| 黑水县| 望谟县| 嘉义县| 高邑县| 卢湾区| 蓝田县| 浦东新区| 太康县| 沂源县| 嫩江县| 海淀区| 弋阳县| 普宁市| 莱州市| 甘肃省| 罗城|