Android開發 用zxing intent讀取QRcode二維條碼

巨人的肩膀

zxing
Official ZXing (“Zebra Crossing”) project home

journeyapps/zxing-android-embedded
ZXing Android Embedded
Port of the ZXing Android application as an Android library project, for embedding in an Android application.

build.gradle

dependencies {
    //skip...
    compile 'com.journeyapps:zxing-android-embedded:3.0.3@aar'
    compile 'com.google.zxing:core:3.2.0'
}
`</pre>

### 程式碼範例

<pre>`    public void testClick( View view ){
        IntentIntegrator it = new IntentIntegrator(this);
        it.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
        it.initiateScan();
    }
`</pre>

### 程式碼範例

<pre>`    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //super.onActivityResult(requestCode, resultCode, data);
        IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (scanResult != null) {
            String contents = scanResult.getContents();
            if (contents != null) {
                ((TextView)findViewById(R.id.textView)).setText( contents );
            }
        }
    }

Android開發 用Runnable做計時器遞增範例

Runnable物件用法

private Handler mIncHandler = new Handler();
`</pre>

<pre>`mIncHandler.postDelayed(mIncRunner, 1000);
`</pre>

<pre>`    private Runnable mIncRunner = new Runnable() {
        @Override
        public void run() {
            //執行內容
            mIncHandler.postDelayed(mIncRunner, 1000);
        }
    };
`</pre>

### 範例程式碼

<pre>`public class MainActivity extends AppCompatActivity {

    private Handler mIncHandler = new Handler();
    private boolean mCounterEnable = true;
    private int mCounter;

    //skip....
`</pre>

### 範例程式碼

<pre>`    @Override
    protected void onResume() {
        super.onResume();

        //讀取共用偏好設定中的計數值
        SharedPreferences sp = this.getPreferences( this.MODE_PRIVATE );
        mCounter = sp.getInt("to.fili.testinctimer.ooxxoioi", 0);

        //啟動計時器
        mCounterEnable = true;
        mIncHandler.postDelayed(mIncRunner, 1000);

        //偵錯用
        ((TextView)findViewById(R.id.nothing)).setText( String.valueOf(mCounter) );
    }
`</pre>

### 範例程式碼

<pre>`    @Override
    protected void onPause() {
        super.onPause();

        //指定計時器在下次執行後,暫停
        mCounterEnable = false;
    }
`</pre>

### 範例程式碼

<pre>`    private Runnable mIncRunner = new Runnable() {
        @Override
        public void run() {
            //累加計數值
            mCounter = mCounter+1;

            //將計數值存回共用偏好設定中
            SharedPreferences sp = MainActivity.this.getPreferences(MainActivity.this.MODE_PRIVATE );
            SharedPreferences.Editor spe = sp.edit();
            spe.putInt("to.fili.testinctimer.ooxxoioi", mCounter);
            spe.commit();

            //若計時器仍啟動中,設定下次執行(延遲)時間
            if(mCounterEnable) {
                mIncHandler.postDelayed(mIncRunner, 1000);
            }

            //偵錯用
            ((TextView)findViewById(R.id.nothing)).setText(String.valueOf(mCounter));
        }
    };

Android開發 HTTP GET範例

巨人的肩膀

Android Asynchronous Http Client
A Callback-Based Http Client Library for Android
http://loopj.com/android-async-http/

build.gradle

dependencies {
    //...skip
    compile 'com.loopj.android:android-async-http:1.4.9'
}
`</pre>

Sync!!

### AndroidManifest.xml

<pre>`    &lt;uses-permission android:name="android.permission.INTERNET"/&gt;

    &lt;application
    //...skip
`</pre>

### 畫面配置

[![Screen Shot 2015-12-25 at 23.42.17](http://filitov.twcat.org/blog/wp-content/uploads/2015/12/Screen-Shot-2015-12-25-at-23.42.17-300x207.png)](http://filitov.twcat.org/blog/wp-content/uploads/2015/12/Screen-Shot-2015-12-25-at-23.42.17.png)

### 程式碼

<pre>`    public void btnClick( View view ){
        String url = ((EditText)findViewById(R.id.editText)).getText().toString();
        final TextView tv = (TextView)findViewById(R.id.textView);

        (new AsyncHttpClient()).get(url, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                tv.setText(new String(responseBody, StandardCharsets.UTF_8));
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });
    }

Android開發 基本「共用偏好設定」範例

參考來源:Android Developers 訓練課程 儲存索引鍵值組

建立基本畫面配置

畫面配置

對應的寫入程式碼

    public void buttonSaveClick(View view){
        String a = ((EditText)findViewById(R.id.editText)).getText().toString();
        int b = Integer.parseInt(((EditText) findViewById(R.id.editText2)).getText().toString());

        SharedPreferences sp = this.getPreferences( this.MODE_PRIVATE );
        SharedPreferences.Editor spe = sp.edit();
        spe.putString( "to.fili.testpref.ooxxoioi.sss", a );
        spe.putInt( "to.fili.testpref.ooxxoioi.ddd", b );
        spe.commit();
    }
