flutter调用用kotlin编写自动生成检查是否已拥有桌面快捷方式和生成APP桌面快捷方式代码按照以上要求请提供完整代码以及导入包
// Kotlin代码 package com.example.shortcutexample
import android.content.Context import android.content.Intent import android.content.pm.PackageManager import android.content.pm.ResolveInfo import android.graphics.Bitmap import android.graphics.Canvas import android.graphics.drawable.BitmapDrawable import android.graphics.drawable.Drawable import androidx.core.content.ContextCompat
class ShortcutHelper {
companion object {
private const val SHORTCUT_ID = "com.example.shortcutexample.SHORTCUT_ID"
fun hasShortcut(context: Context): Boolean {
val contentResolver = context.contentResolver
val uri = getUri(context)
var hasShortcut = false
val cursor = contentResolver.query(uri, null, null, null, null)
cursor?.let {
hasShortcut = cursor.count > 0
cursor.close()
}
return hasShortcut
}
fun addShortcut(context: Context) {
val intent = Intent(context, MainActivity::class.java)
intent.action = Intent.ACTION_MAIN
val shortcutIntent = Intent()
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent)
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "My Shortcut")
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, getShortcutIcon(context))
shortcutIntent.putExtra("duplicate", false)
shortcutIntent.action = "com.android.launcher.action.INSTALL_SHORTCUT"
context.sendBroadcast(shortcutIntent)
}
private fun getUri(context: Context) =
if (android.os.Build.VERSION.SDK_INT < 8) {
val uri = "content://com.android.launcher.settings/favorites?notify=true"
Uri.parse(uri)
} else {
val uri = "content://com.android.launcher2.settings/favorites?notify=true"
Uri.parse(uri)
}
private fun getShortcutIcon(context: Context): Bitmap {
val drawable: Drawable? = ContextCompat.getDrawable(context, R.mipmap.ic_launcher)
drawable?.let {
val bitmap = Bitmap.createBitmap(
drawable.intrinsicWidth,
drawable.intrinsicHeight,
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
drawable.setBounds(0, 0, canvas.width, canvas.height)
drawable.draw(canvas)
return bitmap
}
throw IllegalArgumentException("Unable to get shortcut icon")
}
}
}
// Flutter调用代码
import 'package:flutter/material.dart'; import 'package:flutter/services.dart';
const platform = const MethodChannel('com.example.shortcutexample/shortcut');
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Shortcut Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children:
if (snapshot.hasData) {
return Text(
'Has Shortcut: ${snapshot.data}',
style: TextStyle(fontSize: 18),
);
}
return Text(
'Unable to get shortcut status',
style: TextStyle(fontSize: 18),
);
},
),
],
),
),
),
);
}
Future
void _addShortcut() async { try { await platform.invokeMethod('addShortcut'); } on PlatformException catch (e) { print(e); } } }
// 导入包 import android.net.Uri;
// Flutter调用代码 import 'package:flutter/material.dart'; import 'package:flutter/services.dart';
const platform = const MethodChannel('com.example.shortcutexample/shortcut');
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Shortcut Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children:
if (snapshot.hasData) {
return Text(
'Has Shortcut: ${snapshot.data}',
style: TextStyle(fontSize: 18),
);
}
return Text(
'Unable to get shortcut status',
style: TextStyle(fontSize: 18),
);
},
),
],
),
),
),
);
}
Future
void _addShortcut() async { try { await platform.invokeMethod('addShortcut'); } on PlatformException catch (e) { print(e); } }
原文地址: https://www.cveoy.top/t/topic/eok9 著作权归作者所有。请勿转载和采集!