亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

百度地圖定位6.0動態權限問題

2019-11-09 15:34:41
字體:
來源:轉載
供稿:網友

Android6.0相比之前的Android版本有一個很大的不同點,就是動態的獲取權限。之前我們需要什么權限只需要在Manifest文件中聲明即可,在6.0中,又新增了運行時權限的動態檢測。

    Android6.0分了兩種權限Normal Permissions(安裝時自動授權,用戶也不能取消權限) and Dangerous Permissions(詳情在文章最后):

如果在 Android 6.0 以前的設備上,我們之前在清單文件中聲明權限是沒有問題的,但是如果是在 Android 6.0 設備上,并且項目targetSdkVersion你設置的是23,那再像之前那樣聲明權限,是不起作用的

此時你肯定想到了 如果 targetSdkVersion 值設置的小于23是不是就不會奔潰了,恩,確實如此, 此時即使使用Android6.0的設備,程序也不會奔潰,原因顯而易見,Android 的權限機制是 Android M 后才加入的。從 Android M 開始 應用程序申請的權限是在運行時動態賦給用戶的。所以就需要我們動態的申請權限了

之所以寫這篇文章是因為最近在寫一個APP的時候用到了百度定位,之前使用百度定位的SDK好好的,但是換了手機之后開始不行了,折騰半天,才意識到新手機是6.0的系統,搜索一番,有所了解,經過修改完成了工作,寫下來,權作備份,留人一起進步一個簡單的例子,實現點擊按鈕,屏幕出現當前位置申請密鑰,配置環境就按照官網上面的來,我們按照之前的方法來

