react-native-app-usage-monitor v1.1.1
react-native-app-usage-monitor
A React Native library to monitor app usage on Android.
Installation
Using npm
npm install react-native-app-usage-monitor
Using yarn
yarn add react-native-app-usage-monitor
Linking
If you're using React Native 0.60 or higher, linking is handled automatically. Otherwise, you need to link the package manually:
react-native link react-native-app-usage-monitor
iOS Installation
Run the following command to install the necessary CocoaPods dependencies:
npx pod-install
Usage
Here's an example of how to use the react-native-app-usage-monitor
library in your React Native project.
import React, { useEffect, useState } from 'react';
import { View, Text, FlatList, StyleSheet } from 'react-native';
import AppUsage from 'react-native-app-usage-monitor';
const AppUsageExample = () => {
const [usageData, setUsageData] = useState([]);
const fetchUsageData = async () => {
try {
const data = await AppUsage.getUsageStats();
setUsageData(data);
} catch (error) {
console.error(error);
}
};
useEffect(() => {
fetchUsageData();
}, []);
return (
<View style={styles.container}>
<Text style={styles.title}>App Usage Data</Text>
<FlatList
data={usageData}
keyExtractor={(item) => item.packageName}
renderItem={({ item }) => (
<View style={styles.item}>
<Text>Package: {item.packageName}</Text>
<Text>Time in Foreground: {item.totalTimeInForeground} seconds</Text>
<Text>Last Time Used: {new Date(item.lastTimeUsed).toLocaleString()}</Text>
</View>
)}
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 16,
},
title: {
fontSize: 24,
marginBottom: 16,
},
item: {
marginBottom: 16,
padding: 16,
backgroundColor: '#f9f9f9',
borderRadius: 8,
},
});
export default AppUsageExample;
API
getUsageStats()
Returns an array of usage statistics for the past 24 hours.
Example Output
[
{
"packageName": "com.example.app",
"totalTimeInForeground": 120.5,
"lastTimeUsed": 1629954493000
},
// more items...
]
Permissions
Make sure to add the necessary permissions to your Android AndroidManifest.xml
file:
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" tools:ignore="ProtectedPermissions"/>
Troubleshooting
Common Issues
Permission Denied: Ensure you have granted the required permissions to your app. You may need to guide users to enable the "Usage Access" permission in their device settings.
No Data Available: If no usage data is available, ensure that the usage access permission is enabled and the app has been used within the time frame you are querying.
License
This library is licensed under the MIT License. See the LICENSE file for more information.
Contributing
Contributions are welcome! Please open an issue or submit a pull request if you have any improvements or bug fixes.
Author
MUBBITS - GitHub