Reputation: 386
I want to set wallpaper daily at a particular time and for this, I am using AlarmManager in android to invoke the set wallpaper function. Here is my code where I set Alarm to change wallpaper.:
public void setTime() {
alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, reciveBrodcast.class);
alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);
calendar.set(Calendar.MINUTE, 40);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
}
and I am initializing this function in onCreate() function of MainActivity.class
:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTime();
}
The setTime function started an reciveBrodcast.class
and i am calling setWallpaper function on this like:
public class reciveBrodcast extends BroadcastReceiver {
quotePresenter presenter =new quotePresenter();
@Override
public void onReceive(Context context, Intent intent) {
presenter.downloadTaskIn();
}
Here presenter is a class which has downloadTaskIn() to set wallpaper. But this code is not working and when I try this function with on button then it's working. Please help me how can I invoke this event at a particular time.
My mainifest :
<receiver android:name=".View.reciveBrodcast" android:process=":remote" />
<activity android:name=".View.MainActivity">
Upvotes: 0
Views: 116
Reputation: 417
Please try code below.
A helper Class
to set alarms.
public class AlarmSetter {
private final String TAG = "AlarmSetter";
private final int REQUEST_CODE = 9587;
private Context context;
public AlarmSetter(Context context) {
this.context = context;
}
public void setAlarm() {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (alarmManager == null) {
Log.e(TAG, "ALARM_SERVICE was null :(");
return;
}
Intent intent = new Intent(context, BRWallpaperChanger.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, 0);
cancelPreviousAlarm(alarmManager, pendingIntent);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);
calendar.set(Calendar.MINUTE, 40);
calendar.set(Calendar.SECOND, 0);
long when;
if (System.currentTimeMillis() <= calendar.getTimeInMillis()) {
when = calendar.getTimeInMillis();
} else {
when = calendar.getTimeInMillis() + 86400 * 1000; // Add one day in exact time
}
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
alarmManager.set(AlarmManager.RTC_WAKEUP, when, pendingIntent);
} else if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, when, pendingIntent);
} else {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, when, pendingIntent);
}
Log.d(TAG, "Next trigger set for at " + new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(when));
}
private void cancelPreviousAlarm(AlarmManager alarmManager, PendingIntent pendingIntent) {
alarmManager.cancel(pendingIntent);
}
}
Now in your onCreate()
of MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlarmSetter alarmSetter = new AlarmSetter(context);
alarmSetter.setAlarm();
}
Your BroadcastReceiver
for setting Wallpapers.
public class BRWallpaperChanger extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Set Alarm for next time...
AlarmSetter alarmSetter = new AlarmSetter(context);
alarmSetter.setAlarm();
// Change wallpaper here...
}
}
In your AndroidMenifest.xml
<receiver
android:name=".BRWallpaperChanger"
android:enabled="true"
android:exported="false" />
Please let me know if code works or not.
Upvotes: 1