Android timed start \ cancel

Source: Internet
Author: User

[Java]
Intent intent = new Intent ("cn. pocketdigi. update. alarm ");
Intent. setClass (this, AlarmReceiver. class );
 
PendingIntent pi = PendingIntent. getBroadcast (this, 0, intent, 0 );
// Set a PendingIntent object to send a broadcast
AlarmManager am = (AlarmManager) getSystemService (ALARM_SERVICE );
// Obtain the AlarmManager object
// Am. set (AlarmManager. RTC_WAKEUP, System. currentTimeMillis () + 3500, pi); // only run once
 

Am. setRepeating (AlarmManager. RTC_WAKEUP, System. currentTimeMillis () + 3500,100 00, pi); // repeated execution

Intent intent = new Intent ("cn. pocketdigi. update. alarm ");
Intent. setClass (this, AlarmReceiver. class );
 
PendingIntent pi = PendingIntent. getBroadcast (this, 0, intent, 0 );
// Set a PendingIntent object to send a broadcast
AlarmManager am = (AlarmManager) getSystemService (ALARM_SERVICE );
// Obtain the AlarmManager object
// Am. set (AlarmManager. RTC_WAKEUP, System. currentTimeMillis () + 3500, pi); // only run once


Am. setRepeating (AlarmManager. RTC_WAKEUP, System. currentTimeMillis () + 3500,100 00, pi); // repeated execution
 

[Html]
<Cycler android: name = ". AlarmReceiver">
<Intent-filter>
<Action android: name = "cn. pocketdigi. update. alarm"/>
</Intent-filter>
</Cycler>

<Cycler android: name = ". AlarmReceiver">
<Intent-filter>
<Action android: name = "cn. pocketdigi. update. alarm"/>
</Intent-filter>
</Cycler>
 

[Java]
Public class AlarmReceiver extends BroadcastReceiver {
Private static final String TAG = "AlarmReceiver ";
@ Override
Public void onReceive (Context context, Intent intent ){
// TODO Auto-generated method stub
Log. v (TAG, "receive broadcast ");
// Intent it = new Intent (context, AlarmActivity. class );
// It. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK );
// Context. startActivity (it );
// After receiving the broadcast, start the Activity. For simplicity, you can directly jump to the Activity that sets alarm.
// Intent. FLAG_ACTIVITY_NEW_TASK flag must be added to Intent.
}
}

Public class AlarmReceiver extends BroadcastReceiver {
Private static final String TAG = "AlarmReceiver ";
@ Override
Public void onReceive (Context context, Intent intent ){
// TODO Auto-generated method stub
Log. v (TAG, "receive broadcast ");
// Intent it = new Intent (context, AlarmActivity. class );
// It. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK );
// Context. startActivity (it );
// After receiving the broadcast, start the Activity. For simplicity, you can directly jump to the Activity that sets alarm.
// Intent. FLAG_ACTIVITY_NEW_TASK flag must be added to Intent.
}
}
Cancellation Method:

[Java]
Intent intent = new Intent ("cn. pocketdigi. update. alarm ");
Intent. setClass (this, AlarmReceiver. class );
 
PendingIntent pi = PendingIntent. getBroadcast (this, 0, intent, 0 );
AlarmManager alarm = (AlarmManager) getSystemService (ALARM_SERVICE );
Alarm. cancel (pi );

Intent intent = new Intent ("cn. pocketdigi. update. alarm ");
Intent. setClass (this, AlarmReceiver. class );
 
PendingIntent pi = PendingIntent. getBroadcast (this, 0, intent, 0 );
AlarmManager alarm = (AlarmManager) getSystemService (ALARM_SERVICE );
Alarm. cancel (pi );
 

Another intent Method

[Java]
Code
Intent intent = new Intent (Main. this, alarmreceiver. class );
Intent. setAction ("repeating ");
PendingIntent sender = PendingIntent
. GetBroadcast (Main. this, 0, intent, 0 );
// Start time
Long firstime = SystemClock. elapsedRealtime ();
 
AlarmManager am = (AlarmManager) getSystemService (ALARM_SERVICE); // sends broadcasts continuously for a cycle of 5 seconds.
Am. setRepeating (AlarmManager. ELAPSED_REALTIME_WAKEUP
, Firstime, 5*1000, sender );

Code
Intent intent = new Intent (Main. this, alarmreceiver. class );
Intent. setAction ("repeating ");
PendingIntent sender = PendingIntent
. GetBroadcast (Main. this, 0, intent, 0 );
// Start time
Long firstime = SystemClock. elapsedRealtime ();

AlarmManager am = (AlarmManager) getSystemService (ALARM_SERVICE); // sends broadcasts continuously for a cycle of 5 seconds.
Am. setRepeating (AlarmManager. ELAPSED_REALTIME_WAKEUP
, Firstime, 5*1000, sender );

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.