android - 从 Google 的示例代码中获取 createMime is undefined for type beamActivity 错误

标签 android nfc ndef nfc-p2p android-beam

我正在尝试学习 NFC 并通读 Google 文档。我复制了他们的代码来创建一个 Activty,将数据从一台 Android 设备传输到另一台。

我无法让它在线运行 new NdefRecord[] { createMime(... 我从 Google 的示例代码中收到“未定义 beamActivity 类型”错误消息。

完整代码如下

public class BeamActivity extends Activity
                          implements CreateNdefMessageCallback, 
                                     OnNdefPushCompleteCallback {

    NfcAdapter mNfcAdapter;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // see if there is a NFC interface
        mNfcAdapter=NfcAdapter.getDefaultAdapter(this);
        if (mNfcAdapter==null)
            Toast.makeText(this,"no adapter",
                    Toast.LENGTH_SHORT).show();

        mNfcAdapter.setNdefPushMessageCallback(this,this);  
            mNfcAdapter.setOnNdefPushCompleteCallback(this,this);
    }

    public void onNdefPushComplete( NfcEvent arg0) {
        mHandler.obtainMessage(1).sendToTarget();
    }

    private final Handler mHandler = new Handler() {
        @Override
        public void  handleMessage(Message msg) {
            switch(msg.what) {
            case 1:
                Toast.makeText( getApplicationContext(),"Mesg Sent",
                        Toast.LENGTH_SHORT).show();
                 break;
            } // end switch
        } // end handle mesg
    }; // end new

    // creat call back code
    // gets called to star a beam, return messag to beam  
    @Override
    public NdefMessage createNdefMessage(NfcEvent event) {
        String text = ("Beam me up, Android!\n\n" +
                "Beam Time: " + System.currentTimeMillis());

        // error message hear
        NdefMessage msg = new NdefMessage(
                new NdefRecord[] { createMime(
                        "application/vnd.com.example.android.beam", text.getBytes())
         /**
          * The Android Application Record (AAR) is commented out. When a device
          * receives a push with an AAR in it, the application specified in the AAR
          * is guaranteed to run. The AAR overrides the tag dispatch system.
          * You can add it back in to guarantee that this
          * activity starts when receiving a beamed message. For now, this code
          * uses the tag dispatch system.
          */
          //,NdefRecord.createApplicationRecord("com.example.android.beam")
        });
        return msg;
    }


    @Override
    public void onNewIntent(Intent intent)
    {
        setIntent(intent);
    }

    @Override
    public void onResume()
    {
        super.onRestart();

        if (mNfcAdapter.ACTION_NDEF_DISCOVERED.equals(  getIntent().getAction()))
        {
            processIntent( getIntent() );
        }
    }

    void processIntent( Intent intet)
    {
        Parcelable[] rawMsgs= intet.getParcelableArrayExtra( mNfcAdapter.EXTRA_NDEF_MESSAGES );

        NdefMessage msg = (  NdefMessage) rawMsgs[0];
        String s= new String(  msg.getRecords()[0].getPayload());

        Toast.makeText( getApplicationContext(), s,
                Toast.LENGTH_SHORT).show();
    }

}

最佳答案

createMime(...)NdefRecord 的静态方法:

NdefMessage msg = new NdefMessage(
    new NdefRecord[] {
        NdefRecord.createMime(
                "application/vnd.com.example.android.beam",
                text.getBytes())
    });

关于android - 从 Google 的示例代码中获取 createMime is undefined for type beamActivity 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19235545/

相关文章:

android - 将我的 NFC 标签绑定(bind)到我的应用程序

c# - 如何使用兼容Android的C#读写NDEF格式的NFC标签?

java - 在 Android Studio 中找不到 MainActivity.java

android - 在特定任务中使用 NFC 启动 Activity

android - 在 Android 上通过 NFC 进行 BLE 配对

android - 在连接的任何(空)NFC 上启动应用程序

android - 无法验证用作 NDEF 标签的 MIFARE Classic 标签

android - 如何使 tablayout 文本大小相等?

Android - 彼此相邻的按钮

java - E/蓝牙适配器 : Bluetooth binder is null