[源码] Android 上的一些快捷方式,如通知、快捷方式等

基于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 = "测试")
}

三、开发者图块

参见 官方文档

在这里插入图片描述

四、桌面小部件

参见 官方文档

在这里插入图片描述

相关推荐

  1. android使用通知快捷方式

    2024-03-31 18:44:01       30 阅读
  2. EXCEL快捷方式

    2024-03-31 18:44:01       25 阅读
  3. Linux创建IntelliJ IDEA快捷方式

    2024-03-31 18:44:01       50 阅读
  4. Windows10去除电脑桌面图标快捷方式

    2024-03-31 18:44:01       35 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-31 18:44:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-31 18:44:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-31 18:44:01       82 阅读
  4. Python语言-面向对象

    2024-03-31 18:44:01       91 阅读

热门阅读

  1. python项目练习——7.网站访问日志分析器

    2024-03-31 18:44:01       35 阅读
  2. 数据可视化之多表显示

    2024-03-31 18:44:01       39 阅读
  3. 软件之禅(十一) 消息

    2024-03-31 18:44:01       35 阅读
  4. vim的缓冲区管理技能

    2024-03-31 18:44:01       32 阅读
  5. ChatGPT:学术界必备的写作利器

    2024-03-31 18:44:01       34 阅读
  6. C 语言练习分享

    2024-03-31 18:44:01       36 阅读
  7. leetcode 64.最小路径和

    2024-03-31 18:44:01       35 阅读
  8. vue组件的select怎么赋值?

    2024-03-31 18:44:01       42 阅读
  9. Leetcode-2952-需要添加的硬币的最小数量-c++

    2024-03-31 18:44:01       36 阅读
  10. C++多线程:unique_lock源码分析与使用详解(六)

    2024-03-31 18:44:01       36 阅读
  11. 为什么Redis设计成单线程

    2024-03-31 18:44:01       37 阅读