gpt4 book ai didi

android - 广播接收器和未决 Intent : Show a toast

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:45:07 24 4
gpt4 key购买 nike

以下是必须触发 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();
}
}

list 是:

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

toast 没有出现。这段代码哪里出错了?

最佳答案

您正在混合 2 种东西。如果您希望您的接收器获得 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);

如果您希望您的 Activity 获得 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 中注册

关于android - 广播接收器和未决 Intent : Show a toast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37052630/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com