android_cn_oaid 0.0.2
android_cn_oaid: ^0.0.2 copied to clipboard
Flutter Android plugin for getting OAID/AAID and other device identifiers via Android_CN_OAID library.
import 'package:flutter/material.dart';
import 'package:android_cn_oaid/android_cn_oaid.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: OaidPage(),
);
}
}
class OaidPage extends StatefulWidget {
const OaidPage({super.key});
@override
State<OaidPage> createState() => _OaidPageState();
}
class _OaidPageState extends State<OaidPage> {
final _plugin = AndroidCnOaid();
bool _registered = false;
String _oaid = '-';
String _androidId = '-';
String _pseudoId = '-';
String _guid = '-';
List<String> _manufacturers = [];
String _error = '';
Future<void> _register() async {
try {
await _plugin.register();
setState(() => _registered = true);
} on OaidException catch (e) {
setState(() => _error = e.toString());
}
}
Future<void> _fetchAll() async {
try {
final oaid = await _plugin.getOAID();
final androidId = await _plugin.getAndroidID();
final pseudoId = await _plugin.getPseudoID();
final guid = await _plugin.getGUID();
final manufacturers = await _plugin.getSupportedManufacturers();
setState(() {
_oaid = oaid ?? '不支持';
_androidId = androidId ?? '不支持';
_pseudoId = pseudoId;
_guid = guid;
_manufacturers = manufacturers;
_error = '';
});
} on OaidException catch (e) {
setState(() => _error = e.toString());
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('android_cn_oaid 示例')),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(children: [
ElevatedButton(
onPressed: _register,
child: const Text('1. register()'),
),
const SizedBox(width: 8),
Text(_registered ? '✅ 已初始化' : '未初始化'),
]),
const SizedBox(height: 8),
ElevatedButton(
onPressed: _fetchAll,
child: const Text('2. 获取所有设备标识'),
),
const SizedBox(height: 16),
_InfoRow('OAID', _oaid),
_InfoRow('AndroidID', _androidId),
_InfoRow('PseudoID', _pseudoId),
_InfoRow('GUID', _guid),
if (_manufacturers.isNotEmpty) ...[
const SizedBox(height: 8),
const Text('支持厂商:',
style: TextStyle(fontWeight: FontWeight.bold)),
Text(_manufacturers.join(', ')),
],
if (_error.isNotEmpty) ...[
const SizedBox(height: 8),
Text('错误:$_error', style: const TextStyle(color: Colors.red)),
],
],
),
),
);
}
}
class _InfoRow extends StatelessWidget {
final String label;
final String value;
const _InfoRow(this.label, this.value);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
width: 100,
child: Text('$label:',
style: const TextStyle(fontWeight: FontWeight.bold)),
),
Expanded(child: Text(value, style: const TextStyle(fontSize: 12))),
],
),
);
}
}