Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Beam-必须扩展活动?_Android_Nfc_Android Beam - Fatal编程技术网

Android Beam-必须扩展活动?

Android Beam-必须扩展活动?,android,nfc,android-beam,Android,Nfc,Android Beam,我正在尝试将Android Beam整合到我的应用程序中。但是,我需要为我的应用程序扩展另一个类而不是活动。当我尝试这样做时: public class SomeClass extends AnotherActivity implements CreateNdefMessageCallback{ Android Beam根本不工作,对Beam的触摸也不显示。但是,当我这样做时: public class SomeClass extends Activity implem

我正在尝试将Android Beam整合到我的应用程序中。但是,我需要为我的应用程序扩展另一个类而不是活动。当我尝试这样做时:

public class SomeClass extends AnotherActivity implements
            CreateNdefMessageCallback{
Android Beam根本不工作,对Beam的触摸也不显示。但是,当我这样做时:

public class SomeClass extends Activity implements
            CreateNdefMessageCallback{
安卓光束工作正常。另一个活动扩展了其他一些活动,最终扩展了活动本身,所以我不知道为什么Android Beam在第一种情况下不工作。有人知道为什么会这样吗

这里是更多的原始,Android梁不工作的代码供参考

import android.app.Activity;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcAdapter.CreateNdefMessageCallback;
import android.nfc.NfcEvent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;

public class SomeClass extends AnotherActivity implements
        CreateNdefMessageCallback{
    NfcAdapter mNfcAdapter;
    TextView mInfoText;
    private static final int MESSAGE_SENT = 1;
    private NdefMessage message;
    private TextView textView;

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

        // Check for available NFC Adapter
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);

        if (mNfcAdapter == null) {
            Toast.makeText(this, "NFC is not available", Toast.LENGTH_LONG)
                    .show();
            finish();
            return;
        }

        // Register callback to set NDEF message
        mNfcAdapter.setNdefPushMessageCallback(this, this);

    }

    /**
     * Implementation for the CreateNdefMessageCallback interface
     */

    @Override
    public NdefMessage createNdefMessage(NfcEvent event) {
        String text = ("Beam me up, Android!\n\n" +
                "Beam Time: " + System.currentTimeMillis());
        NdefMessage msg = new NdefMessage(
                new NdefRecord[] {
                    NdefRecord.createMime(
                            "application/vnd.com.example.android.beam",
                            text.getBytes())
                });
        return msg;
    }


    @Override
    public void onResume() {
        super.onResume();
        // Check to see that the Activity started due to an Android Beam
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
            processIntent(getIntent());
        }
    }

    @Override
    public void onNewIntent(Intent intent) {
        // onResume gets called after this to handle the intent
        setIntent(intent);
    }

    /**
     * Parses the NDEF Message from the intent and prints to the TextView
     */
    void processIntent(Intent intent) {
        Parcelable[] rawMsgs = intent
                .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        // only one message sent during the beam
        NdefMessage msg = (NdefMessage) rawMsgs[0];
        // record 0 contains the MIME type, record 1 is the AAR, if present
        mInfoText.setText(new String(msg.getRecords()[0].getPayload()));
    }
}
我已经在DDMS LogCat中检查了两者之间的差异。这是使用其他活动的日志(NFC不工作)


我发现了问题。在另一个活动中(或者在我的例子中,C类中的另一个活动扩展了A,扩展了B,扩展了C),我有下面一行代码

getWindow().setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE);
这将禁用屏幕截图,这对于Android Beam UI(“触摸到Beam”)的功能至关重要。如果没有UI,Android Beam将无法工作。解决方案是在另一项活动中执行以下操作:

getWindow().clearFlags(LayoutParams.FLAG_SECURE);

现在一切正常。

您是否正在使用/激活另一个活动(或其任何超类)中的任何其他NFC功能?不,这是唯一一个使用NFC的类。我在问题的底部添加了另一个活动(NFC不工作)的日志。我希望有人比我更了解低级的东西。
getWindow().clearFlags(LayoutParams.FLAG_SECURE);