109 lines
4.2 KiB
Kotlin
Executable File
109 lines
4.2 KiB
Kotlin
Executable File
package com.skychip.lock
|
|
|
|
import android.content.Intent
|
|
import android.net.Uri
|
|
import android.os.Bundle
|
|
import android.util.Log
|
|
import io.flutter.embedding.android.FlutterActivity
|
|
import io.flutter.plugin.common.MethodChannel
|
|
import io.flutter.embedding.engine.FlutterEngine;
|
|
import io.flutter.plugins.GeneratedPluginRegistrant
|
|
import android.bluetooth.BluetoothAdapter;
|
|
import androidx.core.content.FileProvider
|
|
import java.io.File
|
|
|
|
class MainActivity : FlutterActivity() {
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
GeneratedPluginRegistrant.registerWith(flutterEngine!!)
|
|
MethodChannel(flutterEngine?.dartExecutor!!.binaryMessenger, "starLockFlutterSend").setMethodCallHandler { call, result ->
|
|
if (call.method == "loadNativeShare") {
|
|
val map = call.arguments as Map<String, String>
|
|
val shareText = map["shareText"]
|
|
val urlToShare = map["urlToShare"]
|
|
if (urlToShare == "fileShare") {
|
|
shareFile(shareText)
|
|
} else {
|
|
shareText(shareText, "分享")
|
|
}
|
|
} else if (call.method == "sendGetBlueStatus") {
|
|
// 蓝牙是否开启
|
|
// println("收到原生的信息了 methodmethodmethod: ${call.method}")
|
|
val bluetoothAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()
|
|
var status = "0"
|
|
bluetoothAdapter?.let {
|
|
if (it.isEnabled) {
|
|
// 蓝牙已开启
|
|
status = "1"
|
|
} else {
|
|
// 蓝牙已关闭
|
|
status = "0"
|
|
}
|
|
} ?: run {
|
|
// 设备不支持蓝牙
|
|
status = "-1"
|
|
}
|
|
val flutterEngine: FlutterEngine? = this.flutterEngine // 获取你的 FlutterEngine 实例
|
|
MethodChannel(flutterEngine?.dartExecutor!!.binaryMessenger, "starLockFlutterReceive").invokeMethod("getBlueStatus", status)
|
|
} else {
|
|
result.notImplemented() // 没有实现的方法
|
|
}
|
|
}
|
|
}
|
|
|
|
fun shareText(text: String?, subject: String = "", imageUrl: String = "") {
|
|
val shareIntent = Intent().apply {
|
|
action = Intent.ACTION_SEND
|
|
putExtra(Intent.EXTRA_TEXT, text)
|
|
type = "text/plain"
|
|
// putExtra(Intent.EXTRA_SUBJECT, subject)
|
|
// putExtra(Intent.EXTRA_STREAM, Uri.parse(imageUrl))
|
|
// type = "image/*"
|
|
// addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
|
}
|
|
startActivity(Intent.createChooser(shareIntent, null))
|
|
}
|
|
|
|
fun shareFile(filePath: String?) {
|
|
if (filePath == null) {
|
|
return
|
|
}
|
|
val file = File(filePath)
|
|
if (!file.exists()) {
|
|
Log.e("File Share", "文件不存在: $filePath")
|
|
return
|
|
}
|
|
val uri: Uri = FileProvider.getUriForFile(
|
|
this,
|
|
"${packageName}.provider",
|
|
file
|
|
)
|
|
val shareIntent = Intent().apply {
|
|
action = Intent.ACTION_SEND
|
|
putExtra(Intent.EXTRA_STREAM, uri)
|
|
type = "application/octet-stream"
|
|
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
|
}
|
|
startActivity(Intent.createChooser(shareIntent, null))
|
|
}
|
|
|
|
|
|
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
|
|
GeneratedPluginRegistrant.registerWith(flutterEngine)
|
|
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "starLockFlutterSend").setMethodCallHandler { call, result ->
|
|
if (call.method == "loadNativeShare") {
|
|
val map = call.arguments as Map<String, String>
|
|
val shareText = map["shareText"]
|
|
val urlToShare = map["urlToShare"]
|
|
if (urlToShare == "fileShare") {
|
|
shareFile(shareText)
|
|
} else {
|
|
shareText(shareText, "分享")
|
|
}
|
|
} else {
|
|
result.notImplemented()
|
|
}
|
|
}
|
|
}
|
|
}
|