2014-10-16 4 views
0
내가 그래서 난 내 매니페스트에이 추가 난 내 응용 프로그램 시작하려는 내 휴대 전화에 가까운 NFC 태그를 넣어

:안드로이드 NFC 리디렉션 의도

<intent-filter> 
    <action android:name="android.nfc.action.TECH_DISCOVERED" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
</intent-filter> 
<meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/nfc_tech_filter" /> 

이 잘 작동합니다.

사용자가 나는 사용자가 내 측정 활동의 내 에서 onCreate이 점을 추가, 그래서 그가 태그에서 데이터를 전송할 수 있습니다 전에 먼저 로그인 할 로그인하지 않은 경우

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    this.sessionManager = new SessionManager(this); 
    this.sessionManager.login(); 

    this.setContentView(R.layout.activity_measure); 
    this.nfcAdapter = NfcAdapter.getDefaultAdapter(this); 
    this.pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 

    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); 

    this.filters = new IntentFilter[] { ndef, }; 
    this.techLists = new String[][] { new String[] { android.nfc.tech.NfcV.class.getName() } }; 

    this.textViewYear = (TextView) findViewById(R.id.measure_textview_year); 
    this.textViewMonthDay = (TextView) findViewById(R.id.measure_textview_month_day); 
    this.textViewTime = (TextView) findViewById(R.id.measure_textview_time); 
    this.textViewGlucose = (TextView) findViewById(R.id.measure_textview_glucose); 

    new StartReadTask().execute(); 
} 

그러나 사용자가 자신이 있기 때문에이 구현의 내 메인 메뉴로 리디렉션됩니다 로그인 후 : 여기에 언급 된 'MainActivity는'내 메인 메뉴

/** 
* 
* @param v 
*/ 
@Override 
public void onClick(View view) { 
    String username = this.textEditUsername.getText().toString(); 
    String password = this.textEditPassword.getText().toString(); 

    if(username.trim().length() > 0 && password.trim().length() > 0){ 
     if(username.equals("test") && password.equals("test")){ 
      this.sessionManager.createLoginSession("Test-Name", "Test-Email"); 

      Intent intent = new Intent(this.getApplicationContext(), MainActivity.class); 

      this.startActivity(intent); 
      this.finish(); 
     } else { 
      this.dialogAlertManager.showAlertDialog(LoginActivity.this, "Login failed..", "Username or Password is incorrect", false); 
     }    
    } else { 
     this.dialogAlertManager.showAlertDialog(LoginActivity.this, "Login failed..", "Please enter username and password", false); 
    } 
} 

입니다

. 하지만 내 활동에 로 리디렉션하려는 활동을 원하지만 어떻게 할 수 있는지 모르겠습니다. "정상"로그인 후, nfc 의도없이, 메인 메뉴 으로 리디렉션하는 앱이 필요하지만 nfc 인 텐트로 측정 활동으로 리디렉션하려고합니다.

또한 사용자가 이미 로그인 한 경우 태그의 데이터가 즉시 으로 전송되기를 원합니다. 이제 내가 데이터를 전송하려는 경우 휴대 전화에 태그를 가까이 두어 측정 활동을 시작하고 멀리두고 다시 데이터를 전송하는 것보다 데이터를 전송하십시오.

어떻게 둘 다 할 수 있습니까?

답변

0

동일한 코드에서 nfc 읽기, UI 필드 액세스, 활동 흐름 제어 등 여러 가지 작업을 수행하고 있습니다. (예, 안드로이드 샘플 코드는 종종 나쁜 예를 보여줍니다.)

사용자가 로그인하기 시작했을 때 무슨 일이 일어나고 있는지와 같은 특수한 경우를 처리해야합니다. 이 권리를 얻을 수있는 유일한 기회는 코드에서 서로 다른 책임을 분리하는 것입니다.

한 가지 확실한 점은 태그를 읽을 때 정보를 저장해야한다는 것입니다. 따라서 사용자는 다르게 반응하고 사용자가 로그인 할 때 마지막으로 표시 할 수 있습니다. 다른 활동이 관련된 경우 싱글 톤과 같은 일종의 중앙 엔터티에 데이터를 저장해야 할 수 있습니다. (대안은 활동에서 다른 활동으로 이동할 때 항상 정보를 의도와 함께 전송하는 것입니다.)

NFC 읽기의 경우 Google에서 유용한 "NFC-HUNT"앱이 있습니다. 은 몇 가지 특별한 기능을 가지고 있습니다 :

  1. IT는 NFC 태그를 읽고 UI없이 특별 활동을 사용합니다. 이렇게하면 NFC 판독 값이 캡슐화되고 나머지 앱에는 NFC 코드가 전혀 없습니다. 이 Activty (NFCShimActivity)는 정보를 준비하고 일반 UI를 시작하기위한 의도를 만듭니다.

  2. 일반적으로 액티비티가 표시되고이 액티비티의 새로운 인 텐트가 도착하면 (NFC 태그가 읽혀지기 때문에) 액티비티의 다른 인스턴스가 열립니다. 그냥 네가 원하지 않는 것.

솔루션 매니페스트에서 android:launchMode="singleTask"을 설정하고, 활동에이 방법을 구현하는 것입니다 : 나는 구조를 읽는 NFC 코드에 대한 블로그 항목을 작성했습니다

public void onNewIntent(Intent intent) {...} 

을, 어쩌면 당신이 만드는 데 도움이 이해하기 쉽다.

http://meier-online.com/en/2014/09/nfc-hunt-analysed/