Dev*_*raj 7 android-studio flutter
我正在尝试仅使用包名称从另一个 flutter 应用程序打开 flutter 应用程序。它不是深度链接,而只是简单地打开设备中安装的应用程序。还想共享一些数据并在 flutter 应用程序中接收这些数据。
我的第一个(本机)应用程序,我用一些文本数据调用我的 flutter 应用程序:-
var intent = context.packageManager.getLaunchIntentForPackage(packageName)
if (intent == null) {
intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse("market://details?id=$packageName")
val bundle = Bundle()
bundle.putString("user_id", "1111")
intent.putExtra("data", bundle)
intent.setType("text/plain")
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
Run Code Online (Sandbox Code Playgroud)
我正在使用方法通道。
class _MyHomePageState extends State<MyHomePage> {
static const methodChannel = MethodChannel("samples.flutter.dev/battery");
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
children: <Widget>[
ElevatedButton(
onPressed: () async {
openApp();
},
child: Text("Please work this time"))
],
),
),
);
}
static Future<bool?> openApp() async {
return await methodChannel.invokeMethod<bool>("getOpenApp");
}
}
Run Code Online (Sandbox Code Playgroud)
现在 Activity 类是这样的:-
class MainActivity : FlutterActivity() {
//private var sharedText: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val intent = intent
val action = intent.action
val type = intent.type
if (Intent.ACTION_SEND == action && type != null) {
if ("text/plain" == type) {
// handleSendText(intent) // Handle text being sent
}
}
}
private val CHANNEL = "samples.flutter.dev/battery"
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL)
.setMethodCallHandler { call: MethodCall, result: MethodChannel.Result ->
if (call.method.contentEquals("getOpenApp")) {
var userId = "No data received"
val bundle: Bundle? = intent.getBundleExtra("data");
//val bundle = intent.getBundleExtra("data");
if (bundle!=null){
userId = bundle.getString("user_id").toString()
Log.d("userid","is equals to: $userId")
System.out.print("userid ki value : $userId")
}else{
Log.d("empty","is equals to: empty hai ")
}
result.success(200)
//sharedText = null
}
}
}
Run Code Online (Sandbox Code Playgroud)
}
请让我知道我在这里犯了什么错误,因为我每次都收到空包。
就我而言,我使用这个函数
public void startNewActivity(Context context, String packageName) {
Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
if (intent == null) {
// Bring user to the market or let them choose an app?
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=" + packageName));
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)
当我设置标志时,Intent.FLAG_ACTIVITY_NEW_TASK它起作用了!
| 归档时间: |
|
| 查看次数: |
2887 次 |
| 最近记录: |