Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.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
C# 点击另一个支持NFC的设备后未调用onNewIntent_C#_Android_Xamarin_Nfc_Ndef - Fatal编程技术网

C# 点击另一个支持NFC的设备后未调用onNewIntent

C# 点击另一个支持NFC的设备后未调用onNewIntent,c#,android,xamarin,nfc,ndef,C#,Android,Xamarin,Nfc,Ndef,我和Xamarin用c#编写代码。我试图通过NFC从一台设备到另一台设备共享数据 打开浏览器->选项->共享->应用程序4到MainActivity 我的两台设备都在运行同一个应用程序,我正在将我的设备连接到另一台设备,但什么也没发生 我认为它无法到达onNewIntent() 我错过什么了吗?我一直很困惑,一直找了一个星期 这是我的密码: 使用系统; 使用Android.App; 使用Android.Content; 使用Android.Runtime; 使用Android.Views; 使用

我和Xamarin用c#编写代码。我试图通过NFC从一台设备到另一台设备共享数据

打开浏览器->选项->共享->应用程序4到MainActivity

我的两台设备都在运行同一个应用程序,我正在将我的设备连接到另一台设备,但什么也没发生

我认为它无法到达
onNewIntent()

我错过什么了吗?我一直很困惑,一直找了一个星期

这是我的密码:

使用系统;
使用Android.App;
使用Android.Content;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
使用Android.OS;
使用Android.Nfc;
使用System.Collections.Generic;
使用系统文本;
使用Xamarin.Forms;
名称空间App4
{
[活动(Label=“App4”,MainLauncher=false,Icon=“@drawable/Icon”,LaunchMode=Android.Content.PM.LaunchMode.SingleTop)]
[IntentFilter(new[]{Intent.ActionSend,NfcAdapter.ActionNdefDiscovered},Categories=new[]{
Intent.CategoryDefault,
Intent.CategoryBrowsable
},DataMimeType=“text/plain”)]
公共课活动:活动
{
字符串共享;
悬而未决的不正当内容;
未检测到意向过滤器;
IntentFilter[]intentF;
文本视图测试电视;
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Intent Myintent=newintent(这个,GetType());
SetFlags(ActivityFlags.SingleTop);
mPendingIntent=pendingent.GetActivity(this,0,Myintent,0);
ndefDetected=新的意向过滤器(NfcAdapter.ActionNdefDiscovered);
尝试
{
ndefDetected.AddDataType(“文本/普通”);
ndefDetected.AddCategory(Intent.categorydefult);
}
捕获{};
intentF=新的IntentFilter[]{ndefDetected};
NfcAdapter NA=NfcAdapter.GetDefaultAdapter(此);
if(NA!=null&&NA.IsEnabled)
{
Toast.MakeText(这是“Nfc发现的”,ToastLength.Long).Show();
}否则
{
Toast.MakeText(这是“未找到Nfc”,ToastLength.Long).Show();
}
testTV=findviewbyd(Resource.Id.text\u视图);
share=Intent.GetStringExtra(Intent.ExtraText);
Text=share;
}
受保护的覆盖void OnPause()
{
base.OnPause();
NfcManager manager=(NfcManager)GetSystemService(NfcService);
NfcAdapter adapter=manager.DefaultAdapter;
适配器。禁用ForeGroundEndefPush(此);
适配器。DisableForegroundDispatch(此);
}
受保护的覆盖void OnResume()
{
base.OnResume();
var result2=新字节[NdefRecord.RtdText.Count];
NdefRecord.RtdUri.CopyTo(结果2,0);
NfcManager manager=(NfcManager)GetSystemService(NfcService);
NdefRecord=newndefrecord(NdefRecord.tnfabsoluteri,新字节[0],新字节[0],System.Text.Encoding.Default.GetBytes(share));
manager.DefaultAdapter.EnableForegroundNdefPush(这是新的NdefMessage(记录));
manager.DefaultAdapter.EnableForegroundDispatch(this,mpendingcontent,intentF,null);
}
受保护的覆盖无效Wintent(意图)
{
基恩温特(意图);
testTV.Text=“onNewIntent”;
}
}
}
这是我的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="App4.App4" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.NFC"></uses-permission>
    <application android:label="App4"></application>
</manifest>

是的,您缺少了一些内容:您注册了前台调度以侦听类型为“text/plain”的NDEF消息,这意味着您可能需要文本记录或MIME类型为text/plain的记录

ndefDetected = new IntentFilter(NfcAdapter.ActionNdefDiscovered);
ndefDetected.AddDataType("text/plain");
ndefDetected.AddCategory(Intent.CategoryDefault);
intentF = new IntentFilter[] { ndefDetected };
manager.DefaultAdapter.EnableForegroundDispatch(this, mPendingIntent, intentF, null);
但是,您的应用程序推送的绝对URI记录的类型名称无效(为空!)

