###実現したいこと、問題
NFCをつかい、id,name,lat,lngの四つの値を渡したいのですが、NFCでの通信をしたときにprocessIntentというメソッドを呼ぶようにしているつもりが呼ばれません。
processIntentで4つの値をうけとってintentするものを書いています。
onCreateではNfcがオンになっているかを確認とこの4つの値を設定しています。
デバッガーで確認したところonCreateできちんと4つの値は取得できています。
調べてみたのですがなぜprocessIntentが呼ばれないのかがわかりません・・・
エラーはでていません。
###該当のソースコード
public class ShareActivity extends Activity implements NfcAdapter.CreateNdefMessageCallback { Cursor cursor; private String name; NfcAdapter nfcAdapter; String setid; String setlat; String setlng; String setcoment; private static String TAG = ShareActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_share); //Check for available NFC Adapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); if (nfcAdapter == null) { Log.d(TAG, "in onCreate, NFC is not available"); Toast.makeText(this, "NFC is not available", Toast.LENGTH_LONG).show(); finish(); return; } if (!nfcAdapter.isEnabled()) { Log.d(TAG, "in onCreate, NFC is not enabled"); Toast.makeText(this, "NFC is not enabled", Toast.LENGTH_LONG).show(); finish(); return; } if (!nfcAdapter.isNdefPushEnabled()) { Log.d(TAG, "in onCreate, NFC is not push enabled"); Toast.makeText(this, "NFC is not push enabled", Toast.LENGTH_SHORT).show(); } else { //Register callback Log.d(TAG, "in onCreate, register callback"); nfcAdapter.setNdefPushMessageCallback(this, this); } NewSQLiteOpenHelper helper = new NewSQLiteOpenHelper(this); SQLiteDatabase database = helper.getReadableDatabase(); cursor = database.query(NewSQLiteOpenHelper.MY_TABLE, null, null , null,null,null,null ); if(cursor != null) { cursor.moveToFirst(); if (cursor.getCount() > 0){ Log.v("count",String.valueOf( cursor.getCount())); Log.v("data", "data"); //nameをセット name = cursor.getString(cursor.getColumnIndex("name")); }else { Log.v("Nodata", "nodata"); } } Intent intent = getIntent(); setid = intent.getStringExtra("Arraysetid"); setlat = intent.getStringExtra("Arraysetlat"); setlng = intent.getStringExtra("Arraysetlng"); setcoment = intent.getStringExtra( name + " : " +"Arraysetcoment"); } public void onResume() { super.onResume(); Log.d(TAG, "in onResume"); //TODO what is this?? if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(getIntent().getAction())) { //to receive the beam processIntent(getIntent()); } } public void onNewIntent(Intent intent) { setIntent(intent); } @Override public NdefMessage createNdefMessage(NfcEvent event) { Log.d(TAG, "in createNdefMessage"); NdefMessage msg = new NdefMessage(new NdefRecord[]{ NdefRecord.createMime("application/com.example.sios",setid.getBytes()), NdefRecord.createMime("application/com.example.sios",setlat.getBytes()), NdefRecord.createMime("application/com.example.sios",setlng.getBytes()), NdefRecord.createMime("application/com.example.sios",setcoment.getBytes()) }); return msg; } private void processIntent(Intent intent) { Log.d(TAG, "in processIntent"); //TODO what is this?? Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); NdefMessage msg = (NdefMessage) rawMsgs[0]; NdefRecord[] records = msg.getRecords(); String nfcid = new String(records[0].getPayload()); String nfclat = new String(records[1].getPayload()); String nfclng = new String(records[2].getPayload()); String nfccoment = new String(records[3].getPayload()); Intent warp = new Intent(this, MainActivity.class); warp.putExtra("warp","warp"); warp.putExtra("nfcid",nfcid); warp.putExtra("nfclat",nfclat); warp.putExtra("nfclng",nfclng); warp.putExtra("nfccoment",nfccoment); startActivity(warp); } }
マニフェストファイルにはこう書いてあります
<activity android:name=".ShareActivity"> <intent-filter> <action android:name="android.nfc.action.NDEF_DISCOVERED" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="application/com.example.sios" /> </intent-filter> </activity>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。