广播接收者和未决的意图

广播接收者和未决的意图

本文介绍了广播接收者和未决的意图:举杯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是必须按下 BroadCast接收器的警报的代码:

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);

广播接收器的代码为

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();
}
}

清单是:

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

吐司没有出现。这段代码哪里出问题了?

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

推荐答案

您正在混合两件事。
如果您希望接收者获得意图:

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);

此外,请确保您的接收者已在AndroidManifest.xml中注册

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

这篇关于广播接收者和未决的意图:举杯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 11:56