NdefRecord record = new NdefRecord(NdefRecord.TnfAbsoluteUri,new byte[0], new byte[0], System.Text.Encoding.Default.GetBytes(share));
manager.DefaultAdapter.EnableForegroundNdefPush(this, new NdefMessage(record));
为了匹配意图过滤器,您需要推送文本记录:

byte[] text = System.Text.Encoding.UTF8.GetBytes(share);
byte[] language = System.Text.Encoding.ASCII.GetBytes("en");
byte[] payload = new byte[1 + language.Count + text.Count];
payload[0] = (byte)language.Count;
System.Array.Copy(language, 0, payload, 1, language.Count);
System.Array.Copy(text, 0, payload, 1 + language.Count, text.Count);
NdefRecord record = new NdefRecord(NdefRecord.TnfWellKnown, new List<byte>(NdefRecord.RtdText).ToArray(), new byte[0], payload);
manager.DefaultAdapter.EnableForegroundNdefPush(this, new NdefMessage(record));
byte[]text=System.text.Encoding.UTF8.GetBytes(共享);
byte[]language=System.Text.Encoding.ASCII.GetBytes(“en”);
字节[]有效载荷=新字节[1+语言.Count+文本.Count];
有效负载[0]=(字节)language.Count;
System.Array.Copy(语言,0,有效负载,1,语言,计数);
System.Array.Copy(text,0,有效负载,1+语言.Count,text.Count);
NdefRecord=新的NdefRecord(NdefRecord.TnfWellKnown,新列表(NdefRecord.RtdText.ToArray(),新字节[0],有效载荷);
manager.DefaultAdapter.EnableForegroundNdefPush(这是新的NdefMessage(记录));

是的,您缺少了一些内容:您注册了前台调度以侦听类型为“text/plain”的NDEF消息,这意味着您可能需要文本记录或MIME类型为text/plain的记录

ndefDetected = new IntentFilter(NfcAdapter.ActionNdefDiscovered);
ndefDetected.AddDataType("text/plain");
ndefDetected.AddCategory(Intent.CategoryDefault);
intentF = new IntentFilter[] { ndefDetected };
manager.DefaultAdapter.EnableForegroundDispatch(this, mPendingIntent, intentF, null);
但是,您的应用程序推送的绝对URI记录的类型名称无效(为空!)

NdefRecord record = new NdefRecord(NdefRecord.TnfAbsoluteUri,new byte[0], new byte[0], System.Text.Encoding.Default.GetBytes(share));
manager.DefaultAdapter.EnableForegroundNdefPush(this, new NdefMessage(record));
为了匹配意图过滤器,您需要推送文本记录:

byte[] text = System.Text.Encoding.UTF8.GetBytes(share);
byte[] language = System.Text.Encoding.ASCII.GetBytes("en");
byte[] payload = new byte[1 + language.Count + text.Count];
payload[0] = (byte)language.Count;
System.Array.Copy(language, 0, payload, 1, language.Count);
System.Array.Copy(text, 0, payload, 1 + language.Count, text.Count);
NdefRecord record = new NdefRecord(NdefRecord.TnfWellKnown, new List<byte>(NdefRecord.RtdText).ToArray(), new byte[0], payload);
manager.DefaultAdapter.EnableForegroundNdefPush(this, new NdefMessage(record));
byte[]text=System.text.Encoding.UTF8.GetBytes(共享);
byte[]language=System.Text.Encoding.ASCII.GetBytes(“en”);
字节[]有效载荷=新字节[1+语言.Count+文本.Count];
有效负载[0]=(字节)language.Count;
System.Array.Copy(语言,0,有效负载,1,语言,计数);
System.Array.Copy(text,0,有效负载,1+语言.Count,text.Count);
NdefRecord=新的NdefRecord(NdefRecord.TnfWellKnown,新列表(NdefRecord.RtdText.ToArray(),新字节[0],有效载荷);
manager.DefaultAdapter.EnableForegroundNdefPush(这是新的NdefMessage(记录));

Btw,如何像您的代码一样使用nedfrecard.RtdText?我现在正在使用NdefRecord.CreateTextRecord(“en”,share)。因为出现错误:“无法从'System.Collections.Generic.IList'转换为'byte[]”。我尝试将其复制到新的字节[]:result2。它会起作用吗?@wuken My update应该会起作用,尽管它可能不是最有效的解决方案。顺便说一句,
ndefreRecord.CreateTextRecord
是首选方法(而且是必需的)