本文整理汇总了Java中android.app.AlarmManager类的典型用法代码示例。如果您正苦于以下问题:Java AlarmManager类的具体用法?Java AlarmManager怎么用?Java AlarmManager使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
AlarmManager类属于android.app包,在下文中一共展示了AlarmManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setAlarm
import android.app.AlarmManager; //导入依赖的package包/类
public static void setAlarm(Context context, long waitTimeMillis) {
Intent intent = new Intent(context, ExpirationListener.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + waitTimeMillis, pendingIntent);
}
示例2: setAlarm
import android.app.AlarmManager; //导入依赖的package包/类
/**
* Sets a daily recurring alarm that is to trigger that
* 2 am device time.
*/
public static void setAlarm(Context context) {
DebugLog.logMethod();
context.getApplicationContext();
PendingIntent pendingIntent = getPendingIntent(context);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 2);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(
AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY,
pendingIntent
);
}
示例3: pushLocalNotification
import android.app.AlarmManager; //导入依赖的package包/类
public void pushLocalNotification( float fireTimeOffsetS, String title, String message, String data )
{
//Log.i("yoyo", "local notifcation scheduled in " + fireTimeOffsetS + " seconds");
Context appContext = mContext.getApplicationContext();
Intent intent = new Intent( appContext, PushLocalAlarmReceiver.class);
intent.putExtra( KEY_NTF_TITLE, title );
intent.putExtra( KEY_NTF_MESSAGE, message );
intent.putExtra( KEY_NTF_DATA, data);
//problems stacking multiple alarms...try this??
long timeMilli = System.currentTimeMillis() + (long)(fireTimeOffsetS*1000);
int uniqueID = (int)timeMilli;
PendingIntent pendingIntent = PendingIntent.getBroadcast(appContext, uniqueID, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager am = (AlarmManager)appContext.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, timeMilli, pendingIntent);
//store details of alarm - so it can be cancelled
storePendingLocalAlarm( title, message, data, timeMilli );
}
示例4: scheduleSync
import android.app.AlarmManager; //导入依赖的package包/类
private void scheduleSync(Context context, int syncJitter) {
int jitterMillis = (int) (RANDOM.nextFloat() * syncJitter);
final String debugMessage = "Scheduling next sync for " + jitterMillis + "ms";
LOGI(TAG, debugMessage);
((AlarmManager) context.getSystemService(Context.ALARM_SERVICE))
.set(
AlarmManager.RTC,
System.currentTimeMillis() + jitterMillis,
PendingIntent.getBroadcast(
context,
0,
new Intent(context, TriggerSyncReceiver.class),
PendingIntent.FLAG_CANCEL_CURRENT));
}
示例5: setclickalarm
import android.app.AlarmManager; //导入依赖的package包/类
public void setclickalarm(int id) {
int requestCode = 0;
requestCode = idValue;
// ---------------------------------------------------------------------
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(List.alist, ClockActivity.class);
intent.putExtra("content", contentValue);// ��������
intent.putExtra("alarmtime", alarmValue);// ��������ʱ��
PendingIntent pi = PendingIntent.getActivity(List.alist, requestCode,
intent, PendingIntent.FLAG_CANCEL_CURRENT); // ����id���ò�ͬ������
Calendar alarmTime = Calendar.getInstance();
long alarmdatetime = changedatetime(alarmValue); // ��ʱ��ת��Ϊlong��
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmdatetime, pi);
Toast.makeText(getApplicationContext(), "�������ú���", Toast.LENGTH_LONG)
.show();
}
示例6: initAlarm
import android.app.AlarmManager; //导入依赖的package包/类
private void initAlarm(Context context, String serviceName){
if(mAlarmManager == null){
mAlarmManager = ((AlarmManager)context.getSystemService(Context.ALARM_SERVICE));
}
if(mPendingIntent == null){
Intent intent = new Intent();
ComponentName component = new ComponentName(context.getPackageName(), serviceName);
intent.setComponent(component);
intent.setFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES);
mPendingIntent = PendingIntent.getService(context, 0, intent, 0);
}
mAlarmManager.cancel(mPendingIntent);
}
示例7: setAlarm
import android.app.AlarmManager; //导入依赖的package包/类
/**
* Set an alarm to silence the phone.
*
* @param startTimeInMillis The time at which phone is to be shushed
* @param endTimeInMillis The time at which phone is to be un-shushed
*/
public void setAlarm(long startTimeInMillis, long endTimeInMillis) {
if (startTimeInMillis >= endTimeInMillis) {
return;
}
alarmManager.set(
AlarmManager.RTC_WAKEUP,
startTimeInMillis,
getDefaultPendingIntent(true)
);
alarmManager.set(
AlarmManager.RTC_WAKEUP,
endTimeInMillis,
getDefaultPendingIntent(false)
);
}
示例8: setAlarm
import android.app.AlarmManager; //导入依赖的package包/类
private void setAlarm(Calendar targetCal) {
// Calendar calNow = ;
long timeInMilis = targetCal.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();
long seconds = timeInMilis / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
String time = hours % 24 + " hours," + minutes % 60 + " minutes," + seconds % 60 + " seconds";
Toast.makeText(getApplicationContext(), "Alarm set to " + (time) + " from now", Toast.LENGTH_LONG).show();
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
intent.putExtra(Constants.EXTRA_DEVICE, device);
intent.putExtra(Constants.STATE_DEVICE, buttons_state);
// Log.d("mmeessaaggee","buttons alarm state "+buttons_state);
AppUtils.saveAlarmState(getApplicationContext(), Constant.BUTTON_ALARM_STATE, buttons_state);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getBaseContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),
pendingIntent);
}
示例9: scheduleUpdate
import android.app.AlarmManager; //导入依赖的package包/类
private static void scheduleUpdate(Context context) {
/*
schedule updates via AlarmManager, because we don't want to wake the device on every update
see https://developer.android.com/guide/topics/appwidgets/index.html#MetaData
*/
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent intent = getAlarmIntent(context);
alarmManager.cancel(intent);
/* repeat after every full hour because results of search can change on new day
because of timezones repeat every hour instead of every day */
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.HOUR_OF_DAY, 1);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 1);
alarmManager.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_HOUR, intent);
}
示例10: onStartCommand
import android.app.AlarmManager; //导入依赖的package包/类
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
updateWeather();
updateBingPic();
AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
int anHour = 4 * 60 * 60 * 1000;//4个小时的毫秒数
long triggerAtTime = SystemClock.elapsedRealtime() + anHour;
Intent i = new Intent(this, AutoUpdateService.class);
PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
manager.cancel(pi);
manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);
return super.onStartCommand(intent, flags, startId);
}
示例11: onCreate
import android.app.AlarmManager; //导入依赖的package包/类
/**
* Initializes the service when it is first created
*/
@Override
public void onCreate() {
super.onCreate();
if (Constants.LOGVV) {
Log.v(Constants.TAG, "Service onCreate");
}
mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
mUpdateThread = new HandlerThread(TAG + "-UpdateThread");
mUpdateThread.start();
mUpdateHandler = new Handler(mUpdateThread.getLooper(), mUpdateCallback);
mNotifier = new DownloadNotifier(this);
mNotifier.cancelAll();
mObserver = new DownloadManagerContentObserver();
getContentResolver().registerContentObserver(Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI,
true, mObserver);
}
示例12: deleteAllReminders
import android.app.AlarmManager; //导入依赖的package包/类
/**
* Delete the all reminders
* @param context - the current app context
*/
public void deleteAllReminders(Context context) {
AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
for (int i = 0; i < mPlantsNumber; i++) {
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, i, new Intent(context, BirthdayReceiver.class), 0);
if (am != null) {
am.cancel(pendingIntent);
pendingIntent = PendingIntent.getBroadcast(context, i, new Intent(context, FertilizerReceiver.class), 0);
am.cancel(pendingIntent);
pendingIntent = PendingIntent.getBroadcast(context, i, new Intent(context, HeightMeasureReceiver.class), 0);
am.cancel(pendingIntent);
pendingIntent = PendingIntent.getBroadcast(context, i, new Intent(context, UpdatePhotoReceiver.class), 0);
am.cancel(pendingIntent);
pendingIntent = PendingIntent.getBroadcast(context, i, new Intent(context, WaterReceiver.class), 0);
am.cancel(pendingIntent);
}
}
}
示例13: setBirthdayReminder
import android.app.AlarmManager; //导入依赖的package包/类
/**
* Set when to remind about birthdays
* @param context - the current app context
* @param plant - the plant whose birthday is reminded of
* @param id - the id of the plant
*/
public void setBirthdayReminder(Context context, Plant plant, int id) {
Intent intent = new Intent(context, BirthdayReceiver.class);
intent.putExtra("name", plant.getName());
intent.putExtra("species", plant.getSpecies());
intent.putExtra("birthday", plant.getBirthday());
intent.putExtra("id", id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, intent, 0);
Calendar now = Calendar.getInstance();
now.setTimeInMillis(System.currentTimeMillis());
Calendar birthday = Calendar.getInstance();
birthday.setTimeInMillis(plant.getBirthday());
birthday.set(Calendar.YEAR, now.get((Calendar.YEAR)));
if (birthday.getTimeInMillis() < now.getTimeInMillis()) {
birthday.set(Calendar.YEAR, now.get((Calendar.YEAR)) + 1);
}
AlarmManager am = (AlarmManager)context.getSystemService(ALARM_SERVICE);
if (am != null) {
am.set(AlarmManager.RTC_WAKEUP, birthday.getTimeInMillis(), pendingIntent);
}
}
示例14: setAlarmEnableAirplaneMode
import android.app.AlarmManager; //导入依赖的package包/类
public void setAlarmEnableAirplaneMode(Context context)
{
Log.d(TAG, "setAlarmEnableAirplaneMode");
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
String enableAutoAirplaneMode = settings.getString(Constants.ENABLE_AIRPLANE_TIME, "23:00");
String[] enable = enableAutoAirplaneMode.split(":");
Calendar now = Calendar.getInstance();
Calendar calendarStart = Calendar.getInstance();
now.setTimeInMillis(System.currentTimeMillis());
calendarStart.setTimeInMillis(now.getTimeInMillis());
calendarStart.set(Calendar.HOUR_OF_DAY, Integer.valueOf(enable[0]));
calendarStart.set(Calendar.MINUTE, Integer.valueOf(enable[1]));
calendarStart.set(Calendar.SECOND, 0);
calendarStart.set(Calendar.MILLISECOND, 0);
Intent intentEnable = new Intent(context, AlarmBroadcastReceiver.class);
intentEnable.putExtra(Constants.ID, Constants.ID_ENABLE);
enableAirplaneModePendingIntent = PendingIntent.getBroadcast(context, Constants.ID_ENABLE, intentEnable, 0);
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendarStart.getTimeInMillis(), enableAirplaneModePendingIntent);
setAlarmAfterReboot(context, true);
}
示例15: onReceive
import android.app.AlarmManager; //导入依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
long scheduledTime = getNextScheduledExecutionTime(context);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(context, getClass());
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
if (System.currentTimeMillis() >= scheduledTime) {
scheduledTime = onAlarm(context, scheduledTime);
}
Log.w(TAG, getClass() + " scheduling for: " + scheduledTime);
alarmManager.cancel(pendingIntent);
alarmManager.set(AlarmManager.RTC_WAKEUP, scheduledTime, pendingIntent);
}