I am trying to open flutter app from an another flutter app using
package name only. It is not deep linking but just simply opening the
application which is installed in device. Also want to share some data and receive that in flutter app.My First (Native) application from which I am calling my flutter application with some text data:-
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)
I am using method-channel for that.
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");
}
}
Now the Activity class is like this :-
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
}
}
}
}
Please let me know what mistake I am doing here as I am getting empty bundle every time.
4
Answers
In my case I use this function
When I set the flag
Intent.FLAG_ACTIVITY_NEW_TASK
it worked!more easy way can be this package.
https://pub.dev/packages/external_app_launcher
This should be relatively easy,
consider u have two apps, app1 & app2, u have to open app2 from the
app1
1- on the app2, specify a custom intent in the manifest file
In Android, you can open an app with the adb command line tool.
with adb you can do many cool things like port forwarding, activity launches, install, uninstall — the list goes on.
But for this app to open, you can simply run this command.
adb shell am start -n "<package name>/<path to your main activity class>" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
In this command we use shell to enter the mobile shell then
We use the am(activity manager) to start the new activity.
In our case, we need to start the app, so every app has a launcher activity. We just start it and voila, the app gets started.