I don't know this is an issue or bug. So I'm writing it. I wrote an alarm manager function to execute certain tasks periodically. it's firing periodically. But the problem is I can't use certain plugins inside that. I used the shared preference plugin to take some data from locally. But the alarm manager function returns null on that. I don't know why. It only happens inside that Alarm manager static function. I just used another plugin Get storage to do a similar thing again. But the result was similar. In my case, I am just trying to build a wallpaper App. I just wanna change the wallpaper periodically. There are numerous categories like nature, bike, car, etc. for selection. If the user selects one category. Then I just fired an alarm manager and should have to do automatically change wallpaper according to the category. Due to the static function of the alarm manager, for knowing selected categories I just wrote those to the locals. So for grabbing it in the alarm manager static function, I'm used shared preference and Get storage plugins. But both of them can't work as expected in the alarm manager. I don't know why. Is this a bug or is this due to the static function of the alarm manager? Is there any solution or is there any idea to overcome it?
my code when user taps on a button to set wallpaper for certain category:
onPressed: () async {
print("::::::::::::");
await AndroidAlarmManager.initialize();
var yes = await AndroidAlarmManager.cancel(id);
await AndroidAlarmManager.cancel(id);
categoryScheduled = searchController.text.toString();
print(yes);
final box = GetStorage();
var data = {
"category": categoryScheduled ,
};
await box.write("bg_image", data);
final prefs = await SharedPreferences.getInstance();
prefs.setString('counter', categoryScheduled);
await AndroidAlarmManager.periodic(
timeInterval,
20,
term,
);
},
Here suppose a user selects a category like "nature" for auto-change the wallpaper under the nature category. we just initialize the alarm manager. and also we cancel if a certain alarm is executing. After I'm trying to store that selected category locally using the Get storage plugin and after I tried with shared preference too. But I cant grab that in the alarm manager static function.
my alarm manager static function is given below:
static term() async {
int pageNumber = 0;
final prefs = await SharedPreferences.getInstance();
//here we tries to grab the locally stored keyword using shared preferbnce but it return null.but its fine workin in another function.
final counter = prefs.getString('counter') ?? 0;
//next tried to grab it using another plugin to get storage. its also return null.
final box = GetStorage();
var readData = await box.read("bg_image");
print(readData);
print("this is category");
var query = readData["category"];
var results;
//tries to calling API with selected category. but category returning null.
final response = await http.get(Uri.parse(
"https://api.unsplash.com/search/photos/?client_id=7674db521921f5e47a89c6a432205d49db929dadaae45356226372c756e&page=$pageNumber&query=$query"));
// if (response.statusCode == 200) {
// itemCount++;
// Scaffold.of(context).hideCurrentSnackBar();
// If the call to the server was successful, parse the JSON
var jsonData = json.decode(response.body);
print(jsonData.length);
results = jsonData["results"];
print(results.length);
print(results[0]["urls"]["full"]);
var url = results[0]["urls"]["full"];
var progressString = Wallpaper.ImageDownloadProgress(url);
progressString.listen((data) {}, onDone: () async {
//setting walllpaper
await Wallpaper.homeScreen();
// scaffoldKey.currentState.showSnackBar(snackBar);
}, onError: (error) {
print("Some Error");
});
}