advanced_sms_log 1.0.1 copy "advanced_sms_log: ^1.0.1" to clipboard
advanced_sms_log: ^1.0.1 copied to clipboard

PlatformAndroid

Android SMS log reader with real-time incoming stream, optional store observer, SMS send status callbacks, and OTP via SMS User Consent API.

example/lib/main.dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:advanced_sms_log/advanced_sms_log.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'advanced_sms_log example',
      theme: ThemeData(useMaterial3: true),
      home: const Home(),
    );
  }
}

class Home extends StatefulWidget {
  const Home({super.key});

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  final _events = <String>[];
  StreamSubscription<SmsEvent>? _sub;

  final _toCtrl = TextEditingController();
  final _msgCtrl = TextEditingController(text: 'Hello from advanced_sms_log');

  void addLine(String s) {
    setState(() {
      _events.insert(0, '${DateTime.now().toIso8601String()}  $s');
      if (_events.length > 60) _events.removeLast();
    });
  }

  @override
  void initState() {
    super.initState();
    _sub = AdvancedSmsLog.events.listen((e) {
      switch (e.type) {
        case SmsEventType.received:
          addLine('RECEIVED: ${e.sms?.address} :: ${e.sms?.body}');
          break;
        case SmsEventType.storeChanged:
          addLine('STORE_CHANGED: latest=${e.sms?.address} :: ${e.sms?.body}');
          break;
        case SmsEventType.sentStatus:
          addLine('SENT_STATUS: ${e.meta}');
          break;
        case SmsEventType.otp:
          addLine('OTP: ${e.otp} | msg=${e.message}');
          break;
        case SmsEventType.error:
          addLine('ERROR: ${e.error}');
          break;
      }
    });
  }

  @override
  void dispose() {
    _sub?.cancel();
    _toCtrl.dispose();
    _msgCtrl.dispose();
    super.dispose();
  }

  Future<void> _permLogs() async {
    final ok = await AdvancedSmsLog.requestSmsLogPermission();
    addLine('READ_SMS permission: $ok');
  }

  Future<void> _permReceive() async {
    final ok = await AdvancedSmsLog.requestReceivePermission();
    addLine('RECEIVE_SMS permission: $ok');
  }

  Future<void> _permSend() async {
    final ok = await AdvancedSmsLog.requestSendPermission();
    addLine('SEND_SMS permission: $ok');
  }

  Future<void> _loadInbox() async {
    final ok = await AdvancedSmsLog.hasSmsLogPermission();
    if (!ok) {
      addLine('READ_SMS not granted');
      return;
    }
    final list = await AdvancedSmsLog.getSms(box: SmsBox.inbox, limit: 20, offset: 0);
    addLine('INBOX loaded: ${list.length} items');
    for (final m in list.take(5)) {
      addLine('INBOX: ${m.address} :: ${m.body}');
    }
  }

  Future<void> _startListener() async {
    await AdvancedSmsLog.startIncomingListener(useBroadcast: true, useObserver: true);
    addLine('Listener started (broadcast+observer)');
  }

  Future<void> _stopListener() async {
    await AdvancedSmsLog.stopIncomingListener();
    addLine('Listener stopped');
  }

  Future<void> _startOtp() async {
    await AdvancedSmsLog.startOtpUserConsent(otpRegex: r'\b\d{4,8}\b');
    addLine('OTP consent started');
  }

  Future<void> _sendSms() async {
    final to = _toCtrl.text.trim();
    final msg = _msgCtrl.text.trim();
    if (to.isEmpty || msg.isEmpty) {
      addLine('Enter "to" and "message"');
      return;
    }
    final ok = await AdvancedSmsLog.sendSms(to: to, message: msg);
    addLine('sendSms(): $ok');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('advanced_sms_log example')),
      body: Padding(
        padding: const EdgeInsets.all(12),
        child: Column(
          children: [
            Wrap(
              spacing: 8,
              runSpacing: 8,
              children: [
                FilledButton(onPressed: _permLogs, child: const Text('Request READ_SMS')),
                FilledButton(onPressed: _permReceive, child: const Text('Request RECEIVE_SMS')),
                FilledButton(onPressed: _permSend, child: const Text('Request SEND_SMS')),
                OutlinedButton(onPressed: _loadInbox, child: const Text('Load Inbox')),
                OutlinedButton(onPressed: _startListener, child: const Text('Start Listener')),
                OutlinedButton(onPressed: _stopListener, child: const Text('Stop Listener')),
                OutlinedButton(onPressed: _startOtp, child: const Text('Start OTP Consent')),
              ],
            ),
            const SizedBox(height: 12),
            TextField(
              controller: _toCtrl,
              decoration: const InputDecoration(
                labelText: 'Send To (phone)',
                border: OutlineInputBorder(),
              ),
            ),
            const SizedBox(height: 8),
            TextField(
              controller: _msgCtrl,
              decoration: const InputDecoration(
                labelText: 'Message',
                border: OutlineInputBorder(),
              ),
            ),
            const SizedBox(height: 8),
            Align(
              alignment: Alignment.centerLeft,
              child: FilledButton.tonal(
                onPressed: _sendSms,
                child: const Text('Send SMS (via plugin)'),
              ),
            ),
            const SizedBox(height: 12),
            Expanded(
              child: Container(
                width: double.infinity,
                padding: const EdgeInsets.all(10),
                decoration: BoxDecoration(
                  border: Border.all(color: Theme.of(context).dividerColor),
                  borderRadius: BorderRadius.circular(12),
                ),
                child: ListView.builder(
                  itemCount: _events.length,
                  itemBuilder: (_, i) => Text(
                    _events[i],
                    style: const TextStyle(fontSize: 12, height: 1.25),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
1
likes
150
points
108
downloads

Publisher

verified publishernh97.co.in

Weekly Downloads

Android SMS log reader with real-time incoming stream, optional store observer, SMS send status callbacks, and OTP via SMS User Consent API.

Homepage
Repository (GitHub)
View/report issues

Topics

#sms #telephony #otp #android #permissions

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on advanced_sms_log

Packages that implement advanced_sms_log