設定存取 SD 卡的權限 SD <AndroidManifest.xml> SD <uses-permission android:name="android.permission.read_external_ STORAGE" /> <uses-permission android:name="android.permission.write_external_ STORAGE" /> 13.1.4 範例 :SD 卡音訊播放器 SD ALLOW SD 13-6
多媒體應用 13 新增專案並完成版面配置 <ExMusic01> <activity_main.xml> ImageView ID imgplay ImageView ID imgstop ImageView ID imgfront TextView ID txtsong TextView ID txtmusic ImageView ID imgend ImageView ID imgnext ImageView ID imgpause ListView ID lstmusic <AndroidManifest.xml> 加入存取 SD 卡權限 <AndroidManifest.xml> SD <AndroidManifest.xml> <ExMusic01/app/manifests/AndroidManifest.xml> <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ehappy.exmusic01" > <uses-permission android:name="android.permission.read_external_storage" /> <uses-permission android:name="android.permission.write_external_storage" /> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundicon="@mipmap/ic_launcher_round" android:supportsrtl="true" android:theme="@style/apptheme" > 13-7
加入執行的程式碼 1. oncreate() <ExMusic01/app/java/ /MainActivity.java> 24 private MediaPlayer mediaplayer; 25 private final String SONGPATH=Environment. getexternalstoragedirectory().getpath() + "/"; 26 // 27 String[] songname=new String[] {"greensleeves", "mario", "songbird", "summersong", "tradewinds"}; 28 // 29 String[] songfile=new String[] {"greensleeves.mp3", "mario.mp3", "songbird.mp3", "summersong.mp3", "tradewinds.mp3"}; 30 private int clistitem=0; // 31 private Boolean falgpause=false; // 32 private ArrayAdapter<String> adasong; 33 @Override 34 protected void oncreate(bundle savedinstancestate) { 52 lstmusic.setonitemclicklistener(lstlistener); 53 mediaplayer=new MediaPlayer(); 54 adasong=new ArrayAdapter<String>(this, android.r.layout.simple_list_item_1, songname); 55 requeststoragepermission(); 24 MediaPlayer 53 MediaPlayer 25 SONGPATH SD SD 27 songname 54 68 ListView 29 songfile SONGPATH 55 2. 12.4.4 12.4.4 13-8
多媒體應用 13 <ExLogin/app/java/ /MainActivity.java> 59 private void requeststoragepermission() { 60 if(build.version.sdk_int >= 23) { //Androis 6.0 61 // 62 int haspermission = checkselfpermission(manifest. permission.read_external_storage); 63 if(haspermission!= PackageManager. PERMISSION_GRANTED) { // 64 requestpermissions(new String[]{Manifest. permission.read_external_storage}, 1); 65 return; 66 } 67 } 68 lstmusic.setadapter(adasong); // 69 } 70 71 //requestpermissions 72 @Override 73 public void onrequestpermissionsresult(int requestcode, String[] permissions, int[] grantresults) { 74 if (requestcode == 1) { 75 if (grantresults[0] == PackageManager. PERMISSION_GRANTED) { // 76 lstmusic.setadapter(adasong); 77 } else { 78 Toast.makeText(this, " ", Toast.LENGTH_SHORT).show(); 79 finish(); // 80 } 81 } else { 82 super.onrequestpermissionsresult(requestcode, permissions, grantresults); 83 } 84 } 3. ListView <ExMusic01/app/java/ /MainActivity.java> 86 private ImageView.OnClickListener listener=new ImageView.OnClickListener() { 87 @Override 88 public void onclick(view v) { 13-9
15.3.5 範例 : 設定 Google Maps 的顯示樣式 視點位罝 Google Maps 17 101 樣式 景點 101 15-22
Google Maps 應用程式 15 新增 Google Maps 專案 Google Maps Activity <ExGoogleMap02> 更改應用程式名稱 Title Map ExGoogleMap02 完成 <activity_maps.xml> 版面配置 <activity_maps.xml> TextView Spinner SupportMapFragment map <ExGoogleMap02/app/res/layout/activity_maps.xml> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.ehappy.exgooglemap02.mapsactivity" android:orientation="vertical"> 15-23
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="10dp" android:orientation="horizontal"> <TextView android:id="@+id/txtshow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" " android:textcolor="#0000ff" android:textsize="12sp" /> <Spinner android:id="@+id/spngeopoint" android:layout_width="160dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/txtmaptype" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" " android:textcolor="#0000ff" android:textsize="12sp" /> <Spinner android:id="@+id/spnmaptype" android:layout_width="160dp" android:layout_height="wrap_content" /> </LinearLayout> <fragment android:id="@+id/map" android:name="com.google.android.gms.maps.supportmapfragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="15dp" /> </LinearLayout> 15-24