2015年10月15日 星期四

如何在 App 畫面上顯示 Google map

Reference:

https://developers.google.com/maps/documentation/android-api/map#indoor_maps
https://developer.android.com/guide/topics/location/index.html

Illustration:

按照“取得 Google map的 API key”一文,取得了 Google Map API key之後,之後在下列檔案新增紅字的部分,就可以在你的 app裡面顯示 Google map了

1. AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="your_package_name" >

<permission android:name="your_package_name.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
    <uses-permission android:name="your_package_name.permission.MAPS_RECEIVE"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <!--
 The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but are recommended.
    -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
        android:allowBackup="true"
        ...... >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="6587000" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="@string/google_maps_key" />

        .......
    </application>
    <!--
              如果使用OpenGL ES version 2來顯示
              避免 Google Play Store 認為我們的 App 不支援 OpenGL ES version 2
    -->
    <uses-feature android:glEsVersion="0x00020000" android:required="true" />
</manifest>

2. res/values/google_maps_key.xml

<resources>
......
    <string name="google_maps_key" translatable="false" templateMergeStrategy="preserve">
        YOUR_KEY_HERE
    </string>
</resources>

取得 Google map的 API key

Reference:

http://developer.android.com/tools/publishing/app-signing.html
https://developers.google.com/maps/documentation/android-api/signup
http://wazai.net/1916/%E7%B0%BD%E7%BD%B2%E4%BD%A0%E7%9A%84android%E6%87%89%E7%94%A8%E7%A8%8B%E5%BC%8F-keystore

Step:

1. 打開命令提示字元視窗,輸入下列的指令:
keytool -genkey -v -keystore yourkeyname.keystore -alias yourkeyname -keyalg RSA -keysize 2048 -validity 10000

Note:
如果這邊出現「keytool: command not found」,請把$JAVA_HOME\bin加入環境變數$PATH中,或是直接到$JAVA_HOME\bin\目錄下執行 keytool,例如 $JAVA_HOME\bin是 C:\Program Files\Java\jdk1.7.0_02\bin,就先cd C:\Program Files\Java\jdk1.7.0_02\bin之後開始輸入上面的指令。
將上述指令 -keystore yourkeyname.keystore以及 -alias yourkeyname中的 yourkeyname改成自己要的名字,其他東西照著貼上就好,-validity官方說明文件建議設定10000以上的值
Ex: 
keytool -genkey -v -keystore jasonrelease.keystore -alias jasonrelease -keyalg RSA -keysize 2048 -validity 10000
2. 將產生的 yourkeyname.keystore移到 $JAVA_HOME\bin\目錄下
3. 在命令提示字元視窗輸入下列指令
keytool -list -v -keystore jasonrelease.keystore

android debug key:
keytool -list -v -keystore debug.keystore
password:android
4. 將 SHA1編碼複製起來,等一下在 Google API Console網頁會使用到
5. 利用 Google搜尋 Google API Console,找到Google API註冊網站
https://console.developers.google.com/
6.  API -> Google Maps Android API -> 啟用
7. 憑證 -> 新增憑證 -> API金鑰 -> 「Android」金鑰
8. 新增「套件名稱和指紋」
9. 輸入開發的 app的套件名稱和剛剛複製起來的 SHA1編碼,點擊建立按鈕後完成

2015年10月14日 星期三

複製 Studio project到新的位置

將 Studio project直接複製一份到新的位置,開啟新位置的 project時,有出現異常的情況,按照下面的步驟操作 Studio,就可以排除異常了

Tools -> Android -> Sync Project with Gradle Files

Hide the statusbar and navigation bar permanently

Reference:

http://stackoverflow.com/questions/21724420/how-to-hide-navigation-bar-permanently-in-android-activity

Code snippet:

private int currentApiVersion;

@Override
@SuppressLint("NewApi")
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    currentApiVersion = android.os.Build.VERSION.SDK_INT;

    final int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
        | View.SYSTEM_UI_FLAG_FULLSCREEN
        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

    // This work only for android 4.4+
    if(currentApiVersion >= Build.VERSION_CODES.KITKAT)
    {

        getWindow().getDecorView().setSystemUiVisibility(flags);

        // Code below is to handle presses of Volume up or Volume down.
        // Without this, after pressing volume buttons, the navigation bar will
        // show up and won't hide
        final View decorView = getWindow().getDecorView();
        decorView
            .setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
            {

                @Override
                public void onSystemUiVisibilityChange(int visibility)
                {
                    if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
                    {
                        decorView.setSystemUiVisibility(flags);
                    }
                }
            });
    }

}


@SuppressLint("NewApi")
@Override
public void onWindowFocusChanged(boolean hasFocus)
{
    super.onWindowFocusChanged(hasFocus);
    if(currentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
    {
        getWindow().getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
    }
}

2015年10月13日 星期二

ActionBar 顯示返回鍵

Reference:

http://yuzhou2.blogspot.tw/2015/08/android-actionbar-homeasup.html
http://developer.android.com/guide/topics/ui/actionbar.html
https://developer.android.com/training/implementing-navigation/ancestral.html

Code snippet:

AndroidManifest.xml
<application ...>
    <activity android:name=".MainActivity" ...>
    </activity>
    <activity android:name=".SecondActivity"
              android:parentActivityName=".MainActivity" ...>
    </activity>
</application>


SecondActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
    ActionBar actionBar = getSupportActionBar();
    // change the icon of key
    actionBar.setHomeAsUpIndicator(R.drawable.icon);
    actionBar.setDisplayHomeAsUpEnabled(true);
}

Note:

This is appropriate when the parent activity may be different depending on how the user arrived at the current screen.

SecondActivity.java
@Override
public Intent getParentActivityIntent() {
    finish();
    return null;
}