Submit Search
Upload
Android 基礎課程補充資料
•
Download as PPTX, PDF
•
1 like
•
278 views
Duran Hsieh
Follow
GPS 感測器問題解答 上架計數程式說明
Read less
Read more
Technology
Report
Share
Report
Share
1 of 20
Download now
Recommended
Android基礎課程3 - APP上架、廣告與 Facebook 登入
Android基礎課程3 - APP上架、廣告與 Facebook 登入
Duran Hsieh
Android基礎課程2 - google map android API
Android基礎課程2 - google map android API
Duran Hsieh
Android 基礎開發課程
Android 基礎開發課程
Duran Hsieh
聽微軟專家說為何.NET開發非學不可?
聽微軟專家說為何.NET開發非學不可?
Duran Hsieh
DevSecOps 實踐與 GitHub 進階安全: 建立安全的開發流程
DevSecOps 實踐與 GitHub 進階安全: 建立安全的開發流程
Duran Hsieh
Visual Studio Dev Tunnel.pdf
Visual Studio Dev Tunnel.pdf
Duran Hsieh
GDG Taichung: What is new in Firebase
GDG Taichung: What is new in Firebase
Duran Hsieh
GDG Taichung - Flutter and Firebase.pdf
GDG Taichung - Flutter and Firebase.pdf
Duran Hsieh
Recommended
Android基礎課程3 - APP上架、廣告與 Facebook 登入
Android基礎課程3 - APP上架、廣告與 Facebook 登入
Duran Hsieh
Android基礎課程2 - google map android API
Android基礎課程2 - google map android API
Duran Hsieh
Android 基礎開發課程
Android 基礎開發課程
Duran Hsieh
聽微軟專家說為何.NET開發非學不可?
聽微軟專家說為何.NET開發非學不可?
Duran Hsieh
DevSecOps 實踐與 GitHub 進階安全: 建立安全的開發流程
DevSecOps 實踐與 GitHub 進階安全: 建立安全的開發流程
Duran Hsieh
Visual Studio Dev Tunnel.pdf
Visual Studio Dev Tunnel.pdf
Duran Hsieh
GDG Taichung: What is new in Firebase
GDG Taichung: What is new in Firebase
Duran Hsieh
GDG Taichung - Flutter and Firebase.pdf
GDG Taichung - Flutter and Firebase.pdf
Duran Hsieh
GitHub Action Introduction
GitHub Action Introduction
Duran Hsieh
Cloud Study Jam - ML API 4
Cloud Study Jam - ML API 4
Duran Hsieh
Cloud Study Jam ML API 3
Cloud Study Jam ML API 3
Duran Hsieh
GDG Taichung: Cloud Study Jam ML API
GDG Taichung: Cloud Study Jam ML API
Duran Hsieh
GDG Taichung - Firebase Introduction 01
GDG Taichung - Firebase Introduction 01
Duran Hsieh
Study4TW .NET Conf Local Event Taichung 2018 slideshow
Study4TW .NET Conf Local Event Taichung 2018 slideshow
Duran Hsieh
What is .NET Chinese ver
What is .NET Chinese ver
Duran Hsieh
Microsoft recommendation solution on azure
Microsoft recommendation solution on azure
Duran Hsieh
Microsoft professional program introduction
Microsoft professional program introduction
Duran Hsieh
聰明的投資者
聰明的投資者
Duran Hsieh
聊天機器人概論 Introduce to chat bot sevices
聊天機器人概論 Introduce to chat bot sevices
Duran Hsieh
[Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享
[Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享
Duran Hsieh
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練6
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練6
Duran Hsieh
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5
Duran Hsieh
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
Duran Hsieh
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練3
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練3
Duran Hsieh
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練2
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練2
Duran Hsieh
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練1(20160222)
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練1(20160222)
Duran Hsieh
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5
Duran Hsieh
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
Duran Hsieh
More Related Content
More from Duran Hsieh
GitHub Action Introduction
GitHub Action Introduction
Duran Hsieh
Cloud Study Jam - ML API 4
Cloud Study Jam - ML API 4
Duran Hsieh
Cloud Study Jam ML API 3
Cloud Study Jam ML API 3
Duran Hsieh
GDG Taichung: Cloud Study Jam ML API
GDG Taichung: Cloud Study Jam ML API
Duran Hsieh
GDG Taichung - Firebase Introduction 01
GDG Taichung - Firebase Introduction 01
Duran Hsieh
Study4TW .NET Conf Local Event Taichung 2018 slideshow
Study4TW .NET Conf Local Event Taichung 2018 slideshow
Duran Hsieh
What is .NET Chinese ver
What is .NET Chinese ver
Duran Hsieh
Microsoft recommendation solution on azure
Microsoft recommendation solution on azure
Duran Hsieh
Microsoft professional program introduction
Microsoft professional program introduction
Duran Hsieh
聰明的投資者
聰明的投資者
Duran Hsieh
聊天機器人概論 Introduce to chat bot sevices
聊天機器人概論 Introduce to chat bot sevices
Duran Hsieh
[Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享
[Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享
Duran Hsieh
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練6
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練6
Duran Hsieh
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5
Duran Hsieh
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
Duran Hsieh
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練3
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練3
Duran Hsieh
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練2
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練2
Duran Hsieh
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練1(20160222)
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練1(20160222)
Duran Hsieh
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5
Duran Hsieh
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
Duran Hsieh
More from Duran Hsieh
(20)
GitHub Action Introduction
GitHub Action Introduction
Cloud Study Jam - ML API 4
Cloud Study Jam - ML API 4
Cloud Study Jam ML API 3
Cloud Study Jam ML API 3
GDG Taichung: Cloud Study Jam ML API
GDG Taichung: Cloud Study Jam ML API
GDG Taichung - Firebase Introduction 01
GDG Taichung - Firebase Introduction 01
Study4TW .NET Conf Local Event Taichung 2018 slideshow
Study4TW .NET Conf Local Event Taichung 2018 slideshow
What is .NET Chinese ver
What is .NET Chinese ver
Microsoft recommendation solution on azure
Microsoft recommendation solution on azure
Microsoft professional program introduction
Microsoft professional program introduction
聰明的投資者
聰明的投資者
聊天機器人概論 Introduce to chat bot sevices
聊天機器人概論 Introduce to chat bot sevices
[Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享
[Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練6
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練6
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練3
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練3
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練2
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練2
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練1(20160222)
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練1(20160222)
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
Android 基礎課程補充資料
1.
ANDROID 基礎開發課程 補充 Presented by Duran
Hsieh http://dog0416.blogspot.tw/
2.
OUTLINE • GPS程式補充說明 • 程式上架計數器
3.
• GPS程式補充說明
4.
4Presented By: Duran
Hsieh GPS程式補充說明 • 關於GPS感測器無法用的原因在於 • 因為安全性的關係,在android 6.0(API)使用者權限需要多 幾個步驟,因為一旦安裝app後,後續的更新並不會重新 詢問使用者是否授權。 • 導致我們GPS程式無法使用 (原因:無法取得權限) • 參考資料 • https://developer.android.com/training/permissions/requ esting.html
5.
5Presented By: Duran
Hsieh GPS程式補充說明 • 在我們使用感測器相關程式時,android studio惠要 求我們加入checkSelfPermission,確認權限。 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } 加入後,會出現ACCESS_FINE_LOCATION 與ACCESS_COARSE_LOCATION
6.
6Presented By: Duran
Hsieh GPS程式補充說明 • 但我們需要再次與使用者確認權限,若需要則會顯 示小視窗與使用者再度確認授權。 • 透過ActivityCompat.requestPermissions方法要求權 限。 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){ if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) { } else { ActivityCompat.requestPermissions( this, new String[]{Manifest.permission.INTERNET, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_PERMISSION_PHONE_STATE ); } return; } 判斷是否出現視窗 要求權限,參數分別為 Activity: 我們用this 授權清單 : 放入要求的權限, 為String[] Code: 完成授權後的事件代碼,為int, 可自己設定,我們在全域設定如下 private final int REQUEST_PERMISSION_PHONE_STATE = 1;
7.
7Presented By: Duran
Hsieh GPS程式補充說明 • 在程式碼空白處案右鍵,選擇Generate… -> Overrider
8.
8Presented By: Duran
Hsieh GPS程式補充說明 • 找到onRequestPermissionsResult,點選OK
9.
9Presented By: Duran
Hsieh GPS程式補充說明 • 我們加入一個switch,判斷 resquestCode 是否為我們要 求權限時的代碼 REQUEST_PERMISSION_PHONE_STATE public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case REQUEST_PERMISSION_PHONE_STATE: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { lms.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); Location location = lms.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if(location!=null){ getLocation(location); } } return; } } } 如果授權正確,我們則直接使用GPS感測方法要求回傳資料與取得最後位置,並把資料帶入getLocation方法
10.
10Presented By: Duran
Hsieh GPS程式補充說明 • 解決權限檢查,完成程式 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case REQUEST_PERMISSION_PHONE_STATE: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } lms.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); Location location = lms.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if(location!=null){ getLocation(location); } } return; } } } Android Studio 還是會要求我們檢查權限,我們可以加入, 但不做任何事情,因為這階段我們已經拿到權限
11.
11Presented By: Duran
Hsieh GPS程式補充說明 • 測試方法(1):模擬器 • 建議將感測器選擇為 GPS_PROVIDER lms.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); Location location = lms.getLastKnownLocation(LocationManager.GPS_PROVIDER);
12.
12Presented By: Duran
Hsieh GPS程式補充說明 • 測試方法(1):
13.
13Presented By: Duran
Hsieh GPS程式補充說明 • 測試方法(2):手機 • 建議將感測器選擇為 NETWORK_PROVIDER • 直接測試 lms.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); Location location = lms.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
14.
14Presented By: Duran
Hsieh GPS程式補充說明 • 範例程式 • https://github.com/matsurigoto/AndroidGPSExample • https://github.com/matsurigoto/ProjectDemoApplication
15.
計數器程式
16.
16Presented By: Duran
Hsieh 程式上架計數器 • 流程 • 開啟build.gradle,加入useLibrary 'org.apache.http.legacy'
17.
17Presented By: Duran
Hsieh 程式上架計數器 • 在 OnCreate 方法最後加入下列程式碼 • School 為學校名稱 • App 為程式名稱 String url = "http://www.pink-fun.com.tw/edufor4g/?school=fcu&app=test"; HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(url); try { HttpResponse response = client.execute(request); } catch (Exception e) { } 1. 目前這個API有問題,已經請陳老師在確認 2. 因為安全性疑慮,httpclient 已經被deprecated,建議使用HttpURLConnection 方法
18.
18Presented By: Duran
Hsieh 程式上架計數器 • HttpURLConnection (僅供參考) URL targetUrl = null; HttpURLConnection urlConnection = null; try { targetUrl = new URL("http://www.pink-fun.com.tw/edufor4g/?school=fcu&app=test "); urlConnection = (HttpURLConnection) targetUrl.openConnection(); InputStream in = new BufferedInputStream(urlConnection.getInputStream()); //do something } catch (Exception e) { } finally { urlConnection.disconnect(); }
19.
QUESTION & ANSWERS
20.
THANK YOU FOR WATCHING
Download now