skip to Main Content

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


  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!

    Login or Signup to reply.
  2. 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
                );
    
    Login or Signup to reply.
  3. 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
    Login or Signup to reply.
  4. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search