android 挂起Intent中的intent.putExtra()不起作用

yhqotfr8  于 2023-03-21  发布在  Android
关注(0)|答案(8)|浏览(298)

我正在通过alarmreceiver传递一个来自服务类的挂起Intent。但是,在pendingIntent触发后,broadcastreceiver类没有接收到intent.putExtra()信息。以下是触发pendingIntent的代码

Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, aint, PendingIntent.FLAG_UPDATE_CURRENT);
aint.putExtra("msg", msg);
aint.putExtra("phone", phone);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

报警接收器类别如下

public String msg, phonen;

@Override
public void  onReceive(Context context, Intent intent){
    Bundle extras = intent.getExtras();
    msg = extras.getString("msg");
    phonen = extras.getString("phone");

    Log.d("onReceive", "About to execute MyTask");
    Toast.makeText(context,msg, Toast.LENGTH_LONG).show();
}

未显示吐司中的msg信息(从待定意图接收)。而是显示空白toast。

xtfmy6hx

xtfmy6hx1#

试试这个

Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class);
aint.putExtra("msg", msg);
aint.putExtra("phone", phone);


PendingIntent pendingIntent = PendingIntent.getBroadcast(
    getApplicationContext(),
    id, 
    aint,
    // as stated in the comments, this flag is important!
    PendingIntent.FLAG_UPDATE_CURRENT);
atmip9wb

atmip9wb2#

记住在PendingIntent构造函数中放置一个唯一的id,否则当您尝试获取putExtra值时,可能会得到一些奇怪的结果。

PendingIntent pendingIntent = PendingIntent.getBroadcast(
           getApplicationContext(), 
           UUID.randomUUID().hashCode(), 
           aint, 
           PendingIntent.FLAG_UPDATE_CURRENT
    );
dauxcl2d

dauxcl2d3#

您必须使用getStringExtra()并确保String不为空:

Intent intent = getIntent();    
     msg = intent.getStringExtra("msg");
     phonen = intent.getStringExtra("phone");

     if(msg!=null){
      Toast.makeText(context,msg,
        Toast.LENGTH_LONG).show();
     }

并将您的putExtras反转到PendingIntent之前:

Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class);
   aint.putExtra("msg", msg);
   aint.putExtra("phone", phone);
   PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, aint, PendingIntent.FLAG_UPDATE_CURRENT);
92vpleto

92vpleto4#

这是因为您首先要初始化Intent并添加到PendingIntent。之后您要在Intent中添加信息。您应该先向Intent添加信息,然后再将此Intent添加到PendingIntent。

iqjalb3h

iqjalb3h5#

在我的例子中,我缺少PendingIntent.FLAG_UPDATE_CURRENT作为标志。

esbemjvw

esbemjvw6#

final Intent my_intent = new Intent(this.context , Alarm_Receiver.class);

    //put in extra string into my intent
    //tell the clock that the user pressed the "alarm on button"
    my_intent.putExtra("extra" , 1);
    int state = my_intent.getExtras().getInt("extra");
    Log.i("extraa" , "the state in the main activity in alarm on Button is: " + state);

我遇到了同样的问题,我发现应该在涉及Intent之前先考虑putExtra

ldfqzlk8

ldfqzlk87#

请使用FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT

lbsnaicq

lbsnaicq8#

在Kotlin,我已经这样解决了我的问题。我试图从另一个服务类启动服务。谢谢你的其他回答。

var float_intent = Intent(this,FloatingViewService::class.java)
    float_intent.putExtra("sended_name",customer_name)

    val pending_float_intent = PendingIntent.getService(
        this,UUID.randomUUID().hashCode(),float_intent,FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
    )

相关问题