[英]Start new activity after writing to NFC tag
I am trying to start a new activity
after a write operation to an NFC tag has been completed. 我正在尝试在完成对NFC标签的写入操作后开始新的
activity
。 I tried using a handler but it does not work, the tag gets writen to successfully but the handler doesnt launch the activity
that it should launch after the write operation 我尝试使用处理程序,但它不起作用,标记成功写入但处理程序没有启动它应该在写入操作后启动的
activity
private void formatTag(Tag tag, NdefMessage ndefMessage)
{
NdefFormatable ndefFormatable = NdefFormatable.get(tag);
if (ndefFormatable == null)
{
Toast.makeText(this, "Tag is not NDEF formatable", Toast.LENGTH_LONG).show();
return;
}
try
{
ndefFormatable.connect();
ndefFormatable.format(ndefMessage);
ndefFormatable.close();
Toast.makeText(this, "Tag has be written successfully!", Toast.LENGTH_LONG).show();
writeHandler.sendEmptyMessage(0);
}
catch (Exception e)
{
Log.e("formatTag: ", e.getMessage());
}
}
private Handler writeHandler = new Handler() {
public void handleMessage(Message msg) {
Intent nextActivity = new Intent(WriteCardActivity.this, MainActivity.class);
startActivityForResult(nextActivity, 0);
WriteCardActivity.this.finish();
}
};
here is the manifest 这是清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.loyalty.cardplanet.membershipcard" >
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-feature
android:name="android.hardware.nfc"
android:required="false" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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=".RegisterActivity"
android:label="@string/title_activity_register" >
</activity>
<activity
android:name=".RedeemActivity"
android:label="@string/title_activity_redeem" >
</activity>
<activity
android:name=".PurchaseActivity"
android:label="@string/title_activity_purchase" >
</activity>
<activity
android:name=".ResetPinActivity"
android:label="@string/title_activity_reset_pin" >
</activity>
<activity
android:name=".WriteCardActivity"
android:label="@string/title_activity_write_card" >
</activity>
</application>
</manifest>
You have an IntentFilter
on your MainActivity
so Intent
should match activity's IntentFilter. 您的
MainActivity
上有一个IntentFilter
,因此Intent
应匹配activity的IntentFilter。
So you should start your activity like this: 所以你应该像这样开始你的活动:
Intent nextActivity = new Intent(WriteCardActivity.this, MainActivity.class);
nextActivity.setAction(Intent.ACTION_MAIN);
nextActivity.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(nextActivity);
WriteCardActivity.this.finish();
I figured it out. 我想到了。 I decided to use an
AlertDialog
instead of a Handler
so I removed the Handler
part and added this to onNewIntent
我决定使用
AlertDialog
而不是Handler
所以我删除了Handler
部分并将其添加到onNewIntent
@Override
protected void onNewIntent(Intent intent) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
NdefMessage ndefMessage = createNdefMessage(account+"");
writeNdefMessage(tag, ndefMessage);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(WriteCardActivity.this);
alertDialog.setMessage("Card Written Successfully!");
alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(WriteCardActivity.this, MainActivity.class);
Bundle b = new Bundle();
b.putBoolean("new_window", true); //sets new window
intent.putExtras(b);
startActivity(intent);
}
});
alertDialog.create();
alertDialog.show();
super.onNewIntent(intent);
}
This code is worked for me. 这段代码对我有用。 And don't call WriteCardActivity from MainActivity's onCreate, onResume or onPause.
并且不要从MainActivity的onCreate,onResume或onPause调用WriteCardActivity。 Otherwise WriteCardActivity will start again.
否则WriteCardActivity将重新开始。
private void formatTag(Tag tag, NdefMessage ndefMessage)
{
NdefFormatable ndefFormatable = NdefFormatable.get(tag);
if (ndefFormatable == null)
{
Toast.makeText(this, "Tag is not NDEF formatable", Toast.LENGTH_LONG).show();
return;
}
try
{
ndefFormatable.connect();
ndefFormatable.format(ndefMessage);
ndefFormatable.close();
Toast.makeText(this, "Tag has be written successfully!", Toast.LENGTH_LONG).show();
Intent nextActivity = new Intent(WriteCardActivity.this, MainActivity.class);
startActivityForResult(nextActivity, 0);
WriteCardActivity.this.finish();
}
catch (Exception e)
{
Log.e("formatTag: ", e.getMessage());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.