I want use String start = "16:00"; in specific time, start another activity.
I must use String start = "16:00"
MainActivity.class
String start = "16:00";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setAlarmTime(this);
}
private void setAlarmTime(Context context) {
String[] strStart = start.split(":") // delete ":"
Calendar cal_start = Calendar.getInstance();
cal_start.set(Calendar.HOUR_OF_DAY, Integer.parseInt(strStart[0])); // hour
cal_start.set(Calendar.MINUTE, Integer.parseInt(strStart[1])); //minute
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
alarm.set(AlarmManager.RTC, cal_start.getTimeInMillis(), pIntent);
}
I want while the app is running, current time 4 o'clock , start AlarmActivity.class .
But it does not work.
How to every specific time start another activity on android?
#update
private void setAlarmTime(Context context) {
String[] strStart = start.split(":");
Calendar cal_start = Calendar.getInstance();
cal_start.set(Calendar.HOUR_OF_DAY, Integer.parseInt(strStart[0]));
cal_start.set(Calendar.MINUTE, Integer.parseInt(strStart[1]));
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
alarm.set(AlarmManager.RTC_WAKEUP, cal_start.getTimeInMillis(), pendingIntent);
this source not work.
not work alarmManager.
Please consider to use AlarmManager.RTC_WAKEUP if you need to wake up the device even if it goes off.
alarm.set(AlarmManager.RTC_WAKEUP, cal_start.getTimeInMillis(), pIntent);
AlarmManager.RTC will NOT wake the device up.
Reference: https://developer.android.com/reference/android/app/AlarmManager.html#RTC
Btw, you do not need to pass the context reference to method:
PendingIntent pIntent = PendingIntent.getActivity(this /*can use this as it is a context already */ , 0, intent, 0);
Update:
Please also set the second and millisecond of the cal_start; otherwise it will be the values that you get the calendar instance.
cal_start.set(Calendar.SECOND, 0);
cal_start.set(Calendar.MILLISECOND, 0);
Update 2:
It works in my side, you may try to add
<uses-permission android:name="android.permission.WAKE_LOCK" />
in Manifests.
Btw, if you want this alarm repeat every day
AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal_start.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pIntent);
Add this permission in your manifest
<uses-permission android:name="android.permission.WAKE_LOCK" />
register your receiver class in the application tag in the manifest file
<receiver android:name=".AlarmActivity" />
Resister your alarm that will trigger AlarmActivity at a specific time in your case its 16:00
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 16);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Intent intent = new Intent(getApplicationContext(), AlarmActivity.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Make sure your register class extend BroadcastReceiver like this
class AlarmActivity extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Do whatever you want
// you can generate notifications here
// or can start your application activity you want
}
}
Related
I would create an alarm clock, I wrote this code but return this error:
2019-02-05 10:58:13.902 2663-10077/com.google.android.gms E/ChromeSync: [Sync,SyncIntentOperation] Error handling the intent:
Intent { act=android.intent.action.PACKAGE_ADDED
dat=package:com.example.iacopo.alarmgroup flg=0x4000010
cmp=com.google.android.gms/.chimera.GmsIntentOperationService (has
extras) }.
How can I fix it? Thanks. And this code doesn't create an icon of alarm in the notification panel, near the clock.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.clear();
cal.set(2018,1,5,10,0);
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
this.startService(intent);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
alarmMgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
}
}
and the receiver
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("test","ok");
}
}
cal.set(2018,1,5,10,0);
The calendar takes earlier than the current time, so your alarm clock is unlikely to be triggered. Change the time correctly and try again.
This is a basic snippet from developer.android.com. You can change date and time as per your needs and pass the calendar instance to Alarm Manager intent.
// Example: Wake up the device to fire a one-time (non-repeating) alarm in one minute:
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() +
60 * 1000, alarmIntent);
// Example: Set the alarm to start at approximately 2:00 p.m. say
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);
// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
private void startAlarm() {
AlarmManager alarmMgr;
PendingIntent alarmIntent;
alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 52);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, alarmIntent);
}
startAlarm() method is in the MainActivity class.
The following code should display a toast (declared in onReceive() method of the AlarmReceiver class) at 23:52 and every 20 minutes thereafter
AlarmReceiver extends BroadcastReceiver.
The code compiles without errors but for some reason it is not displaying the toast.
NOTE : The code worked fine with ELAPSED_REALTIME_WAKEUP. I am having problem with RTC_WAKEUP only. So everything else has to be correct. There is something wrong inside the startAlarm() method only.
I have tried your code with some modification as below .
private void startAlarm() {
AlarmManager alarmMgr;
PendingIntent alarmIntent;
alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 2);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 20, alarmIntent);
}
This code works fine with AlarmManager.RTC_WAKEUP. If it does not work for you, please reboot your device after installing it. After reboot completed, start your app. Then it should fire the alarm and every 20 sec interval the alarm fires repeatedly.
You can also check the difference between RTC_WAKEUP and ELAPSED_REALTIME_WAKEUP. From android docs, elapsedRealtime means, Returns milliseconds since boot, including time spent in sleep and RTC means, time in System.currentTimeMillis() (wall clock time in UTC).
Looks like You need to set an action to the intent:
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
intent.setAction("YourPackageName.YourAction");
alarmIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
And in the manifest:
<receiver android:name=".AlarmReciever">
<intent-filter>
<action android:name="YourPackageName.YourAction" />
</intent-filter>
</receiver>
This should fix the issue. You must have a unique action in the intent for use it with broadcast receiver.
I know this is very simple and can do anyone but i m stuck in this plz help where i did mistake
one think very important i start from one.class and stop in second.class and i can do stop after 3rd or 4th day of start.
i start alarm like this
//set Alarm method
private void SetAutoCleanningService()
{
long when= System.currentTimeMillis() + 1000*20;
long repeattime =Constant.NOTIFICATION_TimeDiff;
if(Constant.IsDebug)
System.out.println("time to trigger is ="+getDate(when, "dd/MM/yyyy hh:mm:ss.SSS"));
Intent intent = new Intent(this, AutoCleaningBroadCast.class);
intent.setAction("systweak.AutoCleanningBroadCast");
PendingIntent pendingIntent = PendingIntent.getBroadcast(ApplicationSetting.this,1234567, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when, repeattime,pendingIntent);
}
And stop like this
//close Alarm method
private void StopEarlierAlarm() {
// TODO Auto-generated method stub
AlarmManager aManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(getBaseContext(), AutoCleaningBroadCast.class);
PendingIntent pIntent = PendingIntent.getBroadcast(ApplicationSetting.this, 1234567, intent,0);
aManager.cancel(pIntent);
}
i use same request code but cant able to stop and on every alarm i show one notification that will be repeat in every alarm calling
You can cancel the alarm like this:
Intent intent = new Intent(this, Mote.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1352, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Also, you should remove Intent.FILL_IN_DATA from your call to getBroadcast() in the code where you set the alarm.
Cancel your PendingIntent too.
pIntent.cancel();
Make sure context used in creating intent is same.
If not possible to use same Context, then create intent as a global Static variable and use later.
Check this, it may help you
Step 1:initialize the alarm manager.
AlarmManager alarmManager1;
Step 2:
// for Notification Process
Intent myIntent1 = new Intent(Settings.this,MyNotificationService.class);
pendingintent2 = PendingIntent.getService(SettingsPage.this, 2, myIntent1, 2);
alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar1Notify = Calendar.getInstance();
calendar1Notify.setTimeInMillis(System.currentTimeMillis());
calendar1Notify.set(Calendar.HOUR_OF_DAY, 8);
calendar1Notify.set(Calendar.MINUTE, 15);
alarmManager1.set(AlarmManager.RTC_WAKEUP,calendar1Notify.getTimeInMillis(), pendingintent2);
long time24h = 24 * 60 * 60 * 1000;
alarmManager1.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar1Notify.getTimeInMillis(), time24h,pendingintent2);
Step 3-Stop alarm manager:
alarmManager1.cancel(pendingintent2);
I want to display a notification from my application at a specific time say 6.30 am daily. I have successfully done that. Following is the code which executes at specified time. However the code works only if the app is in open state or put to background. It does not work if I reboot the device and don't launch the app, also does not work if I kill the app .
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
PendingIntent alarmIntent;
alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(MainActivity.this, MyReceiver.class);
alarmIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY,6);
calendar.set(Calendar.MINUTE,30);
calendar.set(Calendar.AM_PM,Calendar.AM);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
alarmIntent);
Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
Following is the receiver BroadCast method
#Override
public void onReceive(Context context, Intent intent) {
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}
The alarm service class displays a notification.
/...Display notification.../
I have a repeating alarm set, my problem is that after cancelling it doesn't cancel (I'm checking this with Log.v()
This is how I create the alarm (in an IntentService)
AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intentToFire = new Intent(CAlarmReceiver.ACTION_CHECK_ALARM);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intentToFire, 0);
private void rescheduleAlarm() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, TIME_TO_REFRESH);
alarms.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), CHECK_TIME, alarmIntent);
}
Then in an Activity I have a button on upon it's click it calls this code
private OnClickListener btnCloseApplicationListener = new OnClickListener() {
public void onClick(View v) {
intentToCancel = new Intent(CAlarmReceiver.ACTION_CHECK_ALARM);
alarmIntent = PendingIntent.getBroadcast(v.getContext(), 0, intentToCancel, 0);
alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.cancel(alarmIntent);
finish();
}
};
After the finish() is executed I keep seeing the logs I have in DDMS window.
How can I cancel it?
Thanks in advance!
try using same context for the intent.
The intents have to match and I think your problem is that you are using different context when trying to cancel the alarm.
alarmIntent = PendingIntent.getBroadcast(this, 0, intentToCancel, 0);