android intent.setaction,android intent.getAction() returning null

I've an app that scans qrcodes and nfc tags. when I scan a qrcode I create an intent, put some strings in as extras(contents of qrcode), set the setAction to com.carefreegroup.QRCODE_ACTION, which is a custom action and then call startActivity(intent).

In the receiving activity the intent.getAction() is returning null. I've set an intent filter for the receiving activity in the manifest with an action the same as the calling activity.

Why is the 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);

.

[UPDATE1]

This is the whole activity that uses the ZXing library to scan the qrcode. It then sets the captured data in the intent as extras, then explicitly calls the next activity with 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();

}

}

}

}

.

This is a snippet from the receiving activity.

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值