package cn.lixyz.testpermission;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.widget.Button;import android.widget.TextView;import com.baidu.location.BDLocation;import com.baidu.location.BDLocationListener;import com.baidu.location.LocationClient;import com.baidu.location.LocationClientOption;import com.baidu.location.Poi;import java.util.List;public class MainActivity extends Activity /*implements BDLocationListener*/ { PRivate Button bt; private TextView tv; public LocationClient mLocationClient = null; public BDLocationListener myListener = new MyLocationListener(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mLocationClient = new LocationClient(getapplicationContext()); // 聲明LocationClient類 mLocationClient.registerLocationListener(myListener); // 注冊監聽函數 initLocation(); bt = (Button) findViewById(R.id.bt); tv = (TextView) findViewById(R.id.tv); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mLocationClient.start(); } }); } private void initLocation() { LocationClientOption option = new LocationClientOption(); option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);// 可選,默認高精度,設置定位模式,高精度,低功耗,僅設備 option.setCoorType("bd09ll");// 可選,默認gcj02,設置返回的定位結果坐標系 int span = 1000; option.setScanSpan(span);// 可選,默認0,即僅定位一次,設置發起定位請求的間隔需要大于等于1000ms才是有效的 option.setIsNeedAddress(true);// 可選,設置是否需要地址信息,默認不需要 option.setOpenGps(true);// 可選,默認false,設置是否使用gps option.setLocationNotify(true);// 可選,默認false,設置是否當gps有效時按照1S1次頻率輸出GPS結果 option.setIsNeedLocationDescribe(true);// 可選,默認false,設置是否需要位置語義化結果,可以在BDLocation.getLocationDescribe里得到,結果類似于“在北京天安門附近” option.setIsNeedLocationPoiList(true);// 可選,默認false,設置是否需要POI結果,可以在BDLocation.getPoiList里得到 option.setIgnoreKillProcess(false);// 可選,默認true,定位SDK內部是一個SERVICE,并放到了獨立進程,設置是否在stop的時候殺死這個進程,默認不殺死 option.SetIgnoreCacheException(false);// 可選,默認false,設置是否收集CRASH信息,默認收集 option.setEnableSimulateGps(false);// 可選,默認false,設置是否需要過濾gps仿真結果,默認需要 mLocationClient.setLocOption(option); } class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { // Receive Location StringBuffer sb = new StringBuffer(256); sb.append("time : "); sb.append(location.getTime()); sb.append("/nerror code : "); sb.append(location.getLocType()); sb.append("/nlatitude : "); sb.append(location.getLatitude()); sb.append("/nlontitude : "); sb.append(location.getLongitude()); sb.append("/nradius : "); sb.append(location.getRadius()); if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位結果 sb.append("/nspeed : "); sb.append(location.getSpeed());// 單位:公里每小時 sb.append("/nsatellite : "); sb.append(location.getSatelliteNumber()); sb.append("/nheight : "); sb.append(location.getAltitude());// 單位:米 sb.append("/ndirection : "); sb.append(location.getDirection());// 單位度 sb.append("/naddr : "); sb.append(location.getAddrStr()); sb.append("/ndescribe : "); sb.append("gps定位成功"); } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 網絡定位結果 sb.append("/naddr : "); sb.append(location.getAddrStr()); // 運營商信息 sb.append("/nOperationers : "); sb.append(location.getOperators()); sb.append("/ndescribe : "); sb.append("網絡定位成功"); } else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 離線定位結果 sb.append("/ndescribe : "); sb.append("離線定位成功,離線定位結果也是有效的"); } else if (location.getLocType() == BDLocation.TypeServerError) { sb.append("/ndescribe : "); sb.append("服務端網絡定位失敗,可以反饋IMEI號和大體定位時間到loc-bugs@baidu.com,會有人追查原因"); } else if (location.getLocType() == BDLocation.TypeNetWorkException) { sb.append("/ndescribe : "); sb.append("網絡不同導致定位失敗,請檢查網絡是否通暢"); } else if (location.getLocType() == BDLocation.TypeCriteriaException) { sb.append("/ndescribe : "); sb.append("無法獲取有效定位依據導致定位失敗,一般是由于手機的原因,處于飛行模式下一般會造成這種結果,可以試著重啟手機"); } sb.append("/nlocationdescribe : "); sb.append(location.getLocationDescribe());// 位置語義化信息 List<Poi> list = location.getPoiList();// POI數據 if (list != null) { sb.append("/npoilist size = : "); sb.append(list.size()); for (Poi p : list) { sb.append("/npoi= : "); sb.append(p.getId() + " " + p.getName() + " " + p.getRank()); } } if (location.getCity() != null) { Message msg = new Message(); msg.obj = location.getCity(); handler.sendMessage(msg); } } } Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { tv.setText((String) msg.obj); mLocationClient.stop(); } };}

我們點擊按鈕,發現并沒有定位成功,查看log,發現如下log

04-24 02:16:59.994 5329-5354/? W/System.err: java.lang.SecurityException: getAllCellInfo: Neither user 10198 nor current process has android.permission.access_COARSE_LOCATION.04-24 02:16:59.994 5329-5354/? W/System.err: at android.app.ContextImpl.enforce(ContextImpl.java:1595)04-24 02:16:59.994 5329-5354/? W/System.err: at android.app.ContextImpl.enforceCallingOrSelfPermission(ContextImpl.java:1627)04-24 02:16:59.994 5329-5354/? W/System.err: at android.content.ContextWrapper.enforceCallingOrSelfPermission(ContextWrapper.java:675)04-24 02:16:59.994 5329-5354/? W/System.err: at android.content.ContextWrapper.enforceCallingOrSelfPermission(ContextWrapper.java:675)04-24 02:16:59.994 5329-5354/? W/System.err: at com.android.phone.PhoneInterfaceManager.enforceFineOrCoarseLocationPermission(PhoneInterfaceManager.java:1835)04-24 02:16:59.994 5329-5354/? W/System.err: at com.android.phone.PhoneInterfaceManager.getAllCellInfoUsingSubId(PhoneInterfaceManager.java:1920)04-24 02:16:59.994 5329-5354/? W/System.err: at com.android.phone.PhoneInterfaceManager.getAllCellInfo(PhoneInterfaceManager.java:1916)04-24 02:16:59.994 5329-5354/? W/System.err: at com.android.internal.telephony.ITelephony$Stub.onTransact(ITelephony.java:732)04-24 02:16:59.994 5329-5354/? W/System.err: at android.os.Binder.execTransact(Binder.java:453)需要android.permission.ACCESS_COARSE_LOCATION權限,我們在Manifest文件中添加之后再運行,發現還是拋同樣的異常,依舊獲取不到位置所以我們就需要針對6.0系統做出修改,來動態申請權限了。修改代碼:

bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startLocation(); } });private void startLocation() { int checkPermission = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION); if (checkPermission != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1); Log.d("TTTT", "彈出提示"); return; } else { mLocationClient.start(); } }enter description here

