2012-09-12 19 views
0

qrcodes 및 nfc 태그를 검색하는 앱이 있습니다. qrcode를 스캔 할 때 일부 문자열을 엑스트라 (qrcode의 내용)로 넣고 setActioncom.carefreegroup.QRCODE_ACTION으로 설정하고 사용자 정의 작업을 수행 한 다음 startActivity(intent)을 호출합니다.android intent.getAction() null을 반환했습니다.

수신 활동에서 intent.getAction()은 null을 반환합니다. 호출 활동과 동일한 동작으로 매니페스트에서받는 활동에 대한 의도 필터를 설정했습니다.

왜 getAction이 null입니까?

public static final String CUSTOM_QRCODE_ACTION = "com.carefreegroup.QRCODE_ACTION"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.qrloggedinmain); 
     nfcscannerapplication = (NfcScannerApplication) getApplication(); 

     ////////////////////////get company options/////////////////////////////// 
     SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext()); 
     tagTouchInterval = appSharedPrefs.getString("10", null); 
     Long tagtouchinteval = new Long(tagTouchInterval); 
     companyOptionTime = 1000* 60 * tagtouchinteval ; 

     Button ScanQrCode = (Button)findViewById(R.id.buttonqrscanner); 

     ScanQrCode.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Log.e(TAG, "onclicked scan"); 

       Intent intent = new Intent(
         "com.google.zxing.client.android.SCAN"); 
       intent.putExtra("SCAN_MODE", "QR_CODE_MODE"); 

       startActivityForResult(intent, 0); 

      } 
     }); 


    }// end of onCreate 


    public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
     Log.e(TAG, "in onActivityResult"); 
     if (requestCode == 0) { 
      if (resultCode == RESULT_OK) { 
       Log.e(TAG, "result ok"); 
       /////////////////////////////// 
       tagScanTime = new DateTime(); 
       thirtySecsAgo = tagScanTime.minus(30000); 
       DateTimeFormatter df = DateTimeFormat.forPattern("dd/MMM/yy h:mmaa"); 
       String formattedScanTime = df.print(tagScanTime); 
       Log.e(TAG, "formatted tag scan time = " + formattedScanTime); 
       String formattedthirtysecsAgoTime = df.print(thirtySecsAgo); 
       Log.e(TAG, "formatted thity secs ago time = " + formattedthirtysecsAgoTime); 


       String contents = intent.getStringExtra("SCAN_RESULT"); 
       Toast.makeText(this, "scanner has found " + contents, 
         Toast.LENGTH_LONG).show(); 


       String[] splitPayload = contents.split("@"); 


       type = splitPayload[0]; 
       compId = splitPayload[1]; 
       personId = splitPayload[2]; 
       personName = splitPayload[3]; 

       Intent QRDataIntent = new Intent(this, 
         NfcscannerActivity.class); 

       intent.putExtra("type", type); 
       intent.putExtra("compId", compId); 
       intent.putExtra("personId", personId); 
       intent.putExtra("personName", personName); 
       intent.setAction(CUSTOM_QRCODE_ACTION); 
       intent.setType("text/plain"); 
       startActivity(QRDataIntent); 

.

String intentAction = intent.getAction(); 

if (intentAction.equalsIgnoreCase(QRCODE_ACTION)) { 

      Log.e(TAG, "QR Code scanned"); 
      String _type = intent.getStringExtra("type"); 
      String _compId = intent.getStringExtra("compId"); 
      String _personId = intent.getStringExtra("personId"); 
      String _personName = intent.getStringExtra("personName"); 

      Log.e(TAG, "payload = " + _type + " " + _compId + " " + _personId + " " + _personName); 

.

<activity android:name=".NfcscannerActivity" > 
      <intent-filter> 
       <action android:name="android.nfc.action.NDEF_DISCOVERED" /> 

       <category android:name="android.intent.category.DEFAULT" /> 

       <data android:mimeType="text/plain" /> 
      </intent-filter> 

      <intent-filter> 
       <action android:name="com.carefreegroup.QRCODE_ACTION" /> 

       <category android:name="android.intent.category.DEFAULT" /> 

       <data android:mimeType="text/plain" /> 


      </intent-filter> 
     </activity> 

는 [갱신 1] 이것은하시면 자동 스캔 할 ZXing 라이브러리를 사용하여 전체의 활동이다. 그런 다음 캡쳐 된 데이터를 인 텐트로 설정하여 엑스트라로 지정한 다음 startActivity()로 다음 활동을 명시 적으로 호출합니다.

package com.carefreegroup; 

import org.joda.time.DateTime; 
import org.joda.time.format.DateTimeFormat; 
import org.joda.time.format.DateTimeFormatter; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.ContentValues; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.preference.PreferenceManager; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class QrLoggedIn extends Activity{ 

    private static final String TAG = QrLoggedIn.class.getSimpleName(); 
    private NfcScannerApplication nfcscannerapplication; 
    private String tagTouchInterval; 
    private long companyOptionTime; 
    private DateTime tagScanTime; 
    private DateTime thirtySecsAgo; 
    private Boolean carerLoggedIn; 
    private String type; 
    private String personId; 
    private String personName; 
    private String compId; 
    private Cursor cursor; 
    static final String CARER_TYPE = "2"; 
    static final String CLIENT_TYPE = "1"; 
    private final String IN = "in"; 
    private final String OUT = "out"; 
    private ContentValues values; 
    public static final String CUSTOM_QRCODE_ACTION = "com.carefreegroup.QRCODE_ACTION"; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.qrloggedinmain); 
     nfcscannerapplication = (NfcScannerApplication) getApplication(); 

     ////////////////////////get company options/////////////////////////////// 
     SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext()); 
     tagTouchInterval = appSharedPrefs.getString("10", null); 
     Long tagtouchinteval = new Long(tagTouchInterval); 
     companyOptionTime = 1000* 60 * tagtouchinteval ; 

     Button ScanQrCode = (Button)findViewById(R.id.buttonqrscanner); 

     ScanQrCode.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Log.e(TAG, "onclicked scan"); 

       Intent intent = new Intent(
         "com.google.zxing.client.android.SCAN"); 
       intent.putExtra("SCAN_MODE", "QR_CODE_MODE"); 

       startActivityForResult(intent, 0); 

      } 
     }); 


    }// end of onCreate 


    public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
     Log.e(TAG, "in onActivityResult"); 
     if (requestCode == 0) { 
      if (resultCode == RESULT_OK) { 
       Log.e(TAG, "result ok"); 
       /////////////////////////////// 
       tagScanTime = new DateTime(); 
       thirtySecsAgo = tagScanTime.minus(30000); 
       DateTimeFormatter df = DateTimeFormat.forPattern("dd/MMM/yy h:mmaa"); 
       String formattedScanTime = df.print(tagScanTime); 
       Log.e(TAG, "formatted tag scan time = " + formattedScanTime); 
       String formattedthirtysecsAgoTime = df.print(thirtySecsAgo); 
       Log.e(TAG, "formatted thity secs ago time = " + formattedthirtysecsAgoTime); 


       String contents = intent.getStringExtra("SCAN_RESULT"); 
       Toast.makeText(this, "scanner has found " + contents, 
         Toast.LENGTH_LONG).show(); 


       String[] splitPayload = contents.split("@"); 


       type = splitPayload[0]; 
       compId = splitPayload[1]; 
       personId = splitPayload[2]; 
       personName = splitPayload[3]; 

       Intent QRDataIntent = new Intent(this, 
         NfcscannerActivity.class); 

       intent.putExtra("type", type); 
       intent.putExtra("compId", compId); 
       intent.putExtra("personId", personId); 
       intent.putExtra("personName", personName); 
       intent.setAction(CUSTOM_QRCODE_ACTION); 
       intent.setType("text/plain"); 
       startActivity(QRDataIntent); 



      } else if (resultCode == RESULT_CANCELED) { 
       // Handle cancel 
       Log.e(TAG, "There's a problem with the scan. Scan result failed"); 
       Toast.makeText(this, "There's a problem with the scan. Scan result failed", 
         Toast.LENGTH_LONG).show(); 
      } 
     } 
    } 

} 

