增加qq音乐写入

This commit is contained in:
2025-07-16 09:54:24 +08:00
parent 679e42cbf6
commit ea3a446768
6 changed files with 114 additions and 2 deletions

View File

@@ -53,6 +53,16 @@
<category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.DEFAULT" />
</intent-filter> </intent-filter>
</activity> </activity>
<!-- <activity-->
<!-- android:name=".activity.third_party.NfcQQMusicActivity"-->
<!-- android:launchMode="singleTask">-->
<!-- <intent-filter>-->
<!-- <action android:name="android.nfc.action.NDEF_DISCOVERED" />-->
<!-- <category android:name="android.intent.category.DEFAULT" />-->
<!-- &lt;!&ndash; 匹配 QQ 音乐 Scheme &ndash;&gt;-->
<!-- <data android:scheme="qqmusic" />-->
<!-- </intent-filter>-->
<!-- </activity>-->
<service <service
android:name="com.google.android.accessibility.selecttospeak.SelectToSpeakService" android:name="com.google.android.accessibility.selecttospeak.SelectToSpeakService"

View File

@@ -0,0 +1,26 @@
package com.ttstd.elderlyassistant.activity.third_party;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.ttstd.elderlyassistant.R;
public class NfcQQMusicActivity extends AppCompatActivity {
private static final String TAG = "NfcQQMusicActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nfc_qq_music);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.e(TAG, "onNewIntent: "+intent.getAction() );
}
}

View File

@@ -149,8 +149,27 @@ public class WriteMessageActivity extends AppCompatActivity {
Toaster.showLong("请输入电话"); Toaster.showLong("请输入电话");
return; return;
} }
WriteMessageUtils.writePhoneToTag(mTag, editable.toString()); String phone = editable.toString();
if (!phone.startsWith("+86"))
phone = "+86" + phone;
WriteMessageUtils.writePhoneToTag(mTag, phone);
} }
}
public void writeQqMusic(View view) {
if (mTag == null) {
Toaster.showLong("NFC卡片未连接");
return;
}
WriteMessageUtils.writeUrlToTag(mTag, "qqmusic://qq.com/media/playSonglist?p={\"song\":[{\"songmid\":\"003Hr2m138zmPR\"}]}");
}
public void writeQqMusicForIos(View view) {
if (mTag == null) {
Toaster.showLong("NFC卡片未连接");
return;
}
WriteMessageUtils.writeUrlToTagIos(mTag, "qqmusic://qq.com/media/playSonglist?p={\"song\":[{\"songmid\":\"003aAPj81VWrbL\"}]}","com.tencent.qqmusic");
}
}
} }

View File

@@ -6,10 +6,15 @@ import android.nfc.NdefMessage;
import android.nfc.NdefRecord; import android.nfc.NdefRecord;
import android.nfc.Tag; import android.nfc.Tag;
import android.nfc.tech.Ndef; import android.nfc.tech.Ndef;
import android.util.Log;
import com.hjq.toast.Toaster;
import java.io.IOException; import java.io.IOException;
public class WriteMessageUtils { public class WriteMessageUtils {
private static final String TAG = "WriteMessageUtils";
public static void writePhoneToTag(Tag tag, String phoneNumber) { public static void writePhoneToTag(Tag tag, String phoneNumber) {
// 构建tel:协议的URI // 构建tel:协议的URI
Uri uri = Uri.parse("tel:" + phoneNumber); Uri uri = Uri.parse("tel:" + phoneNumber);
@@ -22,6 +27,7 @@ public class WriteMessageUtils {
ndef.connect(); ndef.connect();
ndef.writeNdefMessage(message); // 写入NDEF消息 ndef.writeNdefMessage(message); // 写入NDEF消息
ndef.close(); ndef.close();
Toaster.show("写入联系人成功");
} catch (IOException | FormatException e) { } catch (IOException | FormatException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -38,8 +44,31 @@ public class WriteMessageUtils {
ndef.connect(); ndef.connect();
ndef.writeNdefMessage(message); ndef.writeNdefMessage(message);
ndef.close(); ndef.close();
Toaster.show("写入Url成功");
} catch (IOException | FormatException e) { } catch (IOException | FormatException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public static void writeUrlToTagIos(Tag tag, String url, String pkg) {
// 1. 创建HTTP RecordHTTPS链接
Uri uri = Uri.parse(url);
NdefRecord httpRecord = NdefRecord.createUri(uri);
// 2. 创建Android应用包名Record
NdefRecord androidRecord = NdefRecord.createApplicationRecord(pkg);
// 3. 按顺序组合记录
NdefMessage message = new NdefMessage(new NdefRecord[]{httpRecord, androidRecord});
// 4. 写入NFC标签
try {
Ndef ndef = Ndef.get(tag); // 获取标签对象
ndef.connect();
ndef.writeNdefMessage(message);
} catch (Exception e) {
Log.e(TAG, "writeUrlToTagIos: " + e.getMessage());
}
}
} }

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.third_party.NfcQQMusicActivity">
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@@ -50,6 +50,25 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"/> android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{click::writeQqMusic}"
android:text="QQ音乐"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_phone" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{click::writeQqMusicForIos}"
android:text="QQ音乐兼容iPhone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_phone" />
</LinearLayout> </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>