1.0.0 • Published 5 months ago
react-native-app-usage-monitor v1.0.0
# ReactNativeAppUsageMonitor
ReactNativeAppUsageMonitor is a React Native library for monitoring app usage data on Android. It provides a simple interface to retrieve statistics about other applications' usage on the user's device.
## Installation
First, install the library using npm:
```bash
npm install react-native-app-usage-monitor
or using yarn:
yarn add react-native-app-usage-monitor
Linking (React Native < 0.60)
If you are using a React Native version below 0.60, you need to link the library manually:
react-native link react-native-app-usage-monitor
Permissions
Add the following permission to your AndroidManifest.xml
:
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"/>
Usage
To use the library, import it into your JavaScript file:
import AppUsageMonitor from 'react-native-app-usage-monitor';
Requesting Permission
Before you can retrieve app usage data, you need to request the user's permission. Here's an example of how you can ask for it:
import { PermissionsAndroid, Linking } from 'react-native';
async function requestUsageStatsPermission() {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.PACKAGE_USAGE_STATS,
{
title: "App Usage Access",
message: "This app needs access to your usage stats"
}
);
if (granted !== PermissionsAndroid.RESULTS.GRANTED) {
// Permission denied
Linking.openSettings();
}
}
Retrieving App Usage Data
Once permission is granted, you can retrieve the app usage stats:
async function getAppUsageStats() {
try {
await requestUsageStatsPermission();
const stats = await AppUsageMonitor.getUsageStats();
console.log(stats);
} catch (error) {
console.error(error);
}
}
getAppUsageStats();
API Reference
getUsageStats()
Retrieves the app usage statistics. Returns a promise that resolves to an array of app usage data.
Each object in the array contains:
packageName
: The package name of the app.lastTimeUsed
: The last time the app was used.totalTimeInForeground
: The total time the app has been used in the foreground.
Contributing
Contributions are welcome. Please submit a pull request or create an issue for any features or bugs.
License
This project is licensed under the MIT License - see the LICENSE file for details.
### Additional Notes:
- **Customization:** You may need to customize this template to better fit your library's functionality and your specific instructions.
- **LICENSE File:** Ensure you have a `LICENSE` file in your repository if you reference it in the README.
- **Error Handling:** The usage example assumes basic error handling. You might want to elaborate on this in your actual implementation.
- **Testing:** Make sure the instructions are tested and work as expected in your library setup.
1.0.0
5 months ago