I want my alarm manager to be set for a certain time to change the phone background, but the broadcast receiver isn't doing anything. Can you help tell me why?
Scheduling alarm:
public void scheduleAlarm(Context context){
Intent intent = new Intent(context, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 18);
calendar.set(Calendar.MINUTE, 34);
am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
am.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent );
Toast.makeText(context, "Alarm set", Toast.LENGTH_LONG).show();
}
Broadcast Receiver:
#Override
public void onReceive(Context context, Intent intent){
PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
GrilledCheeseLookup.getGrilledCheeseJSON(grilledCheeseUrls, context);
Toast.makeText(context, "Alarm worked", Toast.LENGTH_LONG).show();
}
Enabling in manifest:
<receiver android:name=".AlarmReceiver" android:enabled="true" />
Try creating your Intent while also specifying the packageContext, using the Intent/4 constructor (you can set the uri to null).
Related
The following is the code for an alarm that has to hit the BroadCast Receiver :
Intent intentWithData = new Intent(context, TokenActivity.class);
intentWithData.putExtra(Constants.ID,id);
intentWithData.putExtra(Constants.POSITION, finalI);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 007, intentWithData, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pendingIntent);
The code for the Broadcast receiver is
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class TokenBroadcastReceiver extends BaseBroadCastReceiver {
String Id;
int position;
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Create a toast", Toast.LENGTH_SHORT).show();
}
}
The manifest is :
<receiver android:name=".broadcastReceiver.TokenBroadcastReceiver"/>
The toast is not showing up. Where am I going wrong with this code?
You're mixing 2 things.
If you want your receiver to get the intent:
Intent intentWithData = new Intent(context, TokenBroadcastReceiver.class);
intentWithData.putExtra(Constants.ID,id);
intentWithData.putExtra(Constants.POSITION, finalI);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 7, intentWithData, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pendingIntent);
if you want your activity to get the intent:
Intent intentWithData = new Intent(context, TokenActivity.class);
intentWithData.putExtra(Constants.ID,id);
intentWithData.putExtra(Constants.POSITION, finalI);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 7, intentWithData, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pendingIntent);
Plus, make sure your receiver is registered in your AndroidManifest.xml
You are setting the pending intent to open an activity as per your code
Intent intentWithData = new Intent(context, TokenActivity.class);
and displaying the toast in broadcast receiver. Please correct your code and it will start working.
Intent intentWithData = new Intent(this, TokenBroadcastReceiver.class);
intentWithData.putExtra("id",5);
intentWithData.putExtra("position", 4);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 007, intentWithData, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pendingIntent);
Don't forget to register your broadcast in manifest
<receiver android:name=".broadcastReceiver.TokenBroadcastReceiver"/>
I am trying to implement repeating multiple alarms at different specific times.
The problem is the Alarm Receiver is not getting invoked at any given time.
My code in the activity:
private void setAlarms() {
Intent myIntent=new Intent(this, AlarmReceiver.class);
myIntent.putExtra("MedName",medication_name);
myIntent.setAction("b5.project.medibro.receivers.AlarmReceiver");
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//am.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();
int counter=0;
for(String timer: timers){
//timers is an array of Strings in the format hh:mm
try {
String[] comps=timer.split(":");
Calendar cal=Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, Integer.valueOf(comps[0]));
cal.set(Calendar.MINUTE, Integer.valueOf(comps[1]));
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Log.d(TAG,comps[0]+" "+comps[1]+ "Alarm Time: " + cal.getTime().toString());
PendingIntent pendingIntent = PendingIntent.getService(this, counter, myIntent,0);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(),
AlarmManager.INTERVAL_DAY,
pendingIntent);
counter++;
} catch (ParseException e) {
e.printStackTrace();
Log.d(TAG,"Time Parsing error: "+e.getMessage());
}
}
}
and this is my Alarm Receiver:
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("b5.project.medibro.receivers.AlarmReceiver")) {
Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
Log.d("Alarm Receiver", "Alarm is invoked. ");
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setSmallIcon(R.drawable.messenger_bubble_large_blue);
builder.setContentTitle("Time to take your medicine");
String medName = intent.getStringExtra("MedName");
String text = "Medicine name: " + medName;
builder.setContentText(text);
Notification notification = builder.build();
NotificationManagerCompat.from(context).notify(0, notification);
Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
}
}
}
Manifest declaration:(its within application tags)
<receiver android:name="b5.project.medibro.receivers.AlarmReceiver"
android:enabled="true">
<intent-filter>
<action android:name="b5.project.medibro.receivers.AlarmReceiver"/>
</intent-filter>
</receiver>
I feel the above code should execute the broadcast receiver atleast once but nothing shows up in the logcat.
Can someone tell me what am I missing?
Instead of getService try getBroadcast:
PendingIntent pendingIntent = PendingIntent.getService(this, counter, myIntent,0);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, counter, myIntent,0);
Also look at http://developer.android.com/reference/android/app/PendingIntent.html#getBroadcast(android.content.Context, int, android.content.Intent, int), and figure out the flags you need, 'cause I wouldn't be surprised if only the last alarm (in the for loop) will be working.
I have this in my activity :
AlarmManager alarmMgr;
PendingIntent alarmIntent;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 20);
calendar.set(Calendar.SECOND, 00);
Intent intent = new Intent(this, AlarmBroadcastReceiver.class);
alarmIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 123456, intent, 0);
alarmMgr = (AlarmManager) this
.getSystemService(Context.ALARM_SERVICE);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
alarmIntent);
Toast.makeText(this, "Alarm added", Toast.LENGTH_SHORT).show();
And this is my broadcast receiver class :
#Override
public void onReceive(Context context, Intent intent) {
Intent mIntent = new Intent(context, AlarmMessageActivity.class);
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mIntent);
Toast.makeText(context, "Alarm", Toast.LENGTH_SHORT).show();
Vibrator vibrator = (Vibrator) context
.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
}
I have also added these to the manifest file
<activity
android:name=".AlarmMessageActivity"
android:label="#string/title_activity_alarm_message" >
</activity>
<receiver android:name=".AlarmBroadcastReceiver" >
</receiver>
The AlarmMessageActivity class is just a simple page to display a text.
The above code works for sdk 8. But for Kitkat it does not work and i am also not getting any errors while setting the alarm or when the broadcast receiver is executed. I want to have the alarm working for all versions, my min sdk is 8.
Can anybody tell me where i am possibly wrong. I am new to android.
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.
I am trying this code to popup the alarm message. Its working when launching or opening the app, but it doesn't say any popup message while outside of the app.
I am so confused, i don't know what i am doing wrong.
String alarmtime = cur.getString(cur.getColumnIndex(DBDATA.LG_ALARMTIME));
//Reminder
String[] timesplit = alarmtime.split(":");
int hour = Integer.parseInt(timesplit[0]);
int minute = Integer.parseInt(timesplit[1]);
System.out.println(hour);
System.out.println(minute);
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, ShortTimeEntryReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar alarm = new GregorianCalendar();
alarm.setTimeInMillis(System.currentTimeMillis());
alarm.set(Calendar.HOUR_OF_DAY, hour);
alarm.set(Calendar.MINUTE, minute);
alarm.set(Calendar.SECOND, 0);
System.out.println(System.currentTimeMillis());
System.out.println(alarm.getTimeInMillis());
if (System.currentTimeMillis() > alarm.getTimeInMillis()){
alarm.setTimeInMillis(alarm.getTimeInMillis()+ 24*60*60*1000);// Okay, then tomorrow ...
alarmMgr.set(AlarmManager.RTC_WAKEUP, alarm.getTimeInMillis(),pendingIntent);
}
else
{
alarmMgr.set(AlarmManager.RTC_WAKEUP, alarm.getTimeInMillis(),pendingIntent);
}
I need to pop up the alarm message outside of the app(i.e) exactly like the alarm does.
Thanks for your help guys,
You probably need a BroadcastReceiver.
As you can read in this question : BroadcastReceiver not receiving an alarm's broadcast
You have to build the intent like this :
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(this, "CHECK_ALARM_CODE", alarmIntent, 0);
And receive the alarm like this :
public class AlarmReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
Log.d("OK", "AlarmReceiver.onReceive");
}
}
Don't forget to register your broadcast in your manifest file.