I would like to know - how to show pop up window when AlarmManager will call? I've already created AlarmManager now I need to create something what will show popup window to cancel this Alarm.
My code:
public void setAlarm(long timeInMillis){
if(Build.VERSION.SDK_INT >= 23){
mCalendar.set(
mCalendar.get(Calendar.MONTH),
mCalendar.get(Calendar.YEAR),
mCalendar.get(Calendar.DAY_OF_YEAR),
mCalendar.get(Calendar.HOUR_OF_DAY),
mCalendar.get(Calendar.MINUTE)
);
}
final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, MyAlarm.class);
intent.setData(currentUri);
final PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
alarmManager.setExact(AlarmManager.RTC, timeInMillis, pendingIntent);
}
and
public class MyAlarm extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
MediaPlayer mediaPlayer = MediaPlayer.create(context, Settings.System.DEFAULT_RINGTONE_URI);
mediaPlayer.start();
}
}
onReceive(context, intent) {
/*show the dialog in this method. set the onclick so it can dismiss the alarm,
get the value for the alarm from the bundle. I may be wrong about this
but i think alarmManager has a cancel(PendingIntent operation) method that u can
just send in the intent and your done.
Call a stopMedia(context) method after the cancel in order to stop the media
that is playing
*/
showDialog(context, intent)
//Extract the play media code to a method for readability
playMedia(context)
}
That should solve your problem
Before the code was posted:
We can either use the pending intent and have an activity that handles the pending intent. or use the handler to execute the code.
In either case, create a dialog fragment and then use the appropriate context to show it. setOnClickListener { alarmManager.cancel } for the dialog fragment button.
A little more explanation may be required depending on how the alarm manager is setup
Related
I am getting a problem with my reminder APP.
When I add first reminder broadcast is received once for second time broadcast is received twice for third time it is received thrice.
I tried many different solutions on StackOverfolow but none of them are working
Kindly help me out with detailed answer.
Link to Project
. code is given below:
Function to set Reminder:
public void startAlarm(Calendar c) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlertReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, number, intent, PendingIntent.FLAG_ONE_SHOT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
}
Class receiving Broadcast:
public class AlertReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, intent.getAction(), Toast.LENGTH_LONG).show();
MediaPlayer mediaPlayer = MediaPlayer.create(context, Settings.System.DEFAULT_NOTIFICATION_URI);
mediaPlayer.start();
}
}
I see that you are setting the alarm inside Adapters onBindViewHolder. This is not the right place to setting alarm. Because when you call notifyDataSetChanged it will call onBindViewHolder again and it will set same alarm over and over.
I am currently trying to make an alarm app using android studio and I am having trouble making the dismiss alarm button.
I am passing the request code of the pending intent, when it is created, to my Alarm class which has inherited the Broadcast Reciever which is again sending the request code to a new activity Alarm Page where you can dismiss the Alarm. And using this request code I am creating a pending intent and I am trying to cancel the alarm using this pending intent as it should be the same pending intent I have created(they have the same request code)
boolean result=databaseHelper.addAlarm(fh+":"+fm);
int requestCode=databaseHelper.returnLastId();
AlarmManager alarmManager=(AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(getApplicationContext(),Alarm.class);
i.putExtra("rc",requestCode);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(),requestCode,i,0); alarmManager.setExact(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pi);
int id=intent.getIntExtra("rc",-1);
MediaPlayer mediaPlayer=MediaPlayer.create(context, Settings.System.DEFAULT_ALARM_ALERT_URI);
mediaPlayer.start();
Intent intent1=new Intent(context,AlarmPage.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent1.putExtra("id",id);
context.startActivity(intent1);
Intent intent=getIntent();
id=intent.getIntExtra("id",-1);
Log.d("ID",id+"");
Button dismiss=(Button)findViewById(R.id.dismiss);
dismiss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("button","clicked");
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(getApplicationContext(), Alarm.class);
PendingIntent pi = (PendingIntent) PendingIntent.getBroadcast(getApplicationContext(), id, i, 0);
alarmManager.cancel(pi);
}
});
After pressing the dismiss button I was expecting the Alarm to cancel, but nothing happens, the alarm just keeps ringing. My best guess was that it is somehow maybe creating a new pending intent but my request code is the same as my original pending intent.
I'm trying to make an app that schedules an alarm at a certain time in which the user is suppose to take their medication. I have done everything that most guides and the documentation say it's required to setup the alarm. Here is the method I call when the user presses the button:
private void scheduleAlarm() {
Intent intentAlarm = new Intent(this, AlarmReceiver.class);
PendingIntent pIntent = PendingIntent.getBroadcast(this, alarmeID, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmeTime.getTimeInMillis(), pIntent);
}
Here is the AlarmReceiver.class
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone ringtone = RingtoneManager.getRingtone(context, notification);
ringtone.play();
}
}
and I have put these two lines in the manifest:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<receiver android:name=".services.AlarmReceiver"/>
And yet, my application is not working. The alarm doesn't go off. What am I doing wrong? What else is needed?
You are currently playing alarm ringtone from BroadcastReceiver. Instead of doing that, you can open an Activity and start playing alarm tone from there. You can use stop() method to stop it (may be from button click within the activity).
BroadcastReceiver
#Override
public void onReceive(Context context, Intent intent) {
//start activity
Intent i = new Intent(CurrentActivity.this, NextActivity.class);
context.startActivity(i);
}
NextActivity.class
//Start your ringtone here
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone ringtone = RingtoneManager.getRingtone(context, notification);
ringtone.play();
//Somewhere inside button click
{
ringtone.stop();
}
Just copy pasting this won't work. I believe you can take concept from
here and implement as per your requirement.
And currently there is no any related code that will stop your ringtone.
Okay I am not really sure wither you are asking how to stop the alarm ? or that the alarm is not working at all after the user presses the button but anyhow.
I replaced The alarm with a repeated alarm and launched it using onCreate method and I closed the app and waited for the alarm to go off and it did!
And here is my code just in case:
private void scheduleAlarm() {
Intent intentAlarm = new Intent(this, AlarmReceiver.class);
PendingIntent pIntent = PendingIntent.getBroadcast(this, uniqueID, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// alarmManager.set(AlarmManager.RTC_WAKEUP, 10000, pIntent);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 1000, 1000, pIntent);
}
Everything else I copied and pasted from your code above.
Edit: If you are seeking the repeated alarm then this the page to go to:
https://developer.android.com/training/scheduling/alarms.html
note: my virtual device API is 24
Change the code as below -
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1000*60, pIntent);
May be your alarmeTime.getTimeInMillis() is not properly set. You can try the above line. It fires an alarm after 1 min.
I want to send notification by BroadcastReceiver. Notification sent by AlarmReceiver but again sent notification when i clicked the notification. The same thing happens again, again and again like endless loop
Here is my AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
android.support.v7.app.NotificationCompat.Builder builder = new android.support.v7.app.NotificationCompat.Builder(context);
builder.setSmallIcon(R.mipmap.ic_launcher);
// This intent is fired when notification is clicked
Intent i = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i, 0);
// Notifcation notify sound
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// Set the intent that will fire when the user taps the notification.
builder.setContentIntent(pendingIntent);
// Large icon appears on the left of the notification
builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher));
// Content title, which appears in large type at the top of the notification
builder.setContentTitle("Have a good weekend");
//Notification click after clear notification
builder.setAutoCancel(true);
//Set notification sound
builder.setSound(alarmSound);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Will display the notification in the notification bar
notificationManager.notify(1, builder.build());
}
}
and MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setAlarm();
}
private void setAlarm(){
AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(MainActivity.this, MyAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.DAY_OF_WEEK,6);
calendar.set(Calendar.HOUR_OF_DAY,16);
calendar.set(Calendar.MINUTE,53);
calendar.set(Calendar.SECOND,0);
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
}
}
As on click on notification it open MainActivity and in onCreate method of this activity you are calling setAlarm() . So on click of notification onCreate method is invoked then setAlarm() is invoked , this again set Alarm and build notification.
Do following changes
call setAlarm() onClick of a button, so that it is not invoked automatically onCreate of activity.
If you want to send notification automatically
change the intent of notification
Intent i = new Intent(context, MainActivity.class);
As of now you are using intent to open ManinActivity on click of notification.
Change Intent to
Intent i = new Intent(context, OtherActivity.class);
OtherActivity is the activity which does not setAlarm() or build notification in onCreate method.
Alternative Method
Use sharedPreferences to check whether the notification is build once or not.
if build once then don't call setAlarm()
I am developing a simple android app that manages events(An event contains a name, a description, and a date). For example: Somebody's birthday or something like that.
I want a canvas to pop up by itself at the specified date(even if I'm not in the application). I managed to create a Toast using AlarmManager and BroadcastReceiver which pop ups automatically at the specified date. But I can only write text to a Toast.
Is there any way to create a canvas that will pop up similar to a Toast?
My alarm manager:
AlarmManager alarms = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
IntentFilter filter = new IntentFilter("ALARM_ACTION");
registerReceiver(receiver, filter);
Intent intent = new Intent("ALARM_ACTION");
intent.putExtra("param", "It's your mom's birthday!");
PendingIntent operation = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarms.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+1000,operation) ;
And my Receiver class:
public class Receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//I want to replace the Toast with a canvas.
Toast.makeText(context, intent.getStringExtra("param"), Toast.LENGTH_SHORT).show();
}
}