2016年1月14日 星期四

ListView中使用多種版面

問題

ListView中使用多種版面時,需要使用getItemViewType()以及getViewTypeCount()

getItemViewType():default 0,用來判斷需要使用哪種版面

getViewTypeCount():default 1,有幾種版面

**getItemViewType() 必須 小於 getViewTypeCount() **


若 getItemViewType() >= getViewTypeCount(),將會發生 ArrayIndexOutOfBoundsException!!!

2016年1月3日 星期日

Android GCM permission GET_ACCOUNTS

On Android devices, GCM uses an existing connection for Google services. For pre-3.0 devices, this requires users to set up their Google accounts on their mobile devices. A Google account is not a requirement on devices running Android 4.0.4 or higher.

reference:

http://stackoverflow.com/a/18444343

https://developers.google.com/cloud-messaging/android/client

2016年1月1日 星期五

android 6.0 permission check

問題:

在android 6.0(Marshmallow)開始添加了權限控管的功能,用戶可以隨心所欲的在設定中,將APP的權限進行「允許」或「拒絕」,因此在功能使用之前,都必須檢查該功能是否處於「允許」的狀態下

步驟:

  1. 在 AndroidManifest.xml中必須添加該權限的聲明
    1
    <uses-permission android:name="android.permission.CAMERA" />
    
  2. 當該裝置的版本為android 6.0後續的版本才需要進行後續權限的處理
    1
    2
    3
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)  {
    //...進行權限檢查的後續處理
    }
    
  3. 使用檢查「checkSelfPermission()」進行檢查是否擁有該權限,若權限處於「拒絕」狀態(有可能第一次安裝後),則必須先進行允許權限的步驟
    1
    2
    3
    4
    5
    6
    7
    // Check if the Camera permission is already available.
    if ( ActivityCompat.checkSelfPermission( this, Manifest.permission.CAMERA ) == PackageManager.PERMISSION_GRANTED ) {
        // already available
    } else {
        // asking whether to allow permission...
        requestCameraPermission() ;
    }
    
  4. 當權限處於「拒絕」狀態,此時會使用到「shouldShowRequestPermissionRationale()」和「requestPermissions()」
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    /**
     * Requests the Camera permission.
     * If the permission has been denied previously, a SnackBar will prompt the user to grant the
     * permission, otherwise it is requested directly.
     */
    private void requestCameraPermission() {
        Log.i( TAG, "CAMERA permission has NOT been granted. Requesting permission." ) ;
    
        /**
         * shouldShowRequestPermissionRationale 回傳的規則如下:
         * 1.true:用戶在先前權限詢問的選項中,選擇了「拒絕」,但沒有勾選「不再詢問」
         * 2.false:第一次安裝APP後進入該功能
         * 3.false:用戶在先前權限詢問選項中,選擇了「允許」
         * 4.false:用戶在先前權限詢問的選項中,選擇了「拒絕」,但勾選「不再詢問」
         * 5.false:用戶在「設定」裡頭選擇「允許或拒絕」
         * 
         * 呼叫requestPermissions後,是否會跳出詢問視窗,規則如下:
         * 1.不會:用戶在先前權限詢問選項中,選擇了「允許」
         * 2.不會:用戶在先前權限詢問的選項中,選擇了「拒絕」,但勾選「不再詢問」,在後續onRequestPermissionsResult當中一律回傳PERMISSION_DENIED
         * 3.會:第一次安裝APP後進入該功能(第一次跳出不會有「不再詢問」的選項可以勾選)
         * 4.會:用戶在先前權限詢問的選項中,選擇了「拒絕」
         * 5.會:用戶在「設定」裡頭選擇「允許或拒絕」
         * 
         * 當呼叫requestPermissions後,不管用戶選擇允許或拒絕,又或者沒有跳出詢問視窗,後續都會進入onRequestPermissionsResult執行後續動作,規則如下:
         * 1.PERMISSION_GRANTED(允許):用戶選擇「允許」選項、設定中直接選擇「允許」
         * 2.PERMISSION_DENIED(拒絕):用戶選擇「拒絕」選項、設定中直接選擇「拒絕」
         */
        if ( ActivityCompat.shouldShowRequestPermissionRationale( this, Manifest.permission.CAMERA ) ) {
            // Provide an additional rationale to the user if the permission was not granted
            // and the user would benefit from additional context for the use of the permission.
            // For example if the user has previously denied the permission.
            Log.i( TAG, "Displaying camera permission rationale to provide additional context." ) ;
            
            // 「R.string.permission_camera_rationale」的內容可以多加一些說明提醒使用戶該功能必須允許該權限才可以使用
            Snackbar.make( mLayout, R.string.permission_camera_rationale, Snackbar.LENGTH_INDEFINITE ).setAction( R.string.ok, new View.OnClickListener() {
                @Override
                public void onClick( View view ) {
                    ActivityCompat.requestPermissions( MainActivity.this, new String[] { Manifest.permission.CAMERA }, REQUEST_CAMERA ) ;
                }
            } ).show() ;
        } else {
            // Camera permission has not been granted yet. Request it directly.
            ActivityCompat.requestPermissions( this, new String[] { Manifest.permission.CAMERA }, REQUEST_CAMERA ) ;
        }
    }
    
  5. 呼叫requestPermissions後,後續會有Callback繼續處理
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    /**
     * Callback received when a permissions request has been completed.
     */
    @Override
    public void onRequestPermissionsResult( int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults ) {
        if ( requestCode == REQUEST_CAMERA ) {
            // Received permission result for camera permission.
            Log.i( TAG, "Received response for Camera permission request." ) ;
    
            // Check if the only required permission has been granted
            if ( grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED ) {
                // Camera permission has been granted, preview can be displayed
                Log.i( TAG, "CAMERA permission has now been granted. Showing preview." ) ;
                Snackbar.make( mLayout, R.string.permision_available_camera, Snackbar.LENGTH_SHORT ).show() ;
            } else {
                Log.i( TAG, "CAMERA permission was NOT granted." ) ;
                Snackbar.make( mLayout, R.string.permissions_not_granted, Snackbar.LENGTH_SHORT ).show() ;
            }
        }
        // other callback
        else {
            super.onRequestPermissionsResult( requestCode, permissions, grantResults ) ;
        }
    }
    

reference:


android 6.0 remove apache lib solved method

當使用android 6.0進行專案撰寫,6.0版本移除了apache的功能

官網說明如下:
https://developer.android.com/intl/zh-tw/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http-client

eclipse project solved method

從SDK/platforms/android-23/optional裡頭的org.apache.http.legacy.jar複製到專案的lib資料夾

reference:
http://stackoverflow.com/a/32066606

解決的Eclipse更新ADT插件時遇到的Eclipse reports rendering library more recent than ADT plug-in問題


參照此文章更新ADT
http://wangcuijing.blog.51cto.com/7233352/1320155