7

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
  • if this does not work you can use pub.dev/packages/external_app_launcher Commented Oct 5, 2022 at 4:15
  • It doesn't fulfil the requirement. I cannot pass any data through intent if I use this Library. Commented Oct 6, 2022 at 7:15
  • Do you want to pass data through intent? Intent works only on Android and not on iOS. So, do you want a generic code which can manage to share data on both OS or do you want code that works on Android only? Commented Oct 10, 2022 at 11:59
  • @DharamBudh Yes, I want to write a generic code as I have to do this for IOS too. But as I am not sure that how it will be achieved in Dart, I am doing it in native. If there is any way to do it in generic, would be much appreciated. Commented Oct 11, 2022 at 5:08

4 Answers 4

1

In my case I use this function

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);
}

When I set the flag Intent.FLAG_ACTIVITY_NEW_TASK it worked!

Sign up to request clarification or add additional context in comments.

1 Comment

I used this piece of code but didn't work.
1

more easy way can be this package.

https://pub.dev/packages/external_app_launcher

await LaunchApp.openApp(
              androidPackageName: 'net.pulsesecure.pulsesecure',
              iosUrlScheme: 'pulsesecure://',
              appStoreLink: 'itms-apps://itunes.apple.com/us/app/pulse-secure/id945832041',
              // openStore: false
            );

1 Comment

If we use this, can we parse data via intent from one app to another app?
0

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

<intent-filter>
             <action android:name="android.intent.action.VIEW" />
             <category android:name="android.intent.category.DEFAULT" />
             <category android:name="android.intent.category.BROWSABLE"/>

             <data
             android:scheme="<your-custom-scheme>"
             # if accept any data from u can specify them in the host, 
             path and port(if u have specific ones)
             # android:host="<your-host>"
             # android:port="<your-port>"
             />
</intent-filter>
  1. in your app1, u can just launch the above intent directly which will open your app2
    • u can use native code to do it or an easier way is to use the url_launcher

Comments

0

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.

1 Comment

Kindly read the question first and understand what it asks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.