-1

アプリケーションの操作は完了しましたが、NFC タグをタップしたときにアプリケーションが開くようにしたいです。私を助けてください

私はちょうどメインフェストに追加します

    <uses-permission android:name="android.permission.NFC" />

ここにJavaコードがあります

         public class MainActivity extends Activity {  
    private static final String TAG = "NFCReadTag";  
    private NfcAdapter mNfcAdapter;  
    private IntentFilter[] mNdefExchangeFilters;  
    private PendingIntent mNfcPendingIntent;  
 @Override  
 public void onCreate(Bundle savedInstanceState) {  
   super.onCreate(savedInstanceState);  
   setContentView(R.layout.activity_main);  
         mNfcAdapter = NfcAdapter.getDefaultAdapter(this);  
      mNfcPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,  
                   getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP  
                   | Intent.FLAG_ACTIVITY_CLEAR_TOP), 0);  
         IntentFilter smartwhere = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);  
         smartwhere.addDataScheme("http");  
         smartwhere.addDataAuthority("www.smartwhere.com", null);  
         smartwhere.addDataPath(".*", PatternMatcher.PATTERN_SIMPLE_GLOB);  
         mNdefExchangeFilters = new IntentFilter[] { smartwhere };  
 } 
 @Override  
 public boolean onCreateOptionsMenu(Menu menu) {  
   getMenuInflater().inflate(R.menu.activity_main, menu);  
   return true;  
 }  
    @Override  
    protected void onResume() {  
         super.onResume();  
         if(mNfcAdapter != null) {  
              mNfcAdapter.enableForegroundDispatch(this, mNfcPendingIntent,  
                   mNdefExchangeFilters, null);  
              if (!mNfcAdapter.isEnabled()){  
          LayoutInflater inflater = getLayoutInflater();  
             View dialoglayout = inflater.inflate( (Integer) R.layout.nfc_settings_layout,(ViewGroup) findViewById(R.id.nfc_settings_layout));  
          new AlertDialog.Builder(this).setView(dialoglayout)  
                 .setPositiveButton("Update Settings", new DialogInterface.OnClickListener() {  
                      public void onClick(DialogInterface arg0, int arg1) {  
                                    Intent setnfc = new Intent(Settings.ACTION_WIRELESS_SETTINGS);  
                                    startActivity(setnfc);  
                      }  
                 })  
              .setOnCancelListener(new DialogInterface.OnCancelListener() {  
                   public void onCancel(DialogInterface dialog) {  
                        finish(); // exit application if user cancels  
                }                      
              }).create().show();  
              }  
         } else {  
              Toast.makeText(getApplicationContext(), "Sorry, No NFC Adapter found.", Toast.LENGTH_SHORT).show();  
         }  
    }  
    @Override  
    protected void onPause() {  
         super.onPause();  
         if(mNfcAdapter != null) mNfcAdapter.disableForegroundDispatch(this);  
    }  
    @Override  
    protected void onNewIntent(Intent intent) {  
         super.onNewIntent(intent);            
         if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {  
              NdefMessage[] messages = null;  
              Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);  
              if (rawMsgs != null) {  
                   messages = new NdefMessage[rawMsgs.length];  
                   for (int i = 0; i < rawMsgs.length; i++) {  
                        messages[i] = (NdefMessage) rawMsgs[i];  
                   }  
              }  
              if(messages[0] != null) {  
                   String result="";  
                   byte[] payload = messages[0].getRecords()[0].getPayload();  
                   // this assumes that we get back am SOH followed by host/code  
                   for (int b = 1; b<payload.length; b++) { // skip SOH  
                        result += (char) payload[b];  
                   }  
                   Toast.makeText(getApplicationContext(), "Tag Contains " + result, Toast.LENGTH_SHORT).show();  
              }  
         }  
    }  
} 

問題は、このコードをアプリケーションのどこに追加するかです。

4

1 に答える 1

0

NFC の使用を開始するには、http : //www.tappednfc.com/android-nfc-developer-guide/ にアクセスしてください。

于 2013-04-11T01:24:44.233 に答える