`</pre>

### 對應的讀取程式碼

<pre>`    public void buttonLodClick(View view){
        SharedPreferences sp = this.getPreferences( this.MODE_PRIVATE );
        String a = sp.getString("to.fili.testpref.ooxxoioi.sss", "nothing");
        int b = sp.getInt("to.fili.testpref.ooxxoioi.ddd", 0);

        ((TextView)findViewById(R.id.textView)).setText( a );
        ((TextView)findViewById(R.id.textView2)).setText( String.valueOf(b) );
    }

使用 Intent 啟動 android 手機預設的相機

使用 Intent 啟動 android 手機預設的相機

    &lt;uses -feature android:name="android.hardware.camera" android:required="true"&gt;&lt;/uses&gt;
`</pre>

<pre>`    static final int SNAPSHOT = 1;

    public void testClick( View view ){
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (cameraIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(cameraIntent, SNAPSHOT);
        }
    }
`</pre>

接收 MediaStore.ACTION_IMAGE_CAPTURE Intent 回傳的結果

<pre>`    &lt;uses -permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"&gt;&lt;/uses&gt;
`</pre>

<pre>`    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK &amp;&amp; requestCode == SNAPSHOT) {
            Bundle extras = data.getExtras();
            Bitmap cameraBMP = (Bitmap) extras.get("data");
            ImageView iv;
            iv = (ImageView)findViewById( R.id.myimg );
            iv.setImageBitmap(cameraBMP);
        }
    }

Passenger + apache 用homebrew安裝錯誤訊息 Support binary PassengerAgent not found

brew 裝的 Passenger 不曉得哪次升級後掛了,檢查apache的錯誤訊息發現:

*** Passenger could not be initialized because of this error: Unable to start Phusion Passenger: 
Support binary PassengerAgent not found 
(tried: /usr/local/opt/passenger/libexec/lib/phusion_passenger/locations.ini/buildout/support-binaries/PassengerAgent 
and /var/root/.passenger/support-binaries/5.0.20/PassengerAgent). 
This probably means that your Passenger installation is broken or incomplete, 
or that your 'PassengerRoot' setting contains the wrong value. 
Please reinstall Passenger or, 
or see https://www.phusionpassenger.com/library/config/apache/reference/#passengerroot to learn how to adjust the setting.
`</pre>

檢查 `httpd.conf` 發現是依照 `brew info` 裡的指示沒錯,但就是不會動。

<pre>`LoadModule passenger_module /usr/local/opt/Passenger/libexec/buildout/apache2/mod_passenger.so
PassengerRoot /usr/local/opt/Passenger/libexec/lib/phusion_passenger/locations.ini
PassengerDefaultRuby /usr/bin/ruby 
`</pre>

依照[指示網址](https://www.phusionpassenger.com/library/config/apache/reference/#passengerroot)

<pre>`In all other cases, obtain the correct value by running the following command:
passenger-config --root
`</pre>

然後`passenger-config --root`取得了

<pre>`/usr/local/Cellar/passenger/5.0.20/libexec/src/ruby_supportlib/phusion_passenger/locations.ini
`</pre>

用它來改 `httpd.conf` 就好了...

mm...mm...

留一份備忘:
`/usr/local/Cellar/passenger/5.0.20/libexec/src/ruby_supportlib/phusion_passenger/locations.ini`

<pre>`[locations]
packaging_method=homebrew
bin_dir=/usr/local/Cellar/passenger/5.0.20/libexec/bin
support_binaries_dir=/usr/local/Cellar/passenger/5.0.20/libexec/buildout/support-binaries
lib_dir=/usr/local/Cellar/passenger/5.0.20/libexec/buildout
helper_scripts_dir=/usr/local/Cellar/passenger/5.0.20/libexec/src/helper-scripts
resources_dir=/usr/local/Cellar/passenger/5.0.20/libexec/resources
include_dir=/usr/local/Cellar/passenger/5.0.20/libexec/src
doc_dir=/usr/local/Cellar/passenger/5.0.20/libexec/doc
ruby_libdir=/usr/local/Cellar/passenger/5.0.20/libexec/src/ruby_supportlib
node_libdir=/usr/local/Cellar/passenger/5.0.20/libexec/src/nodejs_supportlib
apache2_module_path=/usr/local/Cellar/passenger/5.0.20/libexec/buildout/apache2/mod_passenger.so
ruby_extension_source_dir=/usr/local/Cellar/passenger/5.0.20/libexec/src/ruby_native_extension
nginx_module_source_dir=/usr/local/Cellar/passenger/5.0.20/libexec/src/nginx_module
build_system_dir=/usr/local/Cellar/passenger/5.0.20/libexec
download_cache_dir=/usr/local/Cellar/passenger/5.0.20/libexec/download_cache

Samsung Galaxy Note 3 hard reset

  1. 關機
  2. 按住 增加音量鍵+HOME鍵+電源鍵 不放,直至開機
  3. 進入回復選單(recovery menu)
  4. 按音量鍵上下選取 wipe data / factory reset ,按電源鍵
  5. 按音量鍵上下選取 reboot system ,按電源鍵