我正在尝试与const isActive = onActive => (match, location) => {
if (match) {
onActive();
}
return match;
}
const App = () => {
const onActive = () => console.log("link is active");
return (
<BrowserRouter>
<div>
<NavLink to="/abc" activeClassName="active" isActive={isActive(onActive)}>Go To ABC</NavLink>
<Route path="/abc" render={() => <h1>ABC Page</h1>} />
</div>
</BrowserRouter>
);
}
合作,并提供服务以将自动SMS发送至某些号码。我遵循了一些教程,并编写了如下内容:
AlarmManager
我的BroadcastReceiver:
SwitchPreference testPref = (SwitchPreference) findPreference("key_auto_sms");
SharedPreferences sharedPrefs = getActivity().getSharedPreferences("XXXXXXXXX", MODE_PRIVATE);
testPref.setChecked(sharedPrefs.getBoolean("AutoSMS", false));
testPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
if (testPref.isChecked()) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int curHr = calendar.get(Calendar.HOUR_OF_DAY);
// Checking whether current hour is over 14
if (curHr >= 15)
{
// Since current hour is over 15, setting the date to the next day
calendar.add(Calendar.DATE, 1);
}
calendar.set(Calendar.HOUR_OF_DAY, 15);
calendar.set(Calendar.MINUTE, 1);
calendar.set(Calendar.SECOND, 00);
AlarmManager am = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(getActivity(), AlarmReceiver.class);
final PendingIntent pi = PendingIntent.getBroadcast(getActivity(), 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
SharedPreferences.Editor editor = getActivity().getSharedPreferences("XXXXXXXXX", MODE_PRIVATE).edit();
editor.putBoolean("AutoSMS", true);
editor.commit();
Toast.makeText(getActivity(), getResources().getString(R.string.Auto_SMS_Enable), Toast.LENGTH_SHORT).show();
} else {
SharedPreferences.Editor editor = getActivity().getSharedPreferences("com.camel.work_list", MODE_PRIVATE).edit();
editor.putBoolean("AutoSMS", false);
editor.commit();
Toast.makeText(getActivity(), getResources().getString(R.string.Auto_SMS_Cancle), Toast.LENGTH_SHORT).show();
Intent intent1 = new Intent(getActivity(), AlarmReceiver.class);
final PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, intent1, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarm = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pendingIntent);
//getActivity().stopService(intent1);
}
return false;
}
});
}
因此,问题在于警报仅会运行一次,然后在第二天停止工作。
我想念什么?
答案 0 :(得分:0)
您不应使用setRepeating(),因为它在时间间隔上不准确。您可以一次使用set()函数,它将更准确地工作,当广播被调用时,您可以再次调用set()(如递归),它将起作用。 这是我的警报设置代码,不适用于huawei等设备,因为这些进程将被杀死。与华为一起使用JobScheduler。如果屏幕关闭时屏幕变亮,则可以正常工作。它不能正常工作。最近,有一个适用于所有设备的WorkManager库,我没有时间研究它,因此我对WorkManager库没有任何建议。希望对您有帮助
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
assert alarmManager != null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setAlarmClock(new AlarmManager.AlarmClockInfo(time, pendingIntent), pendingIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
}