Can I add alarm sound or ringtone to apk ?
I need to add to alarm sound to the application we developing.
Thanks!
Steps you need to do:
Add song that you want to add into res->raw folder.
Then in your activity write this:
Intent AlarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
AlarmIntent.putExtra("Ringtone",
Uri.parse("getResources().getResourceName(R.raw.shankh_final_mid)"));
PendingIntent Sender = PendingIntent.getBroadcast(this, 0, AlarmIntent, 0);
AlarmManager AlmMgr = (AlarmManager)getSystemService(ALARM_SERVICE);
AlmMgr.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() +
(60 * 1000), (24 * 60 * 60 * 1000), Sender);
After that in your reciever class add this line of codes:
public void onReceive(Context context, Intent intent) {
Intent in = new Intent(context, SnoozeEvent.class);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent Sender = PendingIntent.getActivity(context, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);
manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notification = new Notification(R.drawable.icon, "Wake up alarm", System.currentTimeMillis());
notification.setLatestEventInfo(context, "Hanuman Chalisa", "Wake Up...", Sender);
notification.flags = Notification.FLAG_INSISTENT;
notification.sound = Uri.parse("android.resource://my.package.name/raw/notification");
manager.notify(1, notification); }
Related
I have used alarm manager in Android for showing some notifications, but it is not working properly.
My code in activity is:
Intent i = new Intent(this, AlarmBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, i, 0);
AlarmManager alarmManager = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (int) (10*60 * 1000),
pendingIntent);
And the code for receiving a notification after 15 minutes is:
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getService(context, 0, intent, 0);
am.cancel(pi);
am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() +
(int) ( 5*60 * 1000), pi);
use this type of AlarmManager when you use service than you need to change getService insted of getBroadCast. sometime receiver not call and also creat issue in some devices when app in background
Intent intent = new Intent(myContext, SetAlarmService.class);
intent.putExtra("salah",i);
intent.putExtra("repeat",HowManyTimeRepeatAlarm);
PendingIntent pendingIntent = PendingIntent.getService(
myContext, i, intent, 0);
AlarmManager alarmManager = (AlarmManager) myContext.getSystemService(ALARM_SERVICE);
// alarmManager.cancel(pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, reset_cal.getTimeInMillis(),60000 * 2 ,
pendingIntent);
I am creating a notification on a activity called Mainmenu using alarm here is code
Intent myIntent = new Intent(MainMenu.this, SyncService.class);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(MainMenu.this, 1, myIntent,0);
Calendar calforAlram = Calendar.getInstance();
calforAlram.set(Calendar.HOUR_OF_DAY, 20);
calforAlram.set(Calendar.MINUTE, 46);
calforAlram.set(Calendar.SECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calforAlram.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);
alarmManager.cancel(pendingIntent);
On notification received , on click of the notification i have to open the same Mainmenu activity.
Problem: again one more notification is generated , again click on notification again Mainmenu activity will open and continues
Here is Notification code
NotificationManager mManager = (NotificationManager) getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),MAFLogonActivity.class);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle("XXXXXXXX")
.setContentText("Please sync data.").setAutoCancel(true);
intent1.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this.getApplicationContext(), 1, intent1, 0);
notification.setContentIntent(pendingNotificationIntent);
Log.d("On service", "Alarms set for everyday 2 pm.");
mManager.notify(0, notification.build());
return Service.START_NOT_STICKY;
You should check the application has started from push notification or normal.
How to do this.
Notification generate code
Intent intent1 = new Intent(this.getApplicationContext(),MAFLogonActivity.class);
intent1.putExtra("isFromNotification", true);
onCreate() of MAFLogonActivity.java
boolean isFromNotification = getIntent().getBooleanExtra("isFromNotification", false);
if(!isFromNotification){
Intent myIntent = new Intent(MainMenu.this, SyncService.class);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(MainMenu.this, 1, myIntent, 0);
Calendar calforAlram = Calendar.getInstance();
calforAlram.set(Calendar.HOUR_OF_DAY, 20);
calforAlram.set(Calendar.MINUTE, 46);
calforAlram.set(Calendar.SECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calforAlram.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);
alarmManager.cancel(pendingIntent);
}
Hope this would help you.
I'm new to this, so I'm a bit lost.
I have built a notification and passed it to the alarm manager as a pendingIntent, however instead of waiting the interval to trigger the alarm and showing the notification, the notification is instantly shown.
What have I done wrong, that isn't allowing the alarm to be set properly?
public class NotificationController{
Context context;
public void createNotification(Context context){
this.context = context;
Notification notification = getNotification();
//creates notification
Intent intent = new Intent(context, NotificationReceiver.class);
intent.putExtra(NotificationReceiver.NOTIFICATION_ID, 1);
intent.putExtra(NotificationReceiver.NOTIFICATION, notification);
PendingIntent pIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
//schedules notification to be fired.
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 10000 , pIntent);
}
private Notification getNotification(){
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification.Builder builder = new Notification.Builder(context)
.setContentTitle("Reminder")
.setContentText("Car service due in 2 weeks")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent);
return builder.build();
}
}
my receiver
public class NotificationReceiver extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(id, notification);
}
}
I have also registered with <receiver android:name=".NotificationReceiver" > in the AndroidManifest.
In this line
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 10000 , pIntent);
You specify that the alarm has to fire at 10 seconds after the device has been booted (which is in the past, so the alarm fires immediately). If you would want it 10 seconds after you set the alarm, you use the number of milliseconds since the device has been booted PLUS 10 seconds:
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis() + 10000 , pIntent);
Try this:
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis()+10000 , pIntent);
For me old methods are killing after few hours . Please see my code that i am using for different OS versions. May be helpful.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(alarmTime, pendingIntent);
alarmManager.setAlarmClock(alarmClockInfo, pendingIntent);
}
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
}
else {
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
}
Thanks
Arshad
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
and
Intent notifyIntent = new Intent(this, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, NotificationKeys.NOTIFICATION_ID, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar alarmStartTime = Calendar.getInstance();
alarmStartTime.set(Calendar.SECOND, 5);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmStartTime.getTimeInMillis(), pendingIntent);
If you want to set is at a excat time, you should use setExact. Unfortunalety there is no setExactRepating so you have to create this yourself. Schedule a new alarm after one executes or something like that
for more refer https://stackoverflow.com/a/30812993/8370216
I am working on an android application but the problem is that I have to alert user through a message on notification bar which will occur after ten(10) days ,like in candy crush game it gives alert of full lives after some hours. I am a beginner in android so I am confused about the implementation of this scenario. Please guide me of its implementation with code.
You can do so with the AlarmManager and the BroadcastReceiver
AlarmManager
Intent myIntent = new Intent(this, TimeAlarm.class);
pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.DAY_OF_MONTH, 10);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
BroadcastReceiver
public class TimeAlarm extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent paramIntent) {
// Request the notification manager
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Create a new intent which will be fired if you click on the notification
Intent intent = new Intent("android.intent.action.VIEW");
// Attach the intent to a pending intent
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Create the notification
Notification notification = new Notification(R.drawable.icon, "Something new!"), System.currentTimeMillis());
notification.setLatestEventInfo(context, "Something new!", "Description",pendingIntent);
// Fire the notification
notificationManager.notify(1, notification);
}
}
I am using AlarmManager and NotificationManager with BroadcastReceiver.
When I set an alarm with a specific date, it is working the first time. However, when I modify the date and click the confirm button, the alarm is working any date
immediately. I want to set the alarm interval day after expired date with fixed time.
What is the problem with it? I don't understand at the moment.
confirmButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
//set alarm with expiration date
am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
setOneTimeAlarm();
Toast.makeText(fridgeDetails.this, "Alarm automatic set",
Toast.LENGTH_SHORT).show();
setResult(RESULT_OK);
finish();
}
public void setOneTimeAlarm() {
c.set(Calendar.HOUR_OF_DAY, 14);
c.set(Calendar.MINUTE, 49);
c.set(expiredYear, expiredMonth, expiredDay);
Intent myIntent = new Intent(fridgeDetails.this, AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
fridgeDetails.this, 0, myIntent, PendingIntent.FLAG_ONE_SHOT);
am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
}
});
AlarmService.java
public class AlarmService extends BroadcastReceiver{
NotificationManager nm;
#Override
public void onReceive(Context context, Intent intent) {
nm = (NotificationManager) context.getSystemService(
Context.NOTIFICATION_SERVICE);
CharSequence from = "Check your fridge";
CharSequence message = "It's time to eat!";
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(), 0);
Notification notif = new Notification(R.drawable.ic_launcher,
"Keep Fridge", System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
notif.defaults |= Notification.DEFAULT_SOUND;
notif.flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(1, notif);
}
}
You need to set the property instead of FLAG_ONE_SHOT. This is for single alarm event not for the repeat. try this
PendingIntent pendingIntent = PendingIntent.getBroadcast(
fridgeDetails.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
see more detail about from here
Edit:
As you do for notification with PendingIntent
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,new Intent(), 0);
In this you pass the empty object of Intent you need to pass the class name for that when you click on notification which Activity will be launch like in Alarm set time you do
Intent myIntent = new Intent(fridgeDetails.this, AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
fridgeDetails.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
now just pass the Acitivity name in the intent like suppose you want to launch your home activity and activity name like "homeactivity"
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,new Intent(context,HomeActivity.class), 0);