Scheduling tasl on BOOT_COMPLETED - android

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>

Related

Launch application using background service android

Needed: Launch application using alarm manager when alarm trigger it launch the application.
Tried:
BroadCast Receiver for receive alarm trigger and launch application but not launching the application.
#Override
public void onReceive(final Context context, Intent intent) {
Intent myIntent = new Intent(context, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
Setting Alarm:
public void startAlarm(Context context) {
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, RestartServiceBroadcastReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(), AlarmManager.INTERVAL_HOUR, alarmIntent);
}
Manifest:
<receiver android:name="com.android.cchhaatt.RestartServiceBroadcastReceiver"
android:enabled="true"
android:exported="true"
android:label="StartMyServiceAtBootReceiver"
android:process=":remote">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>

WakefulBroadcastReceiver is never invoked

I have following schema BOOT_COMPLETE intent ->BroadcastReciever starts, then starting WakefullBroadcastReceiver -> sending intent to NotificationIntentService.
But my WakefullBroadcastReceiver is never working, if i change schema to BOOT_COMPLETE intent ->BroadcastReciever-> sending intent to NotificationIntentService. everething works perfectly
BroadcastReceiver:
public class BootBroadcastReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (!App.isRunning) {
Log.d("wakefull", "start");
Intent startIntent = new Intent("SOMEACTION");
//startIntent.setAction(Utils.NOTIFY_INTENT);
PendingIntent startPIntent = PendingIntent.getService(context, 0, startIntent, 0);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP,
SystemClock.elapsedRealtime() + 3000, 5000, startPIntent);
App.isRunning = true;
}
Log.e("bool",App.isRunning+"");
}
}
WakefulBroadcastReceiver:
public class SimpleWakefulReciever extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.e("wakefull","received");
Intent service = new Intent(context, NotificationWakefulIntentService.class);
startWakefulService(context,service);
}
}
NotificationIntentService:
public class NotificationWakefulIntentService extends IntentService {
public NotificationWakefulIntentService() {
super("NotificationWakefulIntentService");
}
#Override
protected void onHandleIntent(Intent intent) {
Log.d("time",(System.currentTimeMillis()/1000)+"");
SimpleWakefulReciever.completeWakefulIntent(intent);
}
}
Manifest:
<receiver android:name=".broadCastReciever.BootBroadcastReciever" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="START"/>
</intent-filter>
</receiver>
<receiver android:name=".wakefullBroadcastReciever.SimpleWakefulReciever">
<intent-filter>
<action android:name="SOMEACTION"/>
<action android:name="WAKEFUL"/>
</intent-filter>
</receiver>
<service
android:name=".wakefulService.NotificationWakefulIntentService"
android:enabled="true">
<intent-filter>
<action android:name="NOTIFY_INTENT" />
</intent-filter>
</service>
First, get rid of all <intent-filter> elements other than the one wrapping <action android:name="android.intent.action.BOOT_COMPLETED"/>. Never put an <intent-filter> on a component unless you want arbitrary third-party apps to start that component whenever they want to.
Then, replace:
Intent startIntent = new Intent("SOMEACTION");
PendingIntent startPIntent = PendingIntent.getService(context, 0, startIntent, 0);
with:
Intent startIntent = new Intent(context, SimpleWakefulReciever.class);
PendingIntent startPIntent = PendingIntent.getBroadcast(context, 0, startIntent, 0);
This fixes your Intent to no longer use the action string and fixes your PendingIntent to use getBroadcast(), since you are trying to trigger a BroadcastReceiver.
Then, replace 5000 with a value of at least 60000, since you cannot have a repeating alarm more frequently than that on Android 5.1+.

onReceive not calling in PendingIntent.getBroadcast

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.

My broadcast receiver is not receiving

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>

Broadcastreceiver not receiving triggered Alarm

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>

Categories

Resources