Android上一些APP外的实用功能,如通知、快捷方式

255 阅读1分钟

@TOC

基于jetpack compose 框架的使用代码

一、通知

参见 官方文档

0. 配置权限

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

1. 测试发送通知代码

悬浮提醒

Button(onClick = { 
    val channelId = "notify1"
    val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    val channel = NotificationChannel(channelId, "通知消息1", NotificationManager.IMPORTANCE_HIGH).apply {
        description = "this is a test channel"
        setShowBadge(true)
        enableVibration(true)
        setAllowBubbles(true)
        enableLights(true)
    }
    manager.createNotificationChannel(channel)

    val pendingIntent = PendingIntent.getActivity(context, 1002,
        Intent(this@MainActivity, WakeActivity::class.java),
        PendingIntent.FLAG_MUTABLE)

    val noticeId = Random.nextInt()

    val notification = NotificationCompat.Builder(context, channelId)
        .setSmallIcon(R.drawable.future)
        .setContentTitle("未来")
        .setContentText("未来已经到了")
        .setStyle(NotificationCompat.BigPictureStyle()
            .bigPicture(BitmapFactory.decodeResource(resources, R.drawable.future))
        )
        .setBadgeIconType(NotificationCompat.BADGE_ICON_LARGE)
        .setNumber(10)
        .setAllowSystemGeneratedContextualActions(true)
        .setBubbleMetadata(NotificationCompat.BubbleMetadata.fromPlatform(BubbleMetadata.Builder("a bubble 1").build()))
        .setContentIntent(pendingIntent)
        .build()
    val timer = object:CountDownTimer(3000, 3000){
        override fun onTick(millisUntilFinished: Long) {}
        override fun onFinish() {
            manager.notify(noticeId, notification)
        }
    }
    timer.start()
}){
   Text(text = "测试")
}

2. 打开通知设置界面代码

Button(onClick = { 
	val intentSetting = Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
	intentSetting.putExtra(Settings.EXTRA_APP_PACKAGE, packageName)
	//                            intentSetting.putExtra(Settings.EXTRA_CHANNEL_ID, "chat4")
	startActivity(intentSetting)
}) {
    Text(text = "测试")
}

二、快捷方式

参见 官方文档

1. 测试添加动态快捷方式代码

实测只能添加前四个快捷方式

快捷方式

Button(onClick = { 
        ShortcutManagerCompat.addDynamicShortcuts(context, listOf(
          ShortcutInfoCompat.Builder(context, "id1")
            .setShortLabel("Website 1")
             .setLongLabel("Open the website ${Random.nextInt()}")
             .setIcon(IconCompat.createWithResource(context, R.drawable.future))
             .setIntent(Intent(Intent.ACTION_VIEW,
                 Uri.parse("https://www.mysite.example.com/")))
             .build(),
          ShortcutInfoCompat.Builder(context, "id2")
             .setShortLabel("Website 7")
             .setLongLabel("Open the website ${Random.nextInt()}")
             .setIcon(IconCompat.createWithResource(context, R.drawable.future))
             .setIntent(Intent(Intent.ACTION_VIEW,
                 Uri.parse("https://www.mysite.com/")))
             .build(),
         ShortcutInfoCompat.Builder(context, "id4")
             .setShortLabel("Website 6")
             .setLongLabel("Open the website ${Random.nextInt()}")
             .setIcon(IconCompat.createWithResource(context, R.drawable.future))
             .setIntent(Intent(Intent.ACTION_VIEW,
                 Uri.parse("https://www.mysite.com/")))
             .build(),
         ShortcutInfoCompat.Builder(context, "id3")
             .setShortLabel("Website 5")
             .setLongLabel("Open the website ${Random.nextInt()}")
             .setIcon(IconCompat.createWithResource(context, R.drawable.future))
             .setIntent(Intent(Intent.ACTION_VIEW,
                 Uri.parse("https://www.mysite.com/")))
             .build(),
         ShortcutInfoCompat.Builder(context, "id5")
             .setShortLabel("Website 4")
             .setLongLabel("Open the website ${Random.nextInt()}")
             .setIcon(IconCompat.createWithResource(context, R.drawable.future))
             .setIntent(Intent(Intent.ACTION_VIEW,
                 Uri.parse("https://www.mysite.com/")))
             .build(),
         ShortcutInfoCompat.Builder(context, "id6")
             .setShortLabel("Website 3")
             .setLongLabel("Open the website ${Random.nextInt()}")
             .setIcon(IconCompat.createWithResource(context, R.drawable.future))
             .setIntent(Intent(Intent.ACTION_VIEW,
                 Uri.parse("https://www.mysite.com/")))
             .build()
      ))                    
}) {
    Text(text = "测试")
}

三、开发者图块

参见 官方文档

在这里插入图片描述

四、桌面小部件

参见 官方文档

在这里插入图片描述