Broadcast receiver and pending intent : Show a toa

2019-02-19 03:38发布

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?

2条回答
萌系小妹纸
2楼-- · 2019-02-19 04:11

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"/>
查看更多
ら.Afraid
3楼-- · 2019-02-19 04:14

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

查看更多
登录 后发表回答