2

In our flutter TV application, we are trying the open few entertainment apps using DeviceApps . The following methods doesn't open the App though it is installed on the device.

 DeviceApps.openApp("com.sonyliv");

Not able to figure out why sonyliv is not opening.

Note: Please ignore Touch is not implemented to this app currently.So only tap works.

Complete snippet

import 'package:device_apps/device_apps.dart';
import 'package:flutter/material.dart';
import 'dart:typed_data';

class ListAppsPage extends StatelessWidget {

  const ListAppsPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListAppsBody(),
    );
  }
}

class ListAppsBody extends StatefulWidget {
  @override
  _ListAppBodyState createState() => _ListAppBodyState();
}

class _ListAppBodyState extends State {
  List listApps = [];

  @override
  void initState() {
    super.initState();
    _getApp();
  }

   void _getApp() async{
    List _apps = await DeviceApps.getInstalledApplications(includeAppIcons: true);
    for(var app in _apps){

     // print(app.apkFilePath);

    if(!app.packageName.contains("example")) {
        var item = AppModel(
        title: app.appName,
        package: app.packageName,
        icon: app.icon,
      );
      listApps.add(item);
    }

    }

    //reloading state
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: listApps.length,
      itemBuilder: (context, int i) => Column(
        children: [
           ListTile(
            leading: Image.memory(listApps[i].icon),
            title:  Text(listApps[i].title),
            subtitle:  Text(listApps[i].package),
            onTap: (){
              DeviceApps.openApp(listApps[i].package);
      
            },
          ),
        ],
      ),
    );
  }
}

class AppModel{
  final String title;
  final String package;
  final Uint8List icon;

  AppModel({
   required this.title,
   required this.package,
   required this.icon
  });
}
George
  • 83
  • 12
anandyn02
  • 259
  • 1
  • 3
  • 6

1 Answers1

0

To open another app from within a Flutter app on the same device, you can use the url_launcher package. First, you need to add this package to your Flutter project's pubspec.yaml file. You can do this by adding the following line under dependencies:

dependencies:
  flutter:
    sdk: flutter
  url_launcher: ^6.0.0

Then, run the command flutter pub get to download the package.

Next, you can use the url_launcher package to open another app using its package identifier. In your case, the package identifier for the SonyLIV app is com.sonyliv . Use the following code as an example:

main.dart

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Open Another App'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              openApp();
            },
            child: Text('Open SonyLIV App'),
          ),
        ),
      ),
    );
  }

  void openApp() async {
    final String appUrl = 'com.sonyliv://';
    if (await canLaunch(appUrl)) {
      await launch(appUrl);
    } else {
      throw 'Could not launch $appUrl';
    }
  }
}

When you run the app, it will display a button labeled "Open SonyLIV App." When you click on the button, it will attempt to open the SonyLIV app on the same device if it is installed. If it's not installed, an exception will be thrown.

Please note that the app you're trying to open must be installed on the device for the code to work correctly.

  • the constructed uri under openApp is not launching the app. I found an alternative solution which opens the app. Instead of using 'com.sonyliv://' I'm calling https://www.sonyliv.com/dplnk?sony://details/ – anandyn02 Jun 04 '23 at 14:34