I can't add an Alarm Clock for intent error - android

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);

Related

Alarm Manager is not working in Android

I need to setup an Alarm in some interval of times. To achieve it I wrote:
TestFragment class
private void setupAlarmManager(){
AlarmManager manager = manager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(getContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), i, alarmIntent, PendingIntent.FLAG_ONE_SHOT);
manager.set(AlarmManager.RTC_WAKEUP,1499510100000L, pendingIntent);
manager.set(AlarmManager.RTC_WAKEUP,1499510220000L, pendingIntent);
}
AlarmReceiver class
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
}
}
I put the debug point at Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show(); but nothing happened.
4PM = 1499510100000L
4:03PM = 1499510220000L
What Am I doing wrong here? Further I want to add a Local notification in onReceive method.
You should use a Calendar object to set up the alarm time.
For an alarm at 4PM you could do something like:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 16);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND,0);
Then you set up your AlarmManager as you already did but for the last line use:
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Maybe you want to use a repeating Alarm or even a Handler
If so you should visit: https://developer.android.com/training/scheduling/alarms.html

Why Alarm Manager Not Execute task at Midnight even the code was right?

Scenario :
My task will executed every midnight using AlarmManager, let's say at 00:00:00 and should be repeated everyday
i use the following code :
Calendar setCalendar = Calendar.getInstance();
setCalendar.set(Calendar.HOUR_OF_DAY, 0);
setCalendar.set(Calendar.MINUTE,0);
setCalendar.set(Calendar.SECOND,0);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(this, AlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC, setCalendar.getTimeInMillis(), 1000 * 60 *60 *24, pi);
I also did this alarmManager.setInexactRepeating(AlarmManager.RTC, setCalendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi); but the result still same (not execute every midnight)
The receiver :
public static class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//execute my task
}
}
Android Manifest :
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<receiver android:name="com.projectx.activity.MainActivity$AlarmReceiver"/>
The code above didn't do anything even midnight has passed. Is there any wrong with my code? please help.
And also sometimes the alarm executed every several second after i set the alarm (not only at midnight)
I have no idea, what causes it.
try this:
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent1 = new Intent(context, TestNotifyService.class);
PendingIntent alarmIntent = PendingIntent.getService(context, 0, intent1, 0);
long _Nalarm;
Calendar now = Calendar.getInstance();
Calendar wakeupcall = Calendar.getInstance();
wakeupcall.setTimeInMillis(System.currentTimeMillis());
wakeupcall.set(Calendar.HOUR_OF_DAY, 21);
wakeupcall.set(Calendar.MINUTE, 59);
if (wakeupcall.getTimeInMillis() <= now.getTimeInMillis())
_Nalarm=wakeupcall.getTimeInMillis() + (AlarmManager.INTERVAL_DAY+1);
else
_Nalarm=wakeupcall.getTimeInMillis();
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, _Nalarm,AlarmManager.INTERVAL_DAY, alarmIntent);
}
Try the below code
Calendar setCalendar = Calendar.getInstance();
setCalendar.set(Calendar.HOUR_OF_DAY, 0);
setCalendar.set(Calendar.MINUTE,0);
setCalendar.set(Calendar.SECOND,0);
setCalendar.add(Calendar.DATE, 1);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(this, AlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC, setCalendar.getTimeInMillis(), 1000 * 60 *60 *24 , pi);
And the receiver:
public static class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//execute my task
Toast.makeText(context, " hello", Toast.LENGTH_SHORT).show();
}
}
Let me know if any issues.

how to start B class at every specific time on android?

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
}
}

AlarmManager Broadcast Receiver

I'm making an app which uses an AlarmManager and a Broadcast Receiver to generate a notification everyday at 8:20 AM.
One issue which I'm facing is that every time I open the app after 8:20 AM, the notification is generated.
I guess this is because I have called the receiver in the onCreate() method.
Is there a way to make sure that the receiver is registered only once?
Here is my code :
public class MainActivity extends AppCompatActivity
{
AlarmManager alarmManager;
private PendingIntent alarmIntent;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(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, 00);
calendar.set(Calendar.MINUTE,30);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
}
public class AlarmReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context,"Alarm Raised",Toast.LENGTH_SHORT).show();
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent1 = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
PendingIntent pendingIntent=PendingIntent.getActivity(context,0,intent1,0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setSmallIcon(R.drawable.ic_account_circle_black_18dp);
builder.setContentTitle("Tracker");
builder.setContentText("Turn on Gps");
builder.setPriority(Notification.PRIORITY_MAX);
builder.setDefaults(Notification.DEFAULT_SOUND);
builder.setLights(0x0000FF,3000,2000);
builder.setContentIntent(pendingIntent);
notificationManager.notify(56, builder.build());
}
}
The second parameter of setInexactRepeating indicates that when should the AlarmManager start its task. So if you give it a time in past it will fire once immediately. You must check if the calendar.getTimeInMillis() is before now and add a day to it if so.
So you should simply change this part of your code
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
to this:
long timeToStart = calendar.getTimeInMillis();
if(System.currentTimeMillis() < timeToStart){
timeToStart += 24 * 60 * 60 * 1000; // one day
}
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, timeToStart, AlarmManager.INTERVAL_DAY, alarmIntent);

Android alarm and broadcast

I have defined my Android alarm and BroadcastReceiver as follows. My hope was that I want the alarm to go off two minutes later and every 15 minutes subsequently. This does not seem to be happening. Why is this?
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) + 2);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(getApplicationContext(), DailyNotificationReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pi);
public class DailyNotificationReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("received", "received");
}
}
As discussed in the comments, the BroadcastReceiver was not registered.
Please register the BroadcastReceiver in the manifest

Categories

Resources