写入 NFC 标签后开始新的 activity
Start new activity after writing to NFC tag
我正在尝试在完成对 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>
您的 MainActivity
上有一个 IntentFilter
,因此 Intent
应该匹配 activity 的 IntentFilter。
所以你应该这样开始你的activity:
Intent nextActivity = new Intent(WriteCardActivity.this, MainActivity.class);
nextActivity.setAction(Intent.ACTION_MAIN);
nextActivity.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(nextActivity);
WriteCardActivity.this.finish();
我明白了。我决定使用 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);
}
这段代码适合我。并且不要从 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());
}
}
我正在尝试在完成对 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>
您的 MainActivity
上有一个 IntentFilter
,因此 Intent
应该匹配 activity 的 IntentFilter。
所以你应该这样开始你的activity:
Intent nextActivity = new Intent(WriteCardActivity.this, MainActivity.class);
nextActivity.setAction(Intent.ACTION_MAIN);
nextActivity.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(nextActivity);
WriteCardActivity.this.finish();
我明白了。我决定使用 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);
}
这段代码适合我。并且不要从 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());
}
}