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>
Related
Again i have a problem with Broadcasts..
Fragment:
Intent i = new Intent(context,AlarmReceiver.class);
i.setAction(Intent.ACTION_BOOT_COMPLETED);
int id = (int) alarms_ID;
i.putExtra("_id",id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, i, PendingIntent.FLAG_UPDATE_CURRENT);
Calender calender=...;
alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
BroadcastReceiver:
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"yap",Toast.LENGTH_SHORT).show();
}
and the receiver in manifest:
<receiver
android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
very simple code but still not working...
This code work in activity maybe the problem is the fragment?
If you need more information just say it.
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 creating an service which is use Service Component ,I want to run the application always in background . Suppose i switch off the mobile and when i on the mobile our application is closed i.e. automatically.
am trying this code
androidManifest.xml
<receiver android:name=".receiver.ConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
and adding this code in BroadCastreceiver Class
update
private class ConnectionReceiver extends BroadcastReceiver{
private Timer mTimer;
private TimerTask mTimerTask;
private long interval;
#Override
public void onReceive(Context context, Intent intent) {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context, ConnectionReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + interval, interval, pi);
{
calGps();
}
}
Hi Leema Rose
You want to launch your service/activity after the device booted. Check the below links you will understand how to use BroadcastReceiver.
Autostart an application at bootup
Start Service at boot
I hope it may help you.
New Answer:
Add <category android:name="android.intent.category.HOME" /> in intent-filter of receiver's manifest file.
You have to add a manifest permission entry:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
(of course you should list all other permissions that your app uses).
Then, implement BroadcastReceiver class, it should be simple and fast executable. The best approach is to set an alarm in this receiver to wake up your service (if it's not necessary to keep it running ale the time as Prahast wrote).
public class BootUpReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context, MyService.class), PendingIntent.FLAG_UPDATE_CURRENT);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + interval, interval, pi);
}}
Then, add a Receiver class to your manifest file:
<receiver android:enabled="true" android:name=".receivers.BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
I have the following problem. I have 2 classes. 1 is called AlarmService and the other is called TimeAlarm which extends BroadcastReceiver.
The App should do the following thing: It should generate a new Alarm to a time specified in the preferences (Which it already does...) also in Logcat i can see how the Alarm gets triggered. But the problem is, that the Notification which should be shown does not show up in the StatusBar.
Here is all the code i have for this:
AndroidManifest.xml:
<receiver android:name="com.ikalma.alarmmanager.TimeAlarm">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
AlarmService.java:
private Context context;
private PendingIntent mAlarmSender;
public AlarmService(Context context) {
this.context = context;
Intent notifyIntent = new Intent(Intent.ACTION_MAIN);
notifyIntent.setClass(context, myActivity.class);
mAlarmSender = PendingIntent.getBroadcast(context, 0, notifyIntent, 0);
}
public void startAlarm(int stunde, int minute) {
Calendar updateTime = Calendar.getInstance();
updateTime.set(Calendar.HOUR_OF_DAY, stunde);
updateTime.set(Calendar.MINUTE, minute);
updateTime.set(Calendar.SECOND, 00);
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, mAlarmSender);
}
TimeAlarm.java:
#Override
public void onReceive(Context context, Intent intent) {
Log.e("TEST", "onReceive() called...");
}
The Receiver in the Manifest is inside of the Tag so that should not be a problem.
The problem is, that if i restart my device, it gets called. But not if an Alarm gets triggered.
But the onReceive() method should also be called if an alarm gets triggered, shouldn't it?
Thanks for your help!
your intent filter is only listening to boot complete intents and not your own alarm broadcast intent action. update your intent filter so that your broadcast intent is also received (that means for your special case add the action of Intent.ACTION_MAIN to your intent filter)
<receiver android:name="com.ikalma.alarmmanager.TimeAlarm">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</receiver>
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>