java
1package com.example.ryo.test6; 2 3import android.app.PendingIntent; 4import android.content.Context; 5import android.content.Intent; 6import android.nfc.NfcAdapter; 7import android.os.Vibrator; 8import android.support.v7.app.AppCompatActivity; 9import android.os.Bundle; 10import android.widget.Toast; 11 12import java.util.Arrays; 13public class MainActivity extends AppCompatActivity { 14 15 private NfcAdapter mNfcAdapter; 16 17 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_main); 22 mNfcAdapter = NfcAdapter.getDefaultAdapter(this); 23 } 24 25 @Override 26 protected void onResume() { 27 super.onResume(); 28 29 //NFCカードがかざされた際に、現在のActivityで優先的に受け取る設定を行います。 30 Intent intent = new Intent(this, this.getClass()); 31 intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 32 PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0); 33 mNfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null); 34 } 35 @Override 36 protected void onPause() 37 super.onPause(); 38 //Activityがバックグランドに回った際は優先的に受け取る情報を停止します 39 mNfcAdapter.disableForegroundDispatch(this); 40 } 41 //NFC-UIDを、文字列に変換して表示します。 42 @Override 43 protected void onNewIntent(Intent intent){ 44 //かざしデータを受け取ったときにバイブを鳴らす。 45 ((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).vibrate(100); 46 super.onNewIntent(intent); 47 //NFC-UIDを取得します。 48 byte[] uid=intent.getByteArrayExtra(NfcAdapter.EXTRA_ID); 49 //NFC-UIDを文字列に変換して表示します。 50 Toast.makeText(this, Arrays.toString(uid),Toast.LENGTH_SHORT).show(); 51 } 52 53}
java
1package com.example.ryo.test6; 2 3import android.content.Intent; 4import android.nfc.NfcAdapter; 5import android.os.Build; 6import android.provider.Settings; 7import android.support.v7.app.AppCompatActivity; 8import android.os.Bundle; 9import android.widget.Toast; 10public class BaseActivity extends AppCompatActivity { 11protected NfcAdapter mNfcAdapter; 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.activity_base); 16 //NFCを扱うためのインスタンスを取得 17 mNfcAdapter=NfcAdapter.getDefaultAdapter(this); 18 //NFCが搭載されているかチェックする 19 if(mNfcAdapter!=null){ 20 //NFC機能が有効になっているかチェックする 21 if(!mNfcAdapter.isEnabled()){ 22 //NFC機能が無効の場合はユーザーへ通知 23 Toast.makeText(getApplicationContext(),"error_nfc_disable", Toast.LENGTH_SHORT).show(); 24 } 25 else { 26 Toast.makeText(getApplicationContext(), "error_nfc_nosupport", Toast.LENGTH_SHORT).show(); 27 } 28 } 29 if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN){ 30 startActivity(new Intent(Settings.ACTION_NFC_SETTINGS)); 31 } 32 } 33 34} 35
二つの.Javaというソースコードあるとします。
これを読み込むためにはどうしたらいいですか?
御教授お願いします。
回答1件
あなたの回答
tips
プレビュー