• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Problem with a custom broadcastReciever and AlarmManager

Rokia

Lurker
Dec 19, 2012
2
0
Hello,
i m developping an application for creating , deleting and modifing Tasks with a system alarm manager.
Here is the code snippet where i start a service called 'AlarmService'. The role of this service is to create an AlarmManger and send an intent to a receiver called 'AlarmReceiver'. The role of the receiver is to send notification to the user when the alarm goes off.

[HIGH]
public class CreationTaskActivity extends FragmentActivity {
[...]
Intent intent_alarm = new Intent(CreationTaskActivity.this,AlarmService.class);
intent_alarm.putExtra("nomTache", name);
intent_alarm.putExtra("descTache", description);
intent_alarm.putExtra("identifiant",uri.getLastPathSegment()); intent_alarm.putExtra("declencher",gc.getTimeInMillis());
CreationTaskActivity.this.startService(intent_alarm);
[/HIGH]AlarmService.java:
[HIGH]
public class AlarmService extends IntentService {

public AlarmService() {
super("AlarmService");
}

@Override
protected void onHandleIntent(Intent intent) {

Log.d("alarmservice", "cool");
PendingIntent sender = PendingIntent.getBroadcast(this, 0,intent, 0);
Intent new_intent = new Intent(this, AlarmReceiver.class);
new_intent.putExtra("nomTache", intent.getStringExtra("nomTache"));
new_intent.putExtra("descTache", intent.getStringExtra("descTache"));
new_intent.putExtra("identifiant",intent.getStringExtra("identifiant"));
new_intent.setAction("com.dummies.android.taski_rla0904.alarmaction");
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, intent.getLongExtra("declencher",
System.currentTimeMillis()), sender);
sendBroadcast(new_intent);
}
[/HIGH]AlarmReceiver.java:
[HIGH]
public class AlarmReceiver extends BroadcastReceiver {

private final String ALARM = "com.dummies.android.taski_rla0904.alarmaction";
@Override
public void onReceive(Context ctx, Intent intent) {

//PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
//PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
//wl.acquire();
if(intent.getAction().equals(ALARM)){
NotificationCompat.Builder mbuilder = new NotificationCompat.Builder(ctx);
mbuilder.setSmallIcon(R.drawable.ic_launcher);
mbuilder.setContentTitle(intent.getStringExtra("comTache"));
mbuilder.setContentText(intent.getStringExtra("descTache"));
String id = intent.getStringExtra("identifiant");
Log.d("taski","alarmreceive");
NotificationManager mNotificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(Integer.parseInt(id), mbuilder.build());
}
//wl.release();
}

}
[/HIGH]AndroidManifest.xml:
[HIGH]
[...]
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="com.dummies.android.taski_rla0904.alarmaction" />
</intent-filter>
</receiver>
<service android:name=".AlarmService" />
[/HIGH]

When i run the application on the emulator, i have this exception:
java.lang.IllegalArgumentException: Enable to start receiver .java.lang.IllegalArgumentException:contentIntent required....
 
I just have found the solution. what i have to do is to set contentIntent to the builder of notification. just like this:
[HIGH]
Intent notifyIntent =new Intent(ctx,ResultActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent notifyPIntent =PendingIntent.getActivity(ctx,0,notifyIntent,PendingIntent.FLAG_UPDATE_CURRENT);
Puts the PendingIntent into the notification builder
mbuilder.setContentIntent(notifyPIntent);
[/HIGH]
now i obtain the icon of notification . but when i click on, that doesn't do anything.
have i implement the ResultActivity too?
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones