我想每天在特定时间设置墙纸,为此,我在android中使用AlarmManager调用设置墙纸功能。这是我设置“警报”以更改墙纸的代码。
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);
}
并且我正在MainActivity.class
的onCreate()函数中初始化此函数:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTime();
}
setTime函数启动了一个reciveBrodcast.class
,我正在像这样调用setWallpaper函数:
public class reciveBrodcast extends BroadcastReceiver {
quotePresenter presenter =new quotePresenter();
@Override
public void onReceive(Context context, Intent intent) {
presenter.downloadTaskIn();
}
这里的主持人是一个具有downloadTaskIn()来设置墙纸的类。 但是此代码不起作用,当我尝试使用“打开”按钮时,此功能将起作用。请帮助我如何在特定时间调用此事件。
我的清单:
<receiver android:name=".View.reciveBrodcast" android:process=":remote" />
<activity android:name=".View.MainActivity">
答案 0 :(得分:1)
请尝试以下代码。
Class
助手来设置警报。
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);
}
}
现在进入您onCreate()
中的MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlarmSetter alarmSetter = new AlarmSetter(context);
alarmSetter.setAlarm();
}
您的BroadcastReceiver
用于设置壁纸。
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...
}
}
在您的AndroidMenifest.xml
<receiver
android:name=".BRWallpaperChanger"
android:enabled="true"
android:exported="false" />
请让我知道代码是否有效。