Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Broadcast receiver and pending intent : Show a toast

The following is the code for an alarm that has to hit the BroadCast Receiver :

Intent intentWithData = new Intent(context, TokenActivity.class);
intentWithData.putExtra(Constants.ID,id);
intentWithData.putExtra(Constants.POSITION, finalI);

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 007, intentWithData, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pendingIntent);

The code for the Broadcast receiver is

import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class TokenBroadcastReceiver extends BaseBroadCastReceiver {

String Id;
int position;
@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Create a toast", Toast.LENGTH_SHORT).show();
}
}

The manifest is :

<receiver android:name=".broadcastReceiver.TokenBroadcastReceiver"/>

The toast is not showing up. Where am I going wrong with this code?

like image 600
praxmon Avatar asked May 05 '16 14:05

praxmon


People also ask

Is a pending intent also a way to call out a broadcast receiver?

To perform a broadcast via a pending intent, get a PendingIntent via the getBroadcast() method of the PendingIntent class. To perform an activity via a pending intent, you receive the activity via PendingIntent.

What is intent and pending intent?

PendingIntent is a wrapper of Intent . The foreign app that receives the PendingIntent , doesn't know the content of Intent which is wrapped by PendingIntent . The mission of foreign app is to send back the intent to owner when some conditions are met (For example: alarm with schedule, or notification with click...).

What is pending intent example?

A PendingIntent is a token that you give to a foreign application (e.g. NotificationManager , AlarmManager , Home Screen AppWidgetManager , or other 3rd party applications), which allows the foreign application to use your application's permissions to execute a predefined piece of code.

What is the significance of the pending intent?

A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it.


2 Answers

You're mixing 2 things. If you want your receiver to get the intent:

Intent intentWithData = new Intent(context, TokenBroadcastReceiver.class);
intentWithData.putExtra(Constants.ID,id);
intentWithData.putExtra(Constants.POSITION, finalI);

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 7, intentWithData, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pendingIntent);

if you want your activity to get the intent:

Intent intentWithData = new Intent(context, TokenActivity.class);
intentWithData.putExtra(Constants.ID,id);
intentWithData.putExtra(Constants.POSITION, finalI);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 7, intentWithData, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pendingIntent);

Plus, make sure your receiver is registered in your AndroidManifest.xml

like image 54
MarkySmarky Avatar answered Sep 19 '22 19:09

MarkySmarky


You are setting the pending intent to open an activity as per your code

Intent intentWithData = new Intent(context, TokenActivity.class);

and displaying the toast in broadcast receiver. Please correct your code and it will start working.

    Intent intentWithData = new Intent(this, TokenBroadcastReceiver.class);
    intentWithData.putExtra("id",5);
    intentWithData.putExtra("position", 4);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 007, intentWithData, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pendingIntent);

Don't forget to register your broadcast in manifest

<receiver android:name=".broadcastReceiver.TokenBroadcastReceiver"/>
like image 22
Praween Kumar Mishra Avatar answered Sep 18 '22 19:09

Praween Kumar Mishra