【问题标题】:android intent.getAction() returning nullandroid intent.getAction() 返回 null
【发布时间】:2016-06-14 15:00:44
【问题描述】:

我有一个扫描 qrcodes 和 nfc 标签的应用程序。当我扫描二维码时,我创建了一个意图,将一些字符串作为附加内容(二维码的内容)放入,将setAction 设置为com.carefreegroup.QRCODE_ACTION,这是一个自定义操作,然后调用startActivity(intent)

在接收活动中,intent.getAction() 返回 null。我已经为清单中的接收活动设置了一个意图过滤器,其操作与调用活动相同。

为什么 getAction 为空?

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库扫描二维码的整个活动。然后它将 Intent 中捕获的数据设置为额外数据,然后使用 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();
            }
        }
    }

}

.

这是来自接收活动的 sn-p。

    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);

【问题讨论】:

  • 这段代码在什么方法中:String intentAction = intent.getAction(); if ( intentAction.equalsIgnoreCase(QRCODE_ACTION)) { ?变量intent 是如何设置的?
  • @DavidWasser 嗨,我已经更新了我的帖子以包含两个 sn-ps 代码,显示如何设置和接收意图。谢谢。
  • @DavidWasser 哦,是的,抱歉,intent.getAction() 在接收活动的 onCreate 方法中。
  • 但是接收Activity的onCreate()中的变量intent是怎么设置的呢?
  • @DavidWasser 我使用 Intent intent = this.getIntent();

标签: android android-intent


【解决方案1】:

找到了!

在这段代码中:

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 上并设置操作,但随后您使用变量QRDataIntent 调用startActivity()!它没有动作集,也没有额外内容!

试试这个:

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);

【讨论】:

  • 天哪!我整天都在工作中坚持这一点!嘿,感谢您纠正一个愚蠢的错误,我真的很感激。
  • @IgorGanapolsky 在第一个代码块中,extra 没有添加到传递给startActivity()Intent
【解决方案2】:

随便用

Intent intent = new Intent(this, ClazzName.class);
intent.setAction("action_name");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 2012-06-22
    • 2012-05-28
    • 2014-12-05
    • 2021-07-07
    • 2013-06-27
    • 1970-01-01
    相关资源
    最近更新 更多