Я хочу открыть свое приложение на NF C Сигнал с соответствующей активностью.
Когда тег отформатирован как URI, он работает и меняет мою активность, но только когда приложение открыто. когда он закрыт, приложение не открывается. когда тег отформатирован как текст, он просто открывает мое приложение, но не меняет его на соответствующую активность и показывает мне, что данные нулевые.
Как я могу открыть указанную активность моего приложения в NF C Сигнал метки и какой формат мне использовать для метки? URI или текст?
мне следует изменить <data android:mimeType="text/plain" />
в моем манифесте, если я использую URI для тегов или нет?
вот мой файл манифеста:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ge.softservice.nfcwithactivties">
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!--For processing data from NFC Tag (launchMode)-->
<application
android:launchMode="singleInstance"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Menu2Activity"></activity>
<activity android:name=".Menu1Activity">
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<data android:mimeType="text/plain" />
<action android:name="android.nfc.action.TECH_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Вот мой MainActivity:
package ge.softservice.nfcwithactivties
import android.app.PendingIntent
import android.content.ContentResolver
import android.content.Intent
import android.nfc.*
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.MimeTypeFilter
import kotlinx.android.synthetic.main.activity_main.*
const val TAG = "MainActivity"
class MainActivity : AppCompatActivity() {
var nfcAdapter: NfcAdapter? = null
var nfcPendingIntent: PendingIntent? = null
private var KEY_LOG_TEXT = "logText"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val text = findViewById<TextView>(R.id.text)
val nfcSupported = findViewById<TextView>(R.id.nfc_support)
val nfcEnabled = findViewById<TextView>(R.id.nfc_enabled)
// Restore saved text if available
if (savedInstanceState != null) {
text.text = savedInstanceState.getCharSequence(KEY_LOG_TEXT)
}
// Check if NFC is supported and enabled
nfcAdapter = NfcAdapter.getDefaultAdapter(this)
if (nfcAdapter == null) {
nfcSupported.append("your phone has not NFC")
} else {
nfcSupported.append(" Your phone has NFC")
}
if (nfcAdapter?.isEnabled != null) {
nfcEnabled.append(" you have NFC turned on")
} else {
nfcEnabled.append(" your NFC is turned off")
}
nfcPendingIntent = PendingIntent.getActivity(this, 0,
Intent(this, javaClass).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0)
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
if (intent?.data.toString() == "1. " ) {
openMenu1()
text.append(intent?.data.toString())
} else if (intent?.data.toString() == "2. ") {
openMenu2()
text.append(intent?.data.toString())
} else {
Toast.makeText(this, "Ups.. Unknown tag detected", Toast.LENGTH_SHORT).show()
text.append(intent?.data.toString())
}
}
override fun onResume() {
super.onResume()
// Get all NDEF discovered intents
// Makes sure the app gets all discovered NDEF messages as long as it's in the foreground.
nfcAdapter?.enableForegroundDispatch(this, nfcPendingIntent, null, null)
// Alternative: only get specific HTTP NDEF intent
//nfcAdapter?.enableForegroundDispatch(this, nfcPendingIntent, nfcIntentFilters, null)
}
override fun onPause() {
super.onPause()
// Disable foreground dispatch, as this activity is no longer in the foreground
nfcAdapter?.disableForegroundDispatch(this)
}
fun openMenu1() {
val intent = Intent(this, Menu1Activity::class.java)
startActivity(intent)
}
fun openMenu2() {
val intent = Intent(this, Menu2Activity::class.java)
startActivity(intent)
}
}
Большое спасибо, заранее