我正在尝试在完成对NFC标签的写入操作后启动新的activity
。我尝试使用处理程序,但它不起作用,标记成功写入但处理程序没有启动它应该在写入操作后启动的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();
}
};
这是清单
<?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>
答案 0 :(得分:3)
<<$number.format("currency", $My_data)>>
上有IntentFilter
,因此MainActivity
应与活动的IntentFilter匹配。
所以你应该开始这样的活动:
Intent
答案 1 :(得分:3)
我明白了。我决定使用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);
}
答案 2 :(得分:1)
这段代码对我有用。并且不要在MainActivity的onCreate,onResume或onPause上调用WriteCardActivity。否则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());
}
}