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?
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.
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...).
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.
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.
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
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"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With