04-24 02:29:16.163 6134-6134/cn.lixyz.testpermission D/TTTT: 彈出提示

我們點擊“始終允許”之后,退出程序再此進入,則可以定位了,我們如果我們像要在運行了權限之后立即就可以獲取到位置信息呢?Android提供了onRequestPermissionsResult方法來幫我們實現我們執行了權限規則之后的操作,這個方法和onActivityResult方法類似

publicvoidonRequestPermissionsResult(int requestCode, String[] permissions,int[] grantResults){

switch (requestCode)

{

case1:

if (grantResults[0] == PackageManager.PERMISSION_GRANTED)

{ mLocationClient.start(); } else { Log.d("TTTT","啊偶,被拒絕了,少年不哭,站起來擼"); } break;

default:super.onRequestPermissionsResult(requestCode, permissions, grantResults); } }

當我們點擊禁止按鈕的時候,Log提示"啊偶,被拒絕了,少年不哭,站起來擼"當我們點擊允許按鈕的時候,定位成功完整代碼如下

publicclassMainActivityextendsActivity{

private Button bt;

private TextView tv;

public LocationClient mLocationClient = null;

public BDLocationListener myListener = new MyLocationListener();

@OverrideprotectedvoidonCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mLocationClient = new LocationClient(getApplicationContext()); // 聲明LocationClient類 mLocationClient.registerLocationListener(myListener); // 注冊監聽函數 initLocation();

bt = (Button) findViewById(R.id.bt);

tv = (TextView) findViewById(R.id.tv);

/**

*點擊執行6.0動態定位權限

*/

bt.setOnClickListener(new View.OnClickListener() {

@Override

publicvoidonClick(View v){//mLocationClient.start(); startLocation(); } }); }

privatevoidstartLocation(){

if(Build.VERSION.SDK_INT>=23){int checkPermission = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION);

if (checkPermission != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);return; } else { mLocationClient.start(); }

}else{  mLocationClient.start();

} }

/**

*運行了權限之后立即就可以獲取到位置信息

*/

@Override

publicvoidonRequestPermissionsResult(int requestCode, String[] permissions,int[] grantResults){

switch (requestCode) {

case1:

if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {mLocationClient.start(); }

else { Log.d("TTTT","啊偶,被拒絕了,少年不哭,站起來擼"); } break;

default:super.onRequestPermissionsResult(requestCode, permissions, grantResults); } }

/**

*下面就是常規的定位了,如果不懂可以去百度API查詢

*/

privatevoidinitLocation(){ LocationClientOption option =new LocationClientOption();

option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);// 可選,默認高精度,設置定位模式,高精度,低功耗,僅設備 option.setCoorType("bd09ll");// 可選,默認gcj02,設置返回的定位結果坐標系int span = 1000; option.setScanSpan(span);// 可選,默認0,即僅定位一次,設置發起定位請求的間隔需要大于等于1000ms才是有效的 option.setIsNeedAddress(true);// 可選,設置是否需要地址信息,默認不需要 option.setOpenGps(true);// 可選,默認false,設置是否使用gps option.setLocationNotify(true);// 可選,默認false,設置是否當gps有效時按照1S1次頻率輸出GPS結果 option.setIsNeedLocationDescribe(true);// 可選,默認false,設置是否需要位置語義化結果,可以在BDLocation.getLocationDescribe里得到,結果類似于“在北京天安門附近” option.setIsNeedLocationPoiList(true);// 可選,默認false,設置是否需要POI結果,可以在BDLocation.getPoiList里得到 option.setIgnoreKillProcess(false);// 可選,默認true,定位SDK內部是一個SERVICE,并放到了獨立進程,設置是否在stop的時候殺死這個進程,默認不殺死 option.SetIgnoreCacheException(false);// 可選,默認false,設置是否收集CRASH信息,默認收集 option.setEnableSimulateGps(false);// 可選,默認false,設置是否需要過濾gps仿真結果,默認需要 mLocationClient.setLocOption(option); }classMyLocationListenerimplementsBDLocationListener{@OverridepublicvoidonReceiveLocation(BDLocation location){// Receive Location StringBuffer sb =new StringBuffer(256); sb.append("time : "); sb.append(location.getTime()); sb.append("/nerror code : "); sb.append(location.getLocType()); sb.append("/nlatitude : "); sb.append(location.getLatitude()); sb.append("/nlontitude : "); sb.append(location.getLongitude()); sb.append("/nradius : "); sb.append(location.getRadius()); if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位結果 sb.append("/nspeed : "); sb.append(location.getSpeed());// 單位:公里每小時 sb.append("/nsatellite : "); sb.append(location.getSatelliteNumber()); sb.append("/nheight : "); sb.append(location.getAltitude());// 單位:米 sb.append("/ndirection : "); sb.append(location.getDirection());// 單位度 sb.append("/naddr : "); sb.append(location.getAddrStr()); sb.append("/ndescribe : "); sb.append("gps定位成功"); } elseif(location.getLocType()== BDLocation.TypeNetWorkLocation) {// 網絡定位結果 sb.append("/naddr : "); sb.append(location.getAddrStr());// 運營商信息 sb.append("/noperationers : "); sb.append(location.getOperators()); sb.append("/ndescribe : "); sb.append("網絡定位成功"); } elseif(location.getLocType()== BDLocation.TypeOffLineLocation) {// 離線定位結果 sb.append("/ndescribe : "); sb.append("離線定位成功,離線定位結果也是有效的"); } elseif(location.getLocType()== BDLocation.TypeServerError) { sb.append("/ndescribe : "); sb.append("服務端網絡定位失敗,可以反饋IMEI號和大體定位時間到loc-bugs@baidu.com,會有人追查原因"); } elseif(location.getLocType()== BDLocation.TypeNetWorkException) { sb.append("/ndescribe : "); sb.append("網絡不同導致定位失敗,請檢查網絡是否通暢"); } elseif(location.getLocType()== BDLocation.TypeCriteriaException) { sb.append("/ndescribe : "); sb.append("無法獲取有效定位依據導致定位失敗,一般是由于手機的原因,處于飛行模式下一般會造成這種結果,可以試著重啟手機"); } sb.append("/nlocationdescribe : "); sb.append(location.getLocationDescribe());// 位置語義化信息 List<Poi> list = location.getPoiList();// POI數據if (list != null) { sb.append("/npoilist size = : "); sb.append(list.size()); for (Poi p : list) { sb.append("/npoi= : "); sb.append(p.getId() + " " + p.getName() + " " + p.getRank()); } } if (location.getCity() != null) { Message msg = new Message(); msg.obj = location.getCity(); handler.sendMessage(msg); } } } Handler handler =new Handler() { publicvoidhandleMessage(android.os.Message msg){ tv.setText((String) msg.obj); mLocationClient.stop(); } };}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
美女精品视频一区| 亚洲欧美一区二区三区情侣bbw| 久久久国产精品x99av| 亚洲va久久久噜噜噜| 精品久久久久久久久久久| 亚洲另类图片色| 91免费看片网站| 国色天香2019中文字幕在线观看| 久久久亚洲影院你懂的| 91中文字幕在线| 日本aⅴ大伊香蕉精品视频| 超碰91人人草人人干| 成人妇女免费播放久久久| 亚洲黄页网在线观看| 亚洲精品视频网上网址在线观看| 亚洲图片欧美午夜| 久久国产精品影片| 亚洲免费人成在线视频观看| 日韩精品亚洲元码| 欧美有码在线观看| 亚洲xxxxx| 91av中文字幕| 最好看的2019年中文视频| 国产欧美日韩亚洲精品| 亚洲伊人一本大道中文字幕| 国产在线精品播放| 成人网欧美在线视频| 亚洲精品色婷婷福利天堂| 伊人久久大香线蕉av一区二区| 欧美亚洲一级片| 国产精品久久久久久久久影视| 色爱精品视频一区| 日韩最新中文字幕电影免费看| 91九色单男在线观看| 日本欧美爱爱爱| 精品国产依人香蕉在线精品| 亚洲精品永久免费精品| 日韩久久精品电影| 成人网在线视频| 亚洲黄色在线观看| 精品久久久久久久大神国产| 午夜精品久久17c| 精品久久香蕉国产线看观看gif| 亚洲国产精品久久久久| 欧美黑人巨大xxx极品| 在线日韩日本国产亚洲| 国产精品99蜜臀久久不卡二区| 欧美黄色www| 97国产一区二区精品久久呦| 亚洲性视频网站| 高清欧美性猛交xxxx| 国产精品久久久久免费a∨大胸| 欧美成人精品一区| 欧美极品少妇与黑人| 久久这里只有精品99| 97视频在线免费观看| 欧美日韩国产中字| 亚洲精品理论电影| 久久精品国产欧美亚洲人人爽| 日韩av一区二区在线观看| 国产精品福利在线观看网址| 国产精品高潮在线| 久久久成人的性感天堂| 丝袜美腿精品国产二区| 日韩少妇与小伙激情| 欧美日韩在线免费观看| 中文字幕自拍vr一区二区三区| 午夜精品一区二区三区在线视频| 国产美女搞久久| 亚洲最大福利视频| 欧美日韩另类字幕中文| 欧美国产日韩免费| 日韩欧美国产中文字幕| 国产精品视频在线观看| 欧美日韩国产123| 亚洲情综合五月天| 亚洲精品在线观看www| 欧美在线www| 欧美理论在线观看| 日韩中文字幕视频在线观看| 亚洲网在线观看| 青青a在线精品免费观看| 97香蕉久久超级碰碰高清版| 亚洲一区第一页| 国产精品女主播视频| 亚洲欧美自拍一区| 国产精品久久久久久久久久ktv| 久热精品视频在线免费观看| 91高清视频免费观看| 日韩av在线免播放器| 亚洲福利视频网站| 精品免费在线视频| 欧美一区二区色| 日韩av影视综合网| 欧美电影在线免费观看网站| 日韩欧美在线观看| 68精品国产免费久久久久久婷婷| 日韩免费在线视频| 欧美日韩国产第一页| 亚洲精品网址在线观看| 97色在线视频| 亚洲天堂一区二区三区| 欧美激情乱人伦| www国产精品com| 久久99亚洲精品| 久久国产视频网站| 欧美激情中文网| 欧美裸体视频网站| 高清一区二区三区四区五区| 欧美午夜精品久久久久久人妖| 国产成人精品视频| 欧美疯狂做受xxxx高潮| 在线中文字幕日韩| 亚洲日韩中文字幕在线播放| 日韩av色在线| 最近免费中文字幕视频2019| 国产精品日本精品| 欧美丝袜美女中出在线| 97久久伊人激情网| 欧美午夜久久久| 色av吧综合网| 国产精品99久久久久久久久| 久久国产视频网站| 欧美一区二区三区免费观看| 91国偷自产一区二区三区的观看方式| 91精品视频观看| 欧美久久精品午夜青青大伊人| 国产精品亚洲一区二区三区| 久久久视频免费观看| 在线观看欧美成人| 国产一区二区精品丝袜| 久久精品国产亚洲精品| 久久久久久网址| 日韩av成人在线观看| 黑丝美女久久久| 成人免费视频97| 7777精品久久久久久| 久久人体大胆视频| 中国日韩欧美久久久久久久久| 欧美精品xxx| 亚洲女在线观看| 国产精品福利无圣光在线一区| xvideos国产精品| 日本aⅴ大伊香蕉精品视频| 精品久久久久久中文字幕| 亚洲欧美国产日韩中文字幕| 午夜精品美女自拍福到在线| 97国产真实伦对白精彩视频8| 欧美大成色www永久网站婷| 亚洲三级 欧美三级| 日韩在线观看免费全集电视剧网站| 91精品久久久久久久久青青| 日韩av在线免费观看| 亚洲女人天堂网| 久久久亚洲精品视频| 欧美国产精品va在线观看| 亚洲国产女人aaa毛片在线| 成人欧美在线视频| 亚洲精品99久久久久中文字幕| 久久久精品免费| 欧美成aaa人片在线观看蜜臀| 色综合男人天堂| 成人在线国产精品| 欧美日韩国产一区在线|