dragonfly
dragonfly

Reputation: 1

Share url from other application to my flutter application and making an api call without opening application at all

I am working on application where user can share URL from other application to my flutter and that URL will be saved in database and user can access it from application whenever they want to.

Problem currently I am facing is when URL is shared to my application for example from YouTube it opens application instance for some time until API call is made or URL is saved to SQLite database then by using exit(0) I am closing the app. I want seamless sharing where user don't get interrupted while sharing URL.

I tried using broadcast receiver but I am unable to implement it as I planned the application to behave.

I would like to know is there any way in flutter by which I can successfully implement this feature in my application.

main.dart


Dio dio = Dio();

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
  FirebaseAuth.instance.setSettings(appVerificationDisabledForTesting: false);
  await storeLocal();
  runApp(const MyApp());
}

storeLocal() async {
  var value = await ReceiveSharingIntent.instance.getInitialMedia();
  if (value.length > 0) {
    var shareUrl = value.first.path;


    var response = await dio.get(
        "----=$shareUrl");
    var jarr = json.decode(response.toString());
    var _addUrlPara = jsonEncode({});
    var pref = await SharedPreferences.getInstance();
    pref.setString(ISUPLOADED, "false");
    Map<String, dynamic> decodedData = await jsonDecode(_addUrlPara);
    await storeUrlToSqlite(decodedData);
    exitApp();
  }
}

exitApp() {
  exit(0);
}

Manifest file

            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter >
                <action android:name="android.intent.action.SEND" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:mimeType="*/*" />

            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
    <!-- Required to query activities that can process text, see:
         https://developer.android.com/training/package-visibility?hl=en and
         https://developer.android.com/reference/android/content/Intent#ACTION_PROCESS_TEXT.

         In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
    <queries>
        <intent>
            <action android:name="android.intent.action.PROCESS_TEXT"/>
            <data android:mimeType="text/plain"/>
        </intent>
    </queries>

I have tried to implement broadcast receiver but I am unable to get result for my problem, I tried using exit(0) and exit the app and remove the app instance but when I URL is shared from other application my app is launching and gives black screen because app runApp() is not called

Upvotes: 0

Views: 82

Answers (0)

Related Questions