I have to show notification to user at user's selected time and date and my code is below.
public static void setAlarm(Context context, long id, int rYear, int rMonth, int rDay, int rHour, int rMinute) {
if (Share.wantReminder) {
Share.wantReminder = false;
Calendar calendar = new GregorianCalendar(rYear, rMonth, rDay, rHour, rMinute);
Calendar calendar1 = new GregorianCalendar();
calendar1.setTimeInMillis(System.currentTimeMillis());
calendar1.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
calendar1.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
calendar1.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH));
calendar1.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
calendar1.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE));
Intent intent = new Intent(context, ReminderService.class);
intent.setAction(String.valueOf(id));
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), pendingIntent);
Toast.makeText(context, "Alarm set", Toast.LENGTH_SHORT).show();
}
}
My Manifest.xml
<receiver android:name=".service.ReminderService">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
No notification are comes to actionbar
What should I do?
Please share ReminderService.class
you must have the notification settings complete to trigger this place.
<receiver android:name=".ReminderService">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
My method is triggered here and it doesn't accept service per
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
Make below changes to your receiver in Manifest.xml
<receiver
android:name=".service.ReminderService"
android:process=":remote"
android:excludeFromRecents="true" />
Create and display notification inside onReceive() method of ReminderService class as shown below.
public class ReminderService extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
//Put your code here to create notification
wl.release();
}
}
Add below permission in your Manifest.xml
<uses-permission android:name="android.permission.WAKE_LOCK" />
Related
Needed: Launch application using alarm manager when alarm trigger it launch the application.
Tried:
BroadCast Receiver for receive alarm trigger and launch application but not launching the application.
#Override
public void onReceive(final Context context, Intent intent) {
Intent myIntent = new Intent(context, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
Setting Alarm:
public void startAlarm(Context context) {
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, RestartServiceBroadcastReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(), AlarmManager.INTERVAL_HOUR, alarmIntent);
}
Manifest:
<receiver android:name="com.android.cchhaatt.RestartServiceBroadcastReceiver"
android:enabled="true"
android:exported="true"
android:label="StartMyServiceAtBootReceiver"
android:process=":remote">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
I have used broadcast receiver with alarm manager to hit webservices for every 60 seconds,seems its working fine when i have used my broadcast receiver in different class but its not calling when i declare a receiver with in my activity
my code is below
public static void startAlarm(Context context) {
Intent locationAlarm = new Intent(context, GetLocation.class);
AlarmManager alarms = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Calendar updateTime = Calendar.getInstance();
PendingIntent recurringAlarm = PendingIntent.getBroadcast(context, 0,
locationAlarm, PendingIntent.FLAG_CANCEL_CURRENT);
alarms.setRepeating(AlarmManager.RTC_WAKEUP,
updateTime.getTimeInMillis() + 500, UPDATE_INTERVAL,
recurringAlarm);
}
Broadcast receiver :
public class GetLocation extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
LogUtil.d("Started Service");
}
}
My Manifest file :
<receiver android:name="com.sample.sample.listeners$GetLocation" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
kindly correct my mistake. my Started service method never called
You have a typo in your manifest file. You have put $GetLocation instead of .GetLocation
<receiver android:name="com.sample.sample.listeners.GetLocation" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I am trying to setup an alarm at a specified time, but it is not being caught in my reciver.
Setup:
Intent intent = new Intent(this, ActionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar current = Calendar.getInstance();
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, (current.getTimeInMillis() + 60000),3600000, pendingIntent);
Here is my reciver:
public class ActionReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras(); //breakpoint here that doesn't get triggered
}
}
I have put these values in my manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<receiver android:name="com.project.ActionReceiver" android:enabled="true" />
Not sure what is wrong... thanks!
Finally got the receiver to fire! I added the following code to my manifest:
<receiver
android:name="com.project.ActionReceiver"
android:exported="true" >
<intent-filter>
<action android:name="com.project.ActionSetter" >
</action>
</intent-filter>
</receiver>
Found here with details: https://stackoverflow.com/a/16119351/1174574
The name of receiver in your manifest should be class name, such as:
<receiver android:name="com.project.ActionReceiver">
BTW, set an action is a better practice.
Intent intent = new Intent(this, ActionReceiver.class);
intent.setAction("com.project.action.ALERM");
And in the manifest
<receiver android:name="com.project.ActionReceiver">
<intent-filter>
<action android:name="com.project.action.ALERM"/>
</intent-filter>
</receiver>
Try changing the android:name attribute of your receiver to the fully qualified class name of your ActionReceiver. Something like:
<receiver android:name="com.project.ActionReceiver" android:enabled="true" />
I am really new to android, I have been researching about alarms. I want to alarm if there is a birthday on that day. I've have used alarm manager. I was confused because i have read that it clears after reboot. I don't have an android phone so I'm just using the emulator.
Here's my code :
public void schedAlarm() {
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, AlarmService.class);
pendingIntent = PendingIntent.getBroadcast(this, contact.id, intent, PendingIntent.FLAG_ONE_SHOT);
am.setRepeating(AlarmManager.RTC, timetoAlarm, nextalarm, pendingIntent);
}
I made this BroadcastRecever in replace for AlarmSerivce
Here :
public void onReceive(Context context, Intent intent) {
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "It Birthday!";
CharSequence message =" Greet your friend.";
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
Notification notif = new Notification(R.drawable.ic_launcher, "Birthday", System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
nm.notify(1, notif);
}
is this enough??
A simple answer would be NO. But yes you can achieve this by creating a BroadCastReceiver which will start the Alarm while booting completes of the device.
Use <action android:name="android.intent.action.BOOT_COMPLETED" /> for trapping boot activity in BroadCastReceiver class.
You need to add above line in AndroidManifest.xml as follows,
<receiver android:name=".AutoStartUp" android:enabled="true" android:exported="false" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Yes , you can make AlarmManager to work even after rebooting.
Perhaps this is the easiest way : add the below code in your AndroidManifest.xml:
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
don't forget to include user-permission to the AndroidManifest.xml as:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
in some phones only adding
<action android:name="android.intent.action.Boot_COMPLETED" />
does not work you also have to add
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
along with previous one
I have written next broadcast receiver for BOOT_COMPLETED action
public class FineWeatherBootStarter extends BroadcastReceiver {
PendingIntent pendingIntent = null;
public void onReceive(Context context, Intent intent) {
long firstTime = System.currentTimeMillis();
Intent serviceIntent = new Intent(FineWeather.ACTION_REFRESH);
intent.setType(Weather.CONTENT_TYPE);
pendingIntent = PendingIntent.getService(context, 0,serviceIntent, 0);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC,
firstTime, 30*1000, pendingIntent);
Toast.makeText(context, "STARTED!!!!!!!!!", 5000).show();
}
}
I can see "STARTED!!!!!!!!!" message on boot device, but my service seems like not being started every 30 seconds
Where can be a probleb&
Try altering your manifest
<receiver android:name="MyBootReceiver" android:process=":hascode_process">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>