Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open Flutter app programmatically using Package name only

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.

like image 285
Devraj Avatar asked Aug 14 '26 06:08

Devraj


1 Answers

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!

like image 115
Guilherme Gabanelli Avatar answered Aug 16 '26 09:08

Guilherme Gabanelli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!