SoFunction
Updated on 2025-04-09

Android timed startup\Cancel small example

Copy the codeThe code is as follows:

Intent intent = new Intent("");
(this, );
PendingIntent pi=(this, 0, intent,0);
//Set a PendingIntent object to send a broadcast
AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
//Get AlarmManager object
// (AlarmManager.RTC_WAKEUP, ()+3500, pi);// Only execute once
(AlarmManager.RTC_WAKEUP, ()+3500, 10000, pi); //Repeat execution

Copy the codeThe code is as follows:

<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="" />
</intent-filter>
</receiver>

Copy the codeThe code is as follows:

public class AlarmReceiver extends BroadcastReceiver {
private static final String TAG = "AlarmReceiver";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
(TAG, "Broadcast Received");
// Intent it=new Intent(context,);
// (Intent.FLAG_ACTIVITY_NEW_TASK);
// (it);
//Start Activity after receiving the broadcast. For simplicity, you will jump directly to the Activity that sets alarm
//Intent must be added with Intent.FLAG_ACTIVITY_NEW_TASK flag
}
}

Cancel method:
Copy the codeThe code is as follows:

Intent intent = new Intent("");
(this, );
PendingIntent pi=(this, 0, intent,0);
AlarmManager alarm=(AlarmManager)getSystemService(ALARM_SERVICE);
(pi);

Another intent method
Code
Copy the codeThe code is as follows:

Intent intent =new Intent(, );
("repeating");
PendingIntent sender=PendingIntent
.getBroadcast(, 0, intent, 0);
//Start time
long firstime=();
AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);//5 seconds per cycle, continuously sending broadcasts
(AlarmManager.ELAPSED_REALTIME_WAKEUP
, firstime, 5*1000, sender);