add nfc permission,add AccessibilityService
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package com.google.android.accessibility.selecttospeak;
|
||||
|
||||
import android.accessibilityservice.AccessibilityService;
|
||||
import android.content.Intent;
|
||||
import android.util.Log;
|
||||
import android.view.accessibility.AccessibilityEvent;
|
||||
|
||||
import com.tencent.mmkv.MMKV;
|
||||
import com.ttstd.elderlyassistant.config.CommonConfig;
|
||||
|
||||
public class SelectToSpeakService extends AccessibilityService {
|
||||
private static final String TAG = "SelectToSpeakService";
|
||||
|
||||
private MMKV mMMKV = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE);
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
Log.e(TAG, "onCreate: ");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
Log.e(TAG, "onStartCommand: ");
|
||||
|
||||
return super.onStartCommand(intent, flags, startId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
Log.e(TAG, "onDestroy: ");
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAccessibilityEvent(AccessibilityEvent event) {
|
||||
Log.v(TAG, "onAccessibilityEvent: event = " + event.toString());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInterrupt() {
|
||||
Log.e(TAG, "onInterrupt: ");
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onServiceConnected() {
|
||||
super.onServiceConnected();
|
||||
Log.e(TAG, "onServiceConnected: ");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
package com.ttstd.elderlyassistant;
|
||||
package com.ttstd.elderlyassistant.activity.main;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import com.ttstd.elderlyassistant.R;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.ttstd.elderlyassistant.activity.nfc;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.nfc.NdefMessage;
|
||||
import android.nfc.NdefRecord;
|
||||
import android.nfc.NfcAdapter;
|
||||
import android.nfc.NfcManager;
|
||||
import android.os.Bundle;
|
||||
import android.os.Parcelable;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.ttstd.elderlyassistant.R;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class NfcActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_nfc);
|
||||
|
||||
NfcManager manager = (NfcManager) getSystemService(Context.NFC_SERVICE);
|
||||
NfcAdapter nfcAdapter = manager.getDefaultAdapter();
|
||||
|
||||
if (nfcAdapter == null) {
|
||||
// 设备不支持 NFC
|
||||
Toast.makeText(this, "设备不支持 NFC", Toast.LENGTH_SHORT).show();
|
||||
finish();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onNewIntent(Intent intent) {
|
||||
super.onNewIntent(intent);
|
||||
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
|
||||
Parcelable[] rawMessages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
|
||||
if (rawMessages != null) {
|
||||
NdefMessage[] messages = new NdefMessage[rawMessages.length];
|
||||
for (int i = 0; i < rawMessages.length; i++) {
|
||||
messages[i] = (NdefMessage) rawMessages[i];
|
||||
}
|
||||
// 处理 NDEF 消息
|
||||
processNdefMessages(messages);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void processNdefMessages(NdefMessage[] messages) {
|
||||
for (NdefMessage message : messages) {
|
||||
for (NdefRecord record : message.getRecords()) {
|
||||
if (record.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(record.getType(), NdefRecord.RTD_TEXT)) {
|
||||
// 处理文本记录
|
||||
String text = new String(record.getPayload());
|
||||
Toast.makeText(this, "NFC 数据: " + text, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.ttstd.elderlyassistant.config;
|
||||
|
||||
public class CommonConfig {
|
||||
public static final String MMKV_ID = "InterProcessKV";
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user