.

수신 활동의 스 니펫입니다.

String intentAction = intent.getAction(); 

    Log.e(TAG, "action of intent = " + intentAction); 

    if(intentAction.equalsIgnoreCase(NFC_ACTION)){ 

    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 

    tagId = bytesToHexString(tag.getId()); 

    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) { 

     Log.e(TAG, "NFC Tag scanned"); 
     // ////////////////////////////////////////////////////////////////////// 
     // get the messages from the intent 
     Parcelable[] rawMsgs = intent 
       .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); 
     if (rawMsgs != null) { 
      msgs = new NdefMessage[rawMsgs.length]; 
      for (int i = 0; i < rawMsgs.length; i++) { 
       msgs[i] = (NdefMessage) rawMsgs[i]; 
      } 
     } 
    } else { 
     Log.e(TAG, "ndef not discovered!!!!!!"); 
    } 

    // //////////////////////////////////////////////////////////////////////////////////// 
    // process the msgs array 
    for (int i = 0; i < msgs.length; i++) { 

     NdefRecord[] records = msgs[i].getRecords(); 
     Log.e(TAG, "ndefrecord has a length of " + records.length); 

     tr = parse(records[i]); 
     payload = tr.getText(); 

     Log.e(TAG, "TextRecord.text = " + tr.getText()); 


    } 

    // /////////////////////////////////////////////////// split the payload 
    // using delimiter. assign value at position[0] to tagType 
    String[] splitPayload = payload.split("¦"); 



    tagType = splitPayload[0]; 
    tagCompany = splitPayload[1]; 
    tagPerson = splitPayload[2]; 
    tagUserName = splitPayload[3]; 

    }else if (intentAction.equalsIgnoreCase(QRCODE_ACTION)) { 

     Log.e(TAG, "QR Code scanned"); 
     String _type = intent.getStringExtra("type"); 
     String _compId = intent.getStringExtra("compId"); 
     String _personId = intent.getStringExtra("personId"); 
     String _personName = intent.getStringExtra("personName"); 

     Log.e(TAG, "payload = " + _type + " " + _compId + " " + _personId + " " + _personName); 
+0

사용'문자열 intentAction = intent.getAction를(); if (intentAction.equalsIgnoreCase (QRCODE_ACTION)) {'? 변수'intent'는 어떻게 설정됩니까? –

+0

@DavidWasser 안녕하세요, 의도를 설정하고 수신하는 방법을 보여주는 두 가지 코드 스 니펫을 포함하도록 내 게시물을 업데이트했습니다. 감사. – turtleboy

+0

@DavidWasser oh yes 죄송합니다. intent.getAction()은 수신 작업의 onCreate 메서드에 있습니다. – turtleboy

답변

6

발견! 이 코드에서

:

Intent QRDataIntent = new Intent(this, NfcscannerActivity.class); 

intent.putExtra("type", type); 
intent.putExtra("compId", compId); 
intent.putExtra("personId", personId); 
intent.putExtra("personName", personName); 
intent.setAction(CUSTOM_QRCODE_ACTION); 
intent.setType("text/plain"); 
startActivity(QRDataIntent); 

당신은 엑스트라를 넣고 변수 intent에 작업을 설정할 수 있지만 당신은 변수 QRDataIntentstartActivity()를 호출! 액션 세트 나 엑스트라가 없습니다!

대신을 시도해보십시오

Intent QRDataIntent = new Intent(this, NfcscannerActivity.class); 

QRDataIntent.putExtra("type", type); 
QRDataIntent.putExtra("compId", compId); 
QRDataIntent.putExtra("personId", personId); 
QRDataIntent.putExtra("personName", personName); 
QRDataIntent.setAction(CUSTOM_QRCODE_ACTION); 
QRDataIntent.setType("text/plain"); 
startActivity(QRDataIntent); 
+0

오 마이 갓! 나는 하루 종일이 일에 매달렸다. 어리석은 실수를 고쳐 주셔서 고마워, 정말 고마워. – turtleboy

+1

@IgorGanapolsky 첫 번째 코드 블록에서, 엑스트라는'startActivity()'에 전달 된'Intent '에 추가되지 않습니다! –

+1

우리가 그런 실수를 할 수있어서 정말 놀랍습니다 !!!!! 고맙습니다 – LoveMeow

0

를 그냥이 코드를 무엇 방법

Intent intent = new Intent(this, ClazzName.class); 
intent.setAction("action_name"); 
관련 문제