【问题标题】:how to send data from java service class to kotlin activity class? [closed]如何将数据从 java 服务类发送到 kotlin 活动类? [关闭]
【发布时间】:2021-03-02 17:32:26
【问题描述】:

这是我的 Java 服务类 FCM

公共类 MyFirebaseMessagingService 扩展 FirebaseMessagingService { private static final String TAG = "FCM 数据标签" ;

@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    // TODO(developer): Handle FCM messages here.
   
    Log.d(TAG, "From: " + remoteMessage.getFrom());

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());

    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        String data=  remoteMessage.getNotification().getBody();
       String details[] = (data.split("\n"));
       String username,location,serviceType,date,time,area;
        username=details[0];
        location=details[1];
        serviceType=details[2];
        date=details[3];
        time=details[4];
        area=details[5];

        Intent i=new Intent(getApplicationContext(),FragmentHome.class);
        i.putExtra("user",username);
        i.putExtra("loc",location);
        i.putExtra("type",serviceType);
        i.putExtra("Date",date);
        i.putExtra("Time",time);
        i.putExtra("Area",area);


    }

我不知道如何在 kotlin 类中接收数据。

【问题讨论】:

  • 查找本地广播接收器
  • 在这种情况下,如果您想要在应用程序运行时点击通知的数据,请使用本地广播。

标签: java android kotlin android-intent broadcastreceiver


【解决方案1】:

在 kotlin 活动的 onCreate 方法中,您需要获取这些变量,如下所示 -

val user:String = intent.getStringExtra("user")
val loc:String = intent.getStringExtra("loc")
val type:String = intent.getStringExtra("type")
val date:String = intent.getStringExtra("Date")
val time:String = intent.getStringExtra("Time")
val area:String = intent.getStringExtra("Area")

【讨论】:

  • 我试过了,val broadCastReceiver = object : BroadcastReceiver() { override fun onReceive(contxt: Context?, intent: Intent?) { val user: String? = intent?.getStringExtra("user") tvUser.text = user Toast.makeText(requireContext(),user,Toast.LENGTH_SHORT).show() } } 但 textview 没有更新
  • 请告诉方法接收,我应该使用意图过滤器吗?
  • 分享你想要接收intent dara的课程
  • 我使用了意图过滤器现在它可以工作了,谢谢
  • 请为答案投票
猜你喜欢
  • 2017-03-03
  • 2013-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-30
  • 1970-01-01
  • 1970-01-01
  • 2021-06-26
相关资源
最近更新 更多