如何接收意图广播的参数

时间:2014-11-12 19:17:31

标签: java android broadcast android-broadcast

Integer id_user = 1;
Intent registration= new Intent("com.google.android.c2dm.intent.REGISTER");
                                registration.putExtra("app", PendingIntent.getBroadcast(getBaseContext(),0,new Intent(),0));
                                registration.putExtra("sender",project_number);
                                startService(registration);

我需要加上类似的

registration.putExtra(" id_user",id_user);

并收到广泛的

public class resquest extends BroadcastReceiver {

NEED CATCH HERE..... a USER_ID

    @Override
    public void onReceive(Context context, Intent intent) {
      .....Code here 
    }
}

问候!

1 个答案:

答案 0 :(得分:0)

首先,您需要发送用户ID才能获取它:

Integer id_user = 1;
Intent registration= new Intent("com.google.android.c2dm.intent.REGISTER");
                            registration.putExtra("app", PendingIntent.getBroadcast(getBaseContext(),0,new Intent(),0));
                            registration.putExtra("sender",project_number);
                            registration.putExtra("id_user", id_user.intValue());
                            startService(registration);

然后你可以像这样获取它

public class resquest extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        int id_user = i.getExtras().getInt("id_user");
    }
}