我正在研究nfc,而我正在练习创建一个新的应用程序,而不会泄漏活动。
这是我的活动:
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.IsoDep;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.ref.WeakReference;
public class HomeActivity extends AppCompatActivity {
private NfcAdapter mNfcAdapter;
private static WeakReference<HomeActivity> me = null;
public static final int NFC_DATA_READ = 1;
private static Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (null != me.get()) return;
switch (msg.what) {
case NFC_DATA_READ:
TextView textView = (TextView) me.get().findViewById(R.id.dataText);
textView.setText((String) msg.obj);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter == null) {
// Stop here, we definitely need NFC
Toast.makeText(this, "This device doesn't support NFC.", Toast.LENGTH_LONG).show();
finish();
return;
}
handleIntent(getIntent());
}
@Override
protected void onPause() {
super.onPause();
mNfcAdapter.disableForegroundDispatch(this);
}
@Override
protected void onResume() {
super.onResume();
setupForegroundDispatch(this,mNfcAdapter);
}
@Override
protected void onNewIntent(Intent intent) {
/**
* This method gets called, when a new Intent gets associated with the current activity instance.
* Instead of creating a new activity, onNewIntent will be called. For more information have a look
* at the documentation.
*
* In our case this method gets called, when the user attaches a Tag to the device.
*/
handleIntent(intent);
}
@Override
protected void onStart() {
super.onStart();
me = new WeakReference<HomeActivity>(this);
}
@Override
protected void onStop() {
me.clear();
super.onStop();
}
private void handleIntent(Intent intent){
String action = intent.getAction();
Toast.makeText(getApplicationContext(),"letto qualcosa",Toast.LENGTH_SHORT).show();
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
// In case we would still use the Tech Discovered Intent
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
String[] techList = tag.getTechList();
String searchedTech = IsoDep.class.getName();
for (String tech : techList) {
if (searchedTech.equals(tech)) {
new ReaderTask(handler).execute(tag);
break;
}
}
}
}
/**
* @param activity The corresponding {@link Activity} requesting the foreground dispatch.
* @param adapter The {@link NfcAdapter} used for the foreground dispatch.
*/
private void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);
IntentFilter[] filters = new IntentFilter[1];
String[][] techList = new String[][]{};
// Notice that this is the same filter as in our manifest.
filters[0] = new IntentFilter();
filters[0].addAction(NfcAdapter.ACTION_TECH_DISCOVERED);
// Setup a tech list for all NfcF tags
techList = new String[][] { new String[] { IsoDep.class.getName() } };
adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
}
}
ReaderTask理论上是一个长期运行的任务。 为了不泄漏活动上下文,我创建了一个新类,但是我需要使用此任务的结果更新视图。为此,我在HomeActivity中开发了一个静态内部类Handler,并在该Handler中开发了ReaderTask sendMessage。 处理程序使用对活动的弱引用来检查活动是否有效。
但我认为有一种最好的办法可以做到这一切,或者至少是最后一步。 我认为我的代码不干净:/