문제

제가 하려고 했던 것은 정수를 보내고 받은 다음 그 정수를 가져와 카운트다운 타이머에 설정하는 것입니다.지금까지는 정수를 보내고 다른 장치에서 응용 프로그램을 열 수 있습니다. 그러나 장치가 활동을 로드하면 Newgame 활동이 아닌 MainActivity가 열립니다.이 시점에서 나는 코드가 똑똑하지도 않고 약간 초보자도 아니라는 점을 인정해야 합니다. 그러나 여기에 NFC 통신을 다루는 코드 추출이 있습니다. 이 추출은 Newgame.java에서 가져온 것입니다.

@Override
public NdefMessage createNdefMessage(NfcEvent event) {
    int time = bomb1.getTimer();
    String message = ( " " + time);
    NdefMessage msg = new NdefMessage(
            new NdefRecord[] { NdefRecord.createMime(
                    "application/vnd.com.Jhadwin.passthebomb.newgame" ,message.getBytes())
                    ,NdefRecord.createApplicationRecord("com.Jhadwin.passthebomb")
    });
    return msg;
}    
@Override
public void onResume() {
    super.onResume();
    // Check to see that the Activity started due to an Android Beam
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
        processIntent(getIntent());
    }
}

@Override
public void onNewIntent(Intent intent) {
    // onResume gets called after this to handle the intent
    setIntent(intent);
}

/**
 * Parses the NDEF Message from the intent and prints to the TextView
 */
 void processIntent(Intent intent) {
    Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
            NfcAdapter.EXTRA_NDEF_MESSAGES);
    // only one message sent during the beam
    NdefMessage msg = (NdefMessage) rawMsgs[0];
    // record 0 contains the MIME type, record 1 is the AAR, if present
    String newtimermsg = new String(msg.getRecords()[0].getPayload());
    timeremtextview.setText(newtimermsg);
    int newtimer = Integer.parseInt(newtimermsg);
    bomb1.setTimer(newtimer);
    bomb1.setState(true);
}

이 코드는 Google 웹사이트의 NFC 예에서 채택된 것이므로 도움을 주시면 감사하겠습니다.

AndroidManifest.xml의 애플리케이션 부분도 포함되어 있습니다.

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>            
    </activity>
    <activity android:name="com.Jhadwin.passthebomb.newgame"/>
    <activity android:name="com.Jhadwin.passthebomb.About"/>
    <activity android:name="com.Jhadwin.passthebomb.Help"/>
</application>
도움이 되었습니까?

해결책

AAR(Android 애플리케이션 레코드)을 사용하고 NDEF_DISCOVERED 앱 매니페스트에 인텐트 필터를 추가하면 Android는 앱이 실행 시 NFC 인텐트를 처리할 수 있다는 사실을 알 수 없습니다.결과적으로 매니페스트에서 다음을 선언하는 첫 번째 활동이 열립니다. MAIN 카테고리가 포함된 인텐트 필터 LAUNCHER 수신된 NDEF 메시지를 전달하지 않고.따라서 귀하의 경우에는 com.Jhadwin.passthebomb.MainActivity 사용하게 될 것이다.

Android가 NFC 인텐트(수신된 NDEF 메시지 포함)를 사용자에게 전달하도록 하려면 newgame 활동을 수행하려면 적절한 인텐트 필터를 추가해야 합니다.

<activity android:name="com.Jhadwin.passthebomb.newgame">
    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="application/vnd.com.jhadwin.passthebomb.newgame" />
    </intent-filter>
</activity>

Android의 인텐트 필터는 다음과 같습니다. 대소문자 구분.대소문자 혼합 유형 문제를 방지하기 위해 Android는 MIME 유형 및 NFC 포럼 외부 유형 이름을 자동으로 변환합니다. 소문자 (일반적으로 이러한 유형 이름은 대소문자를 구분하지 않습니다.)따라서 MIME 유형을 모두로 지정해야 합니다. 소문자 일치를 달성하기 위해.

그 외에도 몇 가지 제안 사항이 더 있습니다.

  1. Android 패키지 이름(및 일반적으로 Java 패키지 이름)은 소문자만 사용해야 합니다.수업 이름(활동 포함)은 대문자로 시작해야 합니다.

  2. 사용자 정의 애플리케이션별 MIME 유형을 생성하는 대신 NFC 포럼 외부 유형을 선호해야 합니다.

    NdefMessage msg = new NdefMessage(new NdefRecord[] {
        NdefRecord.createExternal(
            "jhadwin.com",          // your domain name
            "passthebomb.newgame",  // your type name
            message.getBytes()),    // payload
            NdefRecord.createApplicationRecord("com.jhadwin.passthebomb")
    });
    

    이 경우 다음과 같은 인텐트 필터를 사용할 수 있습니다.

    <activity android:name="com.jhadwin.passthebomb.NewGame">
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="vnd.android.nfc" android:host="ext"
                  android:pathPrefix="/jhadwin.com:passthebomb.newgame" />
        </intent-filter>
    </activity>
    
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top