Receiver not calling in Alarm Manager android - android

Hi I need to use notification for particular date in android. So I used alarm manager to trigger in receiver. But Broadcast receiver is not calling. Here is my code.
Here is piece of code..
In Details activity:
Log.e("alarm set", "cal" + cal.getTime());
setAlarm(cal);
private void setAlarm(Calendar targetCal){
//this log got printed.
Log.e("alarm set",""+targetCal.getTime());
Intent intent = new Intent(Details.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Details.this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
}
But AlarmReceiver is not calling.
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent arg1) {
//this is not working.
Log.e("alarm",";alarm");
Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show();
Intent service1 = new Intent(context, AlarmService.class);
context.startService(service1);
}
}
I given permission as,
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<receiver
android:name=".AlarmReceiver"
android:process=":remote" />
<service
android:name=".AlarmService"
android:enabled="true"
android:exported="true"></service>
I couldn't find what mistake I am doing.
Thanks in advance.

Related

Android: Alarm not starting service

Requirement: Start background service repeatedly
Design: Using AlarmManager
What I did:
<service
android:name=".MyService"
android:exported="false" >
</service>
AlarmManager alarm = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, MyService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), SERVICE_ALARM_INTERVAL, pendingIntent);
The service class is straight forward extends Service with:
#Override
public void onCreate() {
HandlerThread thread = new HandlerThread("ServiceStartArguments", Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
//Get the HandlerThread's Looper and use it for our Handler
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
mContext = getApplicationContext();
Toast.makeText(mContext, "Service!!!!!", Toast.LENGTH_LONG).show();
Log.e("tag", "Service!!!");
}
Problem: The Toast message and the Log printing not executed.
What I did wrong here?
Thanks,
You are using PendingIntent.getBroadcast() - this triggers a broadcast. You need to use PendingIntent.getService() if you want to trigger a service.
Try this
AndroidManifest.xml
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="com.example.project.AlarmReceiver" />
</intent-filter>
</receiver>
MainActivity.java
Intent intent = new Intent(context, AlarmReceiver.class);
intent.setAction("com.example.project.AlarmReceiver");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), SERVICE_ALARM_INTERVAL, pendingIntent);
AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm received!!!!!", Toast.LENGTH_LONG).show();
}
}
AlarmManager will be invoked using broadcast receiver as shown above

My Alarm Service don't fire

I have searched in everywhere but I can understand because my Service at a specific time of the day don't fire. Nothing happens.
My code for set up the Alarm:
AlarmManager alarmMgr = (AlarmManager) act.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(act, AlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(act, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
// set for 5 seconds later
alarmMgr.set(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis()+5000, alarmIntent);
My code for the Service:
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("Alarm worked well");
Utils.criarNotificacao(context);
}
}
In the Manifest:
<receiver android:name="br.com.blacktoad.q48h2.utils.AlarmReceiver" android:process=":remote"></receiver>
I can't find any answer.
I find the error!
I need put the:
<receiver android:name="br.com.blacktoad.q48h2.utils.AlarmReceiver" android:process=":remote"></receiver>
in the manifest, but contained in the "application" tag

Notification message does not work

I followed a tutorial to display notification message in a certain time but I don't know why it does not work
Here is the method
//notification
private void startAlarm() {
AlarmManager alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
Calendar Calendar_Object = Calendar.getInstance();
Calendar_Object.set(Calendar.MONTH, 12);
Calendar_Object.set(Calendar.YEAR, 2014);
Calendar_Object.set(Calendar.DAY_OF_MONTH, 31);
Calendar_Object.set(Calendar.HOUR_OF_DAY, 9);
Calendar_Object.set(Calendar.MINUTE, 06);
Calendar_Object.set(Calendar.SECOND, 0);
Intent intent = new Intent(ManagePassengers.this, AlarmReciver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ManagePassengers.this, 0, intent, 0);
alarmManager.set(AlarmManager.RTC, Calendar_Object.getTimeInMillis(), pendingIntent);
}
Broadcast Class
public class AlarmReciver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// When our Alaram time is triggered , this method will be excuted (onReceive)
// We're invoking a service in this method which shows Notification to the User
Intent myIntent = new Intent(context, NotificationService.class);
context.startService(myIntent);
}}
Service class
public class NotificationService extends Service {
private NotificationManager mManager;
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
// Getting Notification Service
mManager = (NotificationManager) this.getApplicationContext()
.getSystemService(
this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(), ManageRides.class);
Notification notification = new Notification(R.drawable.icon,
"See My App something for you", System.currentTimeMillis());
...
mManager.notify(0, notification);
}
....
}
manifest edits
<uses-permission android:name="android.permission.WAKE_LOCK" />
<activity android:name=".AlarmReciver" />
<activity android:name=".NotificationService" />
I wait for the time that I specified in the calendar object but nothing appears, I don't know if I missed any thing
any help will be appreciated
Your registration of Service and Receiver components are wrong. Example of Service and Receiver in Manifest is shown below:
<service
android:name="MyService"
android:icon="#drawable/icon"
android:label="#string/service_name"
>
</service>
<receiver android:name="MyScheduleReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Check these tutorials:
http://www.vogella.com/tutorials/AndroidServices/article.html
http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html

Scheduling an event in AlarmManager on android

I have already read some tutorials and read the documentation, but I can't make this work... This is what I have been testing:
This is the way I register the intent to be called with the alarm manager:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, 1)
PendingIntent sender = PendingIntent.getBroadcast(this, 192837, new Intent(this, AlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
My AlarmReceiver:
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent intent2 = new Intent(context, NewCommit.class);
context.startActivity(intent2);
}
}
And of course I added the receiver in the androidManifest.xml:
<receiver android:process=":remote" android:name="AlarmReceiver"></receiver>
And is INSIDE the application tag.
Any idea? It's driving me crazy, I can't find what's wrong!
Thanks
I just added to the intent from pendingIntent:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
I hope in helps someone!
You need to put permission in AndroidManifest.xml
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />

broadcast receiver not getting called with alarm

I'm trying to call a broadcast receiver at a certain time by setting an alarm. It doesn't appear to be getting called at all. I'm trying to debug with logcat.
My set up for the alarm is something like this:
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar notifyAlarm = Calendar.getInstance();
notifyAlarm.set(Calendar.HOUR, 17);
notifyAlarm.set(Calendar.MINUTE, 00);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 2378530, intent, 0);
alarm.set(AlarmManager.RTC_WAKEUP, notifyAlarm.getTimeInMillis(), pendingIntent);
My receiver is as follows:
public class AlarmReceiver extends BroadcastReceiver {
private static final String TAG ="test receiver";
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.v(TAG, "receiver called");
}
}
also in the manifest I have:
</activity>
<receiver android:name="AlarmReceiver"></receiver>
</application>
Any help appreciated.
Thinking about it, I'm not sure you can send a broadcast to a BroadcastReceiver using an explicit Intent as you are doing it...
Intent intent = new Intent(this, AlarmReceiver.class);
Try giving the <receiver section an <intent-filter> example...
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="com.mypackage.ACTION_DO_SOMETHING" />
</intent-filter>
</receiver>
Then when you create your Intent, do it as follows...
Intent intent = new Intent("com.mypackage.ACTION_DO_SOMETHING");

Categories

Resources