NFC開啓檢查IC芯片

package com.ckt.mmitest.testCases.nfc;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.provider.Settings;
import java.io.FileReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class MmiNfcTest extends Activity {
    public Resources res;
    public TextView board;
    public NfcAdapter nfcAdapter;
    public PendingIntent pendingIntent;
    public String nfc_hint;
    boolean Nfc_result = false;
    private final static String nfc_enable = "svc nfc enable";
    private final static String nfc_disable = "svc nfc disable";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout_nfc_test);

        View decor = getWindow().getDecorView();
        board = (TextView) decor.findViewById(R.id.board);
        res = this.getResources();

    nfcAdapter = NfcAdapter.getDefaultAdapter(this);

    pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
        getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("csh", "onPause");
        //Open_Nfc(nfc_disable);
        nfcAdapter.disable();
        if (nfcAdapter != null)
            nfcAdapter.disableForegroundDispatch(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("csh", "onResume");

        if (nfcAdapter != null){
            nfcAdapter.enableForegroundDispatch(this, pendingIntent,
            null, null);
        }
        if (nfcAdapter == null){
            nfc_hint = res.getString(R.string.msg_nonfc);
            Log.d("csh", "onResume: msg_nonfc");
        }
        else if (nfcAdapter.isEnabled()){
            nfc_hint = res.getString(R.string.msg_nocard);
            Log.d("csh", "onResume: msg_nocard");
        }
        else{
            nfc_hint = "Please wait 2 seconds, the NFC function is being turned on!";
            //Open_Nfc(nfc_enable);
            nfcAdapter.enable();
            Log.d("csh", "onResume: msg_nfcdisabled");
        }
        if(Nfc_result == true){
            Log.d("csh", "onResume Nfc_result = true");
            nfc_hint = "PASS";
            Nfc_result = false;
        }

        board.setText(nfc_hint);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        final Parcelable p = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        if(p != null){
            board.setText("PASS");
            Nfc_result = true;
            Log.d("csh", "onNewIntent: PASS");
        }
    }

    protected void Open_Nfc(String i) {
        Log.d("csh", "Open_Nfc"+i);
        try {
            Process p = Runtime.getRuntime().exec(i);
                String data = null;
                BufferedReader ie = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        } catch (IOException e) {
                    e.printStackTrace();
            }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章