2016-07-16 60 views
1

後onNewIntent不叫。我試圖通過NFC從一臺設備到另一臺設備共享數據。攻到另一個我在C#與Xamarin編碼支持NFC的裝置

打開瀏覽器 - >選項 - >分享 - > APP4到MainActivity

我的兩個設備都運行相同的應用程序,我在我的竊聽設備到另一臺設備,但沒有任何反應。

我認爲它不能達到onNewIntent()

我錯過了什麼嗎?我一直困惑,並繼續尋找一個星期。

這裏是我的代碼:

using System; 
using Android.App; 
using Android.Content; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Android.OS; 
using Android.Nfc; 
using System.Collections.Generic; 
using System.Text; 
using Xamarin.Forms; 

namespace App4 
{ 
    [Activity(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")] 
    public class MainActivity : Activity 
    { 
     string share; 
     PendingIntent mPendingIntent; 
     IntentFilter ndefDetected; 
     IntentFilter[] intentF; 
     TextView testTV; 
     protected override void OnCreate(Bundle bundle) 
     { 

      base.OnCreate(bundle); 
      SetContentView(Resource.Layout.Main); 

      Intent Myintent = new Intent(this, GetType()); 
      Myintent.SetFlags(ActivityFlags.SingleTop); 
      mPendingIntent = PendingIntent.GetActivity(this, 0, Myintent, 0); 
      ndefDetected = new IntentFilter(NfcAdapter.ActionNdefDiscovered); 
      try 
      { 
       ndefDetected.AddDataType("text/plain"); 
       ndefDetected.AddCategory(Intent.CategoryDefault); 
      } 
      catch { }; 

      intentF = new IntentFilter[] { ndefDetected }; 
      NfcAdapter NA = NfcAdapter.GetDefaultAdapter(this); 

      if (NA!=null && NA.IsEnabled) 
      { 
       Toast.MakeText(this, "Nfc Found", ToastLength.Long).Show(); 
      }else 
      { 
       Toast.MakeText(this, "Nfc Not Found", ToastLength.Long).Show(); 
      } 
      testTV = FindViewById<TextView>(Resource.Id.text_view); 
      share = Intent.GetStringExtra(Intent.ExtraText); 
      testTV.Text = share; 
    } 

     protected override void OnPause() 
     { 
      base.OnPause(); 

      NfcManager manager = (NfcManager)GetSystemService(NfcService); 
      NfcAdapter adapter = manager.DefaultAdapter; 
      adapter.DisableForegroundNdefPush(this); 
      adapter.DisableForegroundDispatch(this); 
     } 

     protected override void OnResume() 
     { 
      base.OnResume(); 


      var result2 = new byte[NdefRecord.RtdText.Count]; 
      NdefRecord.RtdUri.CopyTo(result2, 0); 

      NfcManager manager = (NfcManager)GetSystemService(NfcService); 
      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)); 
      manager.DefaultAdapter.EnableForegroundDispatch(this, mPendingIntent, intentF, null); 
     } 

     protected override void OnNewIntent(Intent intent) 
     { 

      base.OnNewIntent(intent); 
      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> 

回答

0

是的,你缺少的東西:您註冊的前景調度監聽的NDEF消息鍵入「text/plain」,這意味着您要麼使用Text記錄,要麼使用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)); 
+0

我會稍後再嘗試。非常感謝您的幫助! – wuken

+0

It works !!!!!非常感謝你!你是我的救世主! – wuken

+0

順便說一句,如何使用NedfRecord.RtdText像你的代碼?我現在正在使用NdefRecord.CreateTextRecord(「en」,分享)。因爲有一個錯誤:「無法從'System.Collections.Generic.IList''to'byte []'」 轉換。我嘗試將它複製到一個新的字節[]:result2。它會起作用嗎? – wuken