Reputation: 15
Here is my problem, I have a service started on boot or on the launch of the application, this service start an alarm which download a file every x minutes. The problem is that the broadcast receiver doesn't seems to receive anything.
here is the concerned code:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Intent alarmIntent = new Intent(this, ServiceCalendrier.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
Toast.makeText(this, "My Service Started ", Toast.LENGTH_LONG).show();
startAlarm();
return Service.START_NOT_STICKY;
}
public void startAlarm() {
manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
int interval =5000;
manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show(); //this toast is printed
}
private final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
getIcs(arg0);// download function
Toast.makeText(arg0, "getICS", Toast.LENGTH_LONG).show();// this one doesn't appear
}
};
Do I have to declare my service as a receiver in my AndroidManifest?
Upvotes: 0
Views: 676
Reputation: 15
I eventually managed to make it work.
public void startAlarm() {
manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
int interval =5000;//7200000;
IntentFilter myFilter = new IntentFilter("WhatEverYouWant");
Intent alarmIntent = new Intent("WhatEverYouWant");
pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
registerReceiver(receiver, myFilter);
manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}
The androidManifest :
<service android:enabled="true" android:name="MyService">
<receiver android:name="MyService">
<intent-filter>
<action android:name="WhatEverYouWant" />
</intent-filter>
</receiver>
</service>
</application>
I still have some work to understand how it works and clean my code but thank you very much for your help
Upvotes: 1
Reputation: 3502
You need to declare your service in manidest file like this
<application>
</activity>
........
..........
</activity>
<service android:name=".ServiceNameClass"></service>
</application>
also u need to register your broadcast somethink like this
LocalBroadcastManager.getInstance(getBaseContext()).registerReceiver(mMessageReceiver,new IntentFilter("my-event"));
Upvotes: 0