SlideShare a Scribd company logo
1 of 20
ANDROID
基礎開發課程 補充
Presented by
Duran Hsieh
http://dog0416.blogspot.tw/
OUTLINE
• GPS程式補充說明
• 程式上架計數器
• GPS程式補充說明
4Presented By: Duran Hsieh
GPS程式補充說明
• 關於GPS感測器無法用的原因在於
• 因為安全性的關係,在android 6.0(API)使用者權限需要多
幾個步驟,因為一旦安裝app後,後續的更新並不會重新
詢問使用者是否授權。
• 導致我們GPS程式無法使用 (原因:無法取得權限)
• 參考資料
• https://developer.android.com/training/permissions/requ
esting.html
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
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;
7Presented By: Duran Hsieh
GPS程式補充說明
• 在程式碼空白處案右鍵,選擇Generate… -> Overrider
8Presented By: Duran Hsieh
GPS程式補充說明
• 找到onRequestPermissionsResult,點選OK
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方法
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 還是會要求我們檢查權限,我們可以加入,
但不做任何事情,因為這階段我們已經拿到權限
11Presented By: Duran Hsieh
GPS程式補充說明
• 測試方法(1):模擬器
• 建議將感測器選擇為 GPS_PROVIDER
lms.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
Location location =
lms.getLastKnownLocation(LocationManager.GPS_PROVIDER);
12Presented By: Duran Hsieh
GPS程式補充說明
• 測試方法(1):
13Presented By: Duran Hsieh
GPS程式補充說明
• 測試方法(2):手機
• 建議將感測器選擇為 NETWORK_PROVIDER
• 直接測試
lms.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
Location location =
lms.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
14Presented By: Duran Hsieh
GPS程式補充說明
• 範例程式
• https://github.com/matsurigoto/AndroidGPSExample
• https://github.com/matsurigoto/ProjectDemoApplication
計數器程式
16Presented By: Duran Hsieh
程式上架計數器
• 流程
• 開啟build.gradle,加入useLibrary 'org.apache.http.legacy'
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 方法
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();
}
QUESTION & ANSWERS
THANK YOU FOR
WATCHING

More Related Content

More from Duran Hsieh

GitHub Action Introduction
GitHub Action IntroductionGitHub Action Introduction
GitHub Action IntroductionDuran Hsieh
 
Cloud Study Jam - ML API 4
Cloud Study Jam -  ML API 4Cloud Study Jam -  ML API 4
Cloud Study Jam - ML API 4Duran Hsieh
 
Cloud Study Jam ML API 3
Cloud Study Jam ML API 3Cloud Study Jam ML API 3
Cloud Study Jam ML API 3Duran Hsieh
 
GDG Taichung: Cloud Study Jam ML API
GDG Taichung: Cloud Study Jam ML APIGDG Taichung: Cloud Study Jam ML API
GDG Taichung: Cloud Study Jam ML APIDuran Hsieh
 
GDG Taichung - Firebase Introduction 01
GDG Taichung - Firebase Introduction 01GDG Taichung - Firebase Introduction 01
GDG Taichung - Firebase Introduction 01Duran Hsieh
 
Study4TW .NET Conf Local Event Taichung 2018 slideshow
Study4TW .NET Conf Local Event Taichung 2018 slideshowStudy4TW .NET Conf Local Event Taichung 2018 slideshow
Study4TW .NET Conf Local Event Taichung 2018 slideshowDuran Hsieh
 
What is .NET Chinese ver
What is .NET Chinese verWhat is .NET Chinese ver
What is .NET Chinese verDuran Hsieh
 
Microsoft recommendation solution on azure
Microsoft recommendation solution on azureMicrosoft recommendation solution on azure
Microsoft recommendation solution on azureDuran Hsieh
 
Microsoft professional program introduction
Microsoft professional program introductionMicrosoft professional program introduction
Microsoft professional program introductionDuran Hsieh
 
聰明的投資者
聰明的投資者聰明的投資者
聰明的投資者Duran Hsieh
 
聊天機器人概論 Introduce to chat bot sevices
聊天機器人概論 Introduce to chat bot sevices聊天機器人概論 Introduce to chat bot sevices
聊天機器人概論 Introduce to chat bot sevicesDuran Hsieh
 
[Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享
[Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享[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 教育訓練62016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練6
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練6Duran Hsieh
 
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練52016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5Duran Hsieh
 
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練42016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4Duran Hsieh
 
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練3
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練32016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練3
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練3Duran Hsieh
 
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練2
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練22016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練2
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練2Duran Hsieh
 
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練1(20160222)
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練1(20160222)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 教育訓練52015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5Duran Hsieh
 
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練42015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4Duran Hsieh
 

More from Duran Hsieh (20)

GitHub Action Introduction
GitHub Action IntroductionGitHub Action Introduction
GitHub Action Introduction
 
Cloud Study Jam - ML API 4
Cloud Study Jam -  ML API 4Cloud Study Jam -  ML API 4
Cloud Study Jam - ML API 4
 
Cloud Study Jam ML API 3
Cloud Study Jam ML API 3Cloud Study Jam ML API 3
Cloud Study Jam ML API 3
 
GDG Taichung: Cloud Study Jam ML API
GDG Taichung: Cloud Study Jam ML APIGDG Taichung: Cloud Study Jam ML API
GDG Taichung: Cloud Study Jam ML API
 
GDG Taichung - Firebase Introduction 01
GDG Taichung - Firebase Introduction 01GDG Taichung - Firebase Introduction 01
GDG Taichung - Firebase Introduction 01
 
Study4TW .NET Conf Local Event Taichung 2018 slideshow
Study4TW .NET Conf Local Event Taichung 2018 slideshowStudy4TW .NET Conf Local Event Taichung 2018 slideshow
Study4TW .NET Conf Local Event Taichung 2018 slideshow
 
What is .NET Chinese ver
What is .NET Chinese verWhat is .NET Chinese ver
What is .NET Chinese ver
 
Microsoft recommendation solution on azure
Microsoft recommendation solution on azureMicrosoft recommendation solution on azure
Microsoft recommendation solution on azure
 
Microsoft professional program introduction
Microsoft professional program introductionMicrosoft professional program introduction
Microsoft professional program introduction
 
聰明的投資者
聰明的投資者聰明的投資者
聰明的投資者
 
聊天機器人概論 Introduce to chat bot sevices
聊天機器人概論 Introduce to chat bot sevices聊天機器人概論 Introduce to chat bot sevices
聊天機器人概論 Introduce to chat bot sevices
 
[Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享
[Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享[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 教育訓練62016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練6
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練6
 
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練52016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5
 
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練42016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
 
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練3
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練32016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練3
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練3
 
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練2
2016年逢甲大學資訊系:ASP.NET MVC 4 教育訓練22016年逢甲大學資訊系: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)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 教育訓練52015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練5
 
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練42015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
 

Android 基礎課程補充資料

  • 1. ANDROID 基礎開發課程 補充 Presented by Duran Hsieh http://dog0416.blogspot.tw/
  • 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
  • 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(); }