I am calling a Alarm every 30 seconds. It is not being called. What am I doing wrong.
AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(this, OnAlarmReceiver.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent
.getBroadcast(this, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 30);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, cal.getTimeInMillis(), 1000 * 30, pi);
My OnAlarmReceiver
public class OnAlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// PullPendingRequests.acquireStaticLock(context);
Toast.makeText(context, "Don't panik but your time is up!!!!.", Toast.LENGTH_LONG).show();
Log.d("Taxeeta:PullPendingRequets", "CallService Location");
context.startService(new Intent(context, DriverService.class));
}
}
My Manifest contents for Alarm and Service
<service
android:name="com.taxeeta.DriverService"
android:enabled="true"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Light.NoTitleBar" />
<receiver
android:name="com.taxeeta.support.OnAlarmReceiver"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.NOTIFY" />
</intent-filter>
</receiver>
Fixed it by changing the code to, thanks to
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 5);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent notifyintent = new Intent(this, OnAlarmReceiver.class);
notifyintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notifyintent.setAction("android.intent.action.NOTIFY");
PendingIntent notifysender = PendingIntent.getBroadcast(this, 0, notifyintent,
PendingIntent.FLAG_UPDATE_CURRENT);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 20 * 1000,
notifysender);
Related
I'm developing an android application that have daily notification. I have a solution but not enough. My code working after restarting the phone. Users will not restart their devices unless they are required. My notifications will not appear until this time. How can I solve?
BootReceiver.java
public class BootReceiver extends BroadcastReceiver {
private AlarmManager alarmManager;
private PendingIntent pendingIntent;
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Device Booted", Toast.LENGTH_SHORT).show();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 19);
calendar.set(Calendar.MINUTE, 20);
calendar.set(Calendar.SECOND, 0);
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(context, 1, alarmIntent, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
}
}
Manifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver
android:name=".AlarmReceiver" />
<receiver
android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
AlarmReceiver.java -> just notification builder
I need to start alarm at every 30 seconds, I need it to be activated without running the app. But whether the app runs or not the AlarmReceiver do not get called. Any suggestions?
start method is in MainActivity.java class
public void start() {
Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.SECOND, 30);
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent pintent = PendingIntent.getService(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pintent);
Log.d("alarm","alarm set for alarm receiver");
}
My Receiver file
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Toast.makeText(context,"Alarm Receiver ",Toast.LENGTH_SHORT).show();
Log.d("Alarm","Alarm receive");
}
}
Manifest File:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity" android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".GetLocationService"/>
<receiver android:name=".AlarmReceiver" android:enabled="true"/>
</application>
here i am able to set alarm, but i didn't receive alarm
In this page there is a finished example of what you need:
https://www.thepolyglotdeveloper.com/2014/10/use-broadcast-receiver-background-services-android/
Apparently you have to change the line:
PendingIntent pintent = PendingIntent.getService(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
To
PendingIntent pintent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Change your start method to this:
public void start() {
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
int interval = 30 * 1000; // 30 seconds of interval.
manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}
Let me know if works.
I ran into this issue recently. The problem was with my custom Parcelable object, using primitives instead worked fine. This answer helped me: Sending Extras to onReceive only retrieves ALARM_COUNT
I am building an android app to display recurring scheduled notification at a specified time of the day.
For this I have created broadcastReceiver using following code:
public class ScheduleNotification extends BroadcastReceiver {
public static final int NOTIFICATION_ID = 1;
#Override
public void onReceive(Context context, Intent intent) {
long when = System.currentTimeMillis();
MainActivity mainActivity = new MainActivity();
String _pasuram_number = mainActivity.get_pasuram_number();
String[] _pasuram_str = mainActivity.get_dd_text(_pasuram_number).split(",");
Log.d("VC", "Notification intent paasuram " + _pasuram_number);
intent.putExtra("pasuramnumber", _pasuram_number);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent snoozeIntent = new Intent(context, MainActivity.class);
PendingIntent piSnooze = PendingIntent.getService(context, 0, snoozeIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setSmallIcon(R.drawable.ic_stat_name);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_stat_name));
builder.setContentTitle("Title of the notification");
builder.setContentText(_pasuram_str[9]+"-"+_pasuram_str[11]);
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(_pasuram_str[0] + "-" + _pasuram_str[8]));
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
}
In the MainActivity added code to create alarm
private void createScheduledNotification(int days)
{
// Get new calendar object and set the date to now
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
// Add defined amount of days to the date
calendar.set(Calendar.HOUR_OF_DAY, 6);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
//calendar.add(Calendar.HOUR_OF_DAY, days * 24);
// Retrieve alarm manager from the system
AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(getBaseContext().ALARM_SERVICE);
// Every scheduled intent needs a different ID, else it is just executed once
int id = (int) System.currentTimeMillis();
// Prepare the intent which should be launched at the date
Intent intent = new Intent(this, ScheduleNotification.class);
// Prepare the pending intent
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Register the alert in the system. You have the option to define if the device has to wake up on the alert or not
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
//alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
The code compiles fine and application runs, but scheduled notification does not appear as expected.
In the manifest file added receiver as follows:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vaishnavism.eclass.dinamorudivyaprabandam" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<!-- permission required to use Alarm Manager -->
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<!-- Register the Alarm Receiver -->
<receiver android:name="com.vaishnavism.eclass.dinamorudivyaprabandam.ScheduleNotification"/>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
android:label="#string/title_activity_settings"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.vaishnavism.eclass.dinamorudivyaprabandam.MainActivity" />
</activity>
</application>
</manifest>
Any help to resolve the issue is greatly appreciated.
Thanks
Try to use set your alarm like below :
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 6);
calendar.set(Calendar.MINUTE, 0);
int interval = 1000 * 60 * 60 * 24;
Intent myIntent = new Intent(yourActivity.this, ScheduleNotification .class);
pendingIntent = PendingIntent.getBroadcast(UserDashBoardActivity.this, 0, myIntent,0);
/* Repeating on every 24 hours interval */
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval, pendingIntent);
And you need to start an Alarm When the Device Boots.
For more info refere here.
Hope it will help you.
Here is my code below, what is the problem? If i look at log, there is no start of AlarmReceiver class. So no notification is sent to user. I tried every minute in loop and still nothing happened. What am i doing wrong?
Thanks in Advance!!
public void setRepeatingAlarm(int hour, int min)
{
Intent myIntent = new Intent(this , AlarmReceiver.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, min);
calendar.set(Calendar.SECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);
}
//Receiver Class AlarmReceiver
public void onReceive(Context context, Intent intent)
{
taskDb = new TaskDb(context);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
String temp ="";
for(String s: taskDb.selectTodayTasks())
{
temp +="-" + s + "\n";
}
if(!temp.equals("")) {
Notification n = new Notification.Builder(context)
.setContentTitle(context.getString(R.string.today_tasks))
.setContentText(temp)
.setLights(Color.CYAN,500,500)
.setContentIntent(pIntent)
.setPriority(Notification.PRIORITY_MAX)
.setAutoCancel(true).build();
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(MainActivity.NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
}
}
//android manifest
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".main.MainActivity"
android:windowSoftInputMode="stateHidden"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".background.AlarmReceiver" android:enabled="true">
</receiver>
</application>
Change PendingIntent.getService to PendingIntent.getBroadcast:
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, 0);
Because PendingIntent.getService is used to Retrieve a PendingIntent that will start a service,... but PendingIntent.getBroadcast for Retrieve a PendingIntent that will perform a broadcast,...
I have looked everywhere for this solution most of the replies ask to set alarm in boot receiver.
i have implemented a boot receiver and started a service and set a alarm using set method.
Service is started fine but alarm is not setting.
please help i am stuck on that.
i can post some also if you want but boot receiver is working fine as i am also starting service in that
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
SharedPreferences sharedPref = context.getSharedPreferences(
Util.SHARED_PREF_NAME, Context.MODE_PRIVATE);
long curTime = System.currentTimeMillis();
long endTime = sharedPref.getLong(Util.END_TIME, -1);
long startTime = sharedPref.getLong(Util.START_TIME, -1);
if (curTime < endTime && startTime >= curTime) {
Intent intent1 = new Intent(context, HUD.class);
PendingIntent pintent = PendingIntent.getService(context, 1987,
intent1, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarm = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, endTime, 100, pintent);
Log.e("alaram set", endTime + " " + curTime);
}
Intent service = new Intent(context, HUD.class);
context.startService(service);
}
}
MyReceiver.java
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Manifest.xml
Service started successfully after boot
but alarm.setRepeating(AlarmManager.RTC_WAKEUP, endTime, 100, pintent);
alarm is not working.
I think i clear my question.
Plz help
Intent intent1 = new Intent(context, HUD.class);
PendingIntent pintent = PendingIntent.getService(context, 1987,
intent1, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarm = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 30);
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), 100, pending);
Try your setInexactRepeating instead of setRepeating that is more performance optimized method.