google翻譯 與 隱私權宣告

最近遇到android app若要正式上架, 得製作隱私權宣告頁面

而這隱私權又要弄很多語系怎辦, 這時候就要在宣告中加上翻譯功能了

  • 加上java script function
<script>
function googleTranslateElementInit() {
  new google.translate.TranslateElement({
    pageLanguage: 'en',
    layout: google.translate.TranslateElement.InlineLayout.SIMPLE
  }, 'google_translate_element');
}
</script>
<script src="https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
  • 頁面插入一段div
<div id="google_translate_element"></div>
  • 若需要限定語系, 請於javascript 的 pageLanguage: 'en', 底下, 加上 語系限定

includedLanguages: 'zh-CN,zh-TW',

android kotlin 呼叫(發起)email app

好多種版本, 終於找到可以用的

  • 第一種寫法(比較適合寫慣java的老師傅)
val email_intent =  Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
    email_intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    email_intent.putExtra(Intent.EXTRA_EMAIL, arrayOf("your@email.com"))
    email_intent.putExtra(Intent.EXTRA_SUBJECT, "問題回報");
    email_intent.putExtra(Intent.EXTRA_TEXT,"");
    try {
      startActivity(Intent.createChooser(email_intent, "請選擇郵件軟體"))
    }catch (e:ActivityNotFoundException){
      Toast.makeText(myactivity, "請確認並設定好郵件收發軟體", Toast.LENGTH_LONG).show()
    }
  • 第二種寫法
val email_intent = Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:")).apply {
    addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    putExtra(Intent.EXTRA_EMAIL, arrayOf("your@email.com"))
    putExtra(Intent.EXTRA_SUBJECT, "問題回報")
    putExtra(Intent.EXTRA_TEXT, "")
  }
  try {
    startActivity(Intent.createChooser(email_intent, "請選擇郵件軟體"))
  } catch ( ex: ActivityNotFoundException) {
    Toast.makeText(myactivity, "請確認並設定好郵件收發軟體", Toast.LENGTH_LONG).show()
  }

android kotlin coroutines

import kotlinx.coroutines.*
fun main() {
   repeat(3) {
        GlobalScope.launch {
            println("Hi from ${Thread.currentThread()}")
        }
    }
}
  • 非同步
import kotlinx.coroutines.*
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

val formatter = DateTimeFormatter.ISO_LOCAL_TIME
val time = { formatter.format(LocalDateTime.now()) }

suspend fun getValue(): Double {
    println("entering getValue() at ${time()}")
    delay(3000)
    println("leaving getValue() at ${time()}")
    return Math.random()
}

fun main() {
    runBlocking {
        val num1 = getValue()
        val num2 = getValue()
        println("result of num1 + num2 is ${num1 + num2}")
    }
}

同步

import kotlinx.coroutines.*
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

val formatter = DateTimeFormatter.ISO_LOCAL_TIME
val time = { formatter.format(LocalDateTime.now()) }

suspend fun getValue(): Double {
    println("entering getValue() at ${time()}")
    delay(3000)
    println("leaving getValue() at ${time()}")
    return Math.random()
}

fun main() {
    runBlocking {
        val num1 = async { getValue() }
        val num2 = async { getValue() }
        println("result of num1 + num2 is ${num1.await() + num2.await()}")
    }
}

job

val job: Job = GlobalScope.launch(Dispatchers.Main) {
    // launch coroutine in the main thread
    for (i in 10 downTo 1) { // countdown from 10 to 1
        textView.text = "count down $i ..." // update text
        delay(1000) // wait half a second
    }
    textView.text = "Done!"
}
job.cancel()

android kotlin viewBinding 新做法

寫android總是寫不好, 之前取得view要使用findViewByI的方式, 過了一段時間回頭看, 現在又改了改成使用viewBinding 說是比較快...

  • gradle(module)
buildFeatures {
    viewBinding = true
}
  • main activity
class MainActivity : AppCompatActivity() {

    lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }
}
  • 使用方式變簡單了
// 舊版
val myButton: Button = findViewById(R.id.my_button)
myButton.text = "A button"

// 新版
val myButton: Button = binding.myButton
myButton.text = "A button"

// Best way with view binding and no extra variable
binding.myButton.text = "A button"

android取得藍芽裝置連線狀態

Java

public static boolean isConnected(BluetoothDevice device) {
    try {
        Method m = device.getClass().getMethod("isConnected", (Class[]) null);
        boolean connected = (boolean) m.invoke(device, (Object[]) null);
        return connected;
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}

Kotlin

fun isConnected(device: BluetoothDevice): Boolean {
  return try {
    val m: Method = device.javaClass.getMethod("isConnected")
    m.invoke(device) as Boolean
  } catch (e: Exception) {
    throw IllegalStateException(e)
  }
}

android 10 藍牙程式必須取得ACCESS_FINE_LOCATION權限

寫android 藍牙程式, 若需要scan 功能, 在android 10以前, 只要取得ACCESS_COARSE_LOCATION權限即可, 到了android 10則必須取得ACCESS_FINE_LOCATION權限

詳細的寫法就不多著墨了

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

工作關係需要雙開wechat

  1. 安卓使用雙開助手(請注意, 這會被警告是有害軟體, 請斟酌使用)
    https://www.multiopen.cn/
  2. windows 10上面雙開
    新增一個wechat_multi.bat檔案
    輸入(wechat目錄請依照實際例子使用)
start C:\"Program Files (x86)"\Tencent\WeChat\WeChat.exe 
start C:\"Program Files (x86)"\Tencent\WeChat\WeChat.exe

儲存後, 執行wechat.bat 即可雙開


1 2 3 4