如何在屏幕固定的情况下通过NFC发送字符串?

12

我正尝试在我的应用程序使用屏幕固定时通过NFC发送一个String。但是它无法工作:传输不发生;但是如果我禁用屏幕固定,String的传输就可以工作。

我可以暂时禁用屏幕固定,然后执行传输,但这是一种安全风险。

我该如何解决这个问题?


如果您想尝试所有代码,这里是全部代码。您只需要手动通过应用程序设置启用屏幕固定(因此代码较少,仍可产生相同的结果)。 我使用两个运行Android 5.0的Nexus 7测试了这个问题。

您不必阅读所有这些代码,如果您知道我可以添加到我的清单中的内容以允许在屏幕固定期间使用NFC,则可能可以解决此问题。


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidnfc"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.NFC"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.androidnfc.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>
        </activity>
    </application>
</manifest>

MainActivity.java

public class MainActivity extends Activity implements CreateNdefMessageCallback, OnNdefPushCompleteCallback
{ 
   TextView textInfo;
   EditText textOut;  
   NfcAdapter nfcAdapter;

   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      textInfo = (TextView)findViewById(R.id.info);
      textOut = (EditText)findViewById(R.id.textout);

      nfcAdapter = NfcAdapter.getDefaultAdapter(this);
      nfcAdapter.setNdefPushMessageCallback(this, this);
      nfcAdapter.setOnNdefPushCompleteCallback(this, this);
   }

   @Override
   protected void onResume() 
   {
      super.onResume();
      Intent intent = getIntent();
      String action = intent.getAction();

      if(action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED))
      {
         Parcelable[] parcelables = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
         NdefMessage inNdefMessage = (NdefMessage)parcelables[0];
         NdefRecord[] inNdefRecords = inNdefMessage.getRecords();
         NdefRecord NdefRecord_0 = inNdefRecords[0];
         String inMsg = new String(NdefRecord_0.getPayload());
         textInfo.setText(inMsg);
      }
   }

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

   @Override
   public void onNdefPushComplete(NfcEvent event) {
      final String eventString = "onNdefPushComplete\n" + event.toString();
      runOnUiThread(new Runnable() {

        @Override
        public void run() {
          Toast.makeText(getApplicationContext(), eventString, Toast.LENGTH_LONG).show();
        }
       });
   }

   @Override
   public NdefMessage createNdefMessage(NfcEvent event) {
      String stringOut = textOut.getText().toString();
      byte[] bytesOut = stringOut.getBytes();

      NdefRecord ndefRecordOut = new NdefRecord(
         NdefRecord.TNF_MIME_MEDIA, 
         "text/plain".getBytes(),
                new byte[] {}, 
                bytesOut);

      NdefMessage ndefMessageout = new NdefMessage(ndefRecordOut);
      return ndefMessageout;
   }
}

布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.androidnfc.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/textout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

你有试过这个例子吗?你使用哪个Android版本和设备进行测试?我刚刚在运行Android 5.0.1(LRX22C)的Nexus 4上测试了一下,即使屏幕被固定,你的例子也能够正常工作。 - Michael Roland
我在运行 Android 5 的两个 Nexus 7 上尝试了它。你的设备必须开启屏幕固定功能才会遇到问题。 - J_Strauton
在Android 5.0.1上,您的示例仍然可以工作:在接收设备上,屏幕会自动取消固定并启动活动的新实例。这可能是由于手动固定而不是通过程序代码进行固定引起的吗? - Michael Roland
我已经尝试手动固定和代码固定进行测试 -- 两者都不起作用。 - J_Strauton
1个回答

4
我不确定这是否真正回答了你的问题,但我想总结一下我的发现:
在尝试您的示例时,Android 5.0.1(Nexus 4上的LRX22C)在接收到NDEF消息后会自动取消屏幕固定并重新启动活动。因此,似乎注册在清单中的意图过滤器优先于(手动?)屏幕固定。
我知道这与问题描述中所描述的经验不完全相符。我想知道这是由于不同的Android版本(5.0 vs. 5.0.1)还是由于使用手动屏幕固定而不是编程屏幕固定导致的...
在我的测试设置中,我能够通过使用前台调度系统注册活动来接收其NDEF消息来解决问题(即防止活动被自动取消固定):
在您的onResume()方法中创建一个待处理意图,并启用前台调度:
PendingIntent pi = this.createPendingResult(0x00A, new Intent(), 0);
nfcAdapter.enableForegroundDispatch(this, pi, null, null);

然后您将收到意图(intent),通过活动(activity)的onActivityResult()方法,通知您已发现的标签(tag):

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case 0x00A:
            onNewIntent(data);
        break;
    }
}

此外,您还需要在onPause()方法中禁用前台调度:
nfcAdapter.disableForegroundDispatch(this);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接