# Executing in Background

If you want to call the `telephony` methods in background, you can do in the following ways.

**1. Using only Telephony.instance**

If you want to continue using `Telephony.insatnce` in the background, you will need to make sure that once the app comes back to the front, it again calls `Telephony.insatnce`.

```dart
backgrounMessageHandler(SmsMessage message) async {
    // Handle background message
    Telephony.insatnce.sendSms(to: "123456789", message: "Message from background")
}

void main() {
  runApp(MyApp());
}

class _MyAppState extends State<MyApp> {
  String _message;
  // This will not work as the instance will be replaced by
  // the one in background.
  final telephony = Telephony.instance;

   @override
  void initState() {
    super.initState();
    // You should make sure call to instance is made every time 
    // app comes to foreground
    final inbox = Telephony.insatnce.getInboxSms()
  }
```

**2. Use backgroundInstance**

If you cannot make sure that the call to instance would be made every time app comes to foreground, or if you would prefer to maintain a separate background instance, you can use `Telephony.backgroundInstance` in the background execution context.

```dart
backgrounMessageHandler(SmsMessage message) async {
    // Handle background message
    Telephony.backgroundInstance.sendSms(to: "123456789", message: "Message from background")
}

void main() {
  runApp(MyApp());
}

class _MyAppState extends State<MyApp> {
  String _message;
  final telephony = Telephony.instance;

   @override
  void initState() {
    super.initState();
    final inbox = telephony.getInboxSms()
  }
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://telephony.shounakmulay.dev/executing-in-background.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
