質問編集履歴
2
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|
1
コードです
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,3 +1,187 @@
|
|
1
|
+
```java
|
2
|
+
|
3
|
+
package com.example.ryo.test6;
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
import android.app.PendingIntent;
|
8
|
+
|
9
|
+
import android.content.Context;
|
10
|
+
|
11
|
+
import android.content.Intent;
|
12
|
+
|
13
|
+
import android.nfc.NfcAdapter;
|
14
|
+
|
15
|
+
import android.os.Vibrator;
|
16
|
+
|
17
|
+
import android.support.v7.app.AppCompatActivity;
|
18
|
+
|
19
|
+
import android.os.Bundle;
|
20
|
+
|
21
|
+
import android.widget.Toast;
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
import java.util.Arrays;
|
26
|
+
|
27
|
+
public class MainActivity extends AppCompatActivity {
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
private NfcAdapter mNfcAdapter;
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
@Override
|
38
|
+
|
39
|
+
protected void onCreate(Bundle savedInstanceState) {
|
40
|
+
|
41
|
+
super.onCreate(savedInstanceState);
|
42
|
+
|
43
|
+
setContentView(R.layout.activity_main);
|
44
|
+
|
45
|
+
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
|
46
|
+
|
47
|
+
}
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
@Override
|
52
|
+
|
53
|
+
protected void onResume() {
|
54
|
+
|
55
|
+
super.onResume();
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
//NFCカードがかざされた際に、現在のActivityで優先的に受け取る設定を行います。
|
60
|
+
|
61
|
+
Intent intent = new Intent(this, this.getClass());
|
62
|
+
|
63
|
+
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
64
|
+
|
65
|
+
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
|
66
|
+
|
67
|
+
mNfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
@Override
|
72
|
+
|
73
|
+
protected void onPause()
|
74
|
+
|
75
|
+
super.onPause();
|
76
|
+
|
77
|
+
//Activityがバックグランドに回った際は優先的に受け取る情報を停止します
|
78
|
+
|
79
|
+
mNfcAdapter.disableForegroundDispatch(this);
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
//NFC-UIDを、文字列に変換して表示します。
|
84
|
+
|
85
|
+
@Override
|
86
|
+
|
87
|
+
protected void onNewIntent(Intent intent){
|
88
|
+
|
89
|
+
//かざしデータを受け取ったときにバイブを鳴らす。
|
90
|
+
|
91
|
+
((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).vibrate(100);
|
92
|
+
|
93
|
+
super.onNewIntent(intent);
|
94
|
+
|
95
|
+
//NFC-UIDを取得します。
|
96
|
+
|
97
|
+
byte[] uid=intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
|
98
|
+
|
99
|
+
//NFC-UIDを文字列に変換して表示します。
|
100
|
+
|
101
|
+
Toast.makeText(this, Arrays.toString(uid),Toast.LENGTH_SHORT).show();
|
102
|
+
|
103
|
+
}
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
```
|
110
|
+
|
111
|
+
```java
|
112
|
+
|
113
|
+
package com.example.ryo.test6;
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
import android.content.Intent;
|
118
|
+
|
119
|
+
import android.nfc.NfcAdapter;
|
120
|
+
|
121
|
+
import android.os.Build;
|
122
|
+
|
123
|
+
import android.provider.Settings;
|
124
|
+
|
125
|
+
import android.support.v7.app.AppCompatActivity;
|
126
|
+
|
127
|
+
import android.os.Bundle;
|
128
|
+
|
129
|
+
import android.widget.Toast;
|
130
|
+
|
131
|
+
public class BaseActivity extends AppCompatActivity {
|
132
|
+
|
133
|
+
protected NfcAdapter mNfcAdapter;
|
134
|
+
|
135
|
+
@Override
|
136
|
+
|
137
|
+
protected void onCreate(Bundle savedInstanceState) {
|
138
|
+
|
139
|
+
super.onCreate(savedInstanceState);
|
140
|
+
|
141
|
+
setContentView(R.layout.activity_base);
|
142
|
+
|
143
|
+
//NFCを扱うためのインスタンスを取得
|
144
|
+
|
145
|
+
mNfcAdapter=NfcAdapter.getDefaultAdapter(this);
|
146
|
+
|
147
|
+
//NFCが搭載されているかチェックする
|
148
|
+
|
149
|
+
if(mNfcAdapter!=null){
|
150
|
+
|
151
|
+
//NFC機能が有効になっているかチェックする
|
152
|
+
|
153
|
+
if(!mNfcAdapter.isEnabled()){
|
154
|
+
|
155
|
+
//NFC機能が無効の場合はユーザーへ通知
|
156
|
+
|
157
|
+
Toast.makeText(getApplicationContext(),"error_nfc_disable", Toast.LENGTH_SHORT).show();
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
else {
|
162
|
+
|
163
|
+
Toast.makeText(getApplicationContext(), "error_nfc_nosupport", Toast.LENGTH_SHORT).show();
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN){
|
170
|
+
|
171
|
+
startActivity(new Intent(Settings.ACTION_NFC_SETTINGS));
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
```
|
184
|
+
|
1
185
|
二つの.Javaというソースコードあるとします。
|
2
186
|
|
3
187
|
これを読み込むためにはどうしたらいいですか?
|