【问题标题】:Intent cant receive data in androidIntent无法在android中接收数据
【发布时间】:2017-12-06 15:13:41
【问题描述】:

如果我的意图为空,我想从意图发送数据,然后简单地打开我的主要活动。这是我的试用,但它不适合我。

从一个活动发送

intent = new Intent(this, MainActivity.class);
        intent.putExtra("androidkey",body);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

接收时间

 String uid;
    Intent intent = getIntent();
 uid = intent.getStringExtra("androidkey");

主Activity的oncreate方法

 if (uid.equals("hello")){
            startActivity(new Intent(MainActivity.this, About.class));

        }else if (uid.equals("")||uid.equals(null)){

            startActivity(new Intent(MainActivity.this, MainActivity.class));
        }

请告诉我解决方法 因为它使我的应用程序崩溃。

实际上我想在我的代码中做的是

 @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {


        if(remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data: " + remoteMessage.getData());
            sendNotification(remoteMessage.getData().get("message"));


        }

        //Check if the message contains notification

        if(remoteMessage.getNotification() != null) {
            Log.d(TAG, "Mesage body:" + remoteMessage.getNotification().getBody());
            sendNotification(remoteMessage.getNotification().getBody());
        }
    }

    private void sendNotification(String body) {
 if (body.equals("Image Upload Successfully")){
            intent= new Intent(this, Image_Gallery.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        }else if (body.equals("Video Upload Successfully")){

            intent = new Intent(this, Video_Gallary.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        }else if (body.equals("Home Work Are Uploaded")){

            intent = new Intent(this, HomeWork.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        }

当我的应用程序在前台并且通知到达它打开的完美页面但当应用程序不在前台时它打开 Mainactivity

所以请告诉我我该怎么做..?

【问题讨论】:

  • 请发布 logcat 错误。它可以帮助每个人找到问题。
  • 不要决定在 MainActivity 中加载哪个活动,当它已经启动时,但在创建意图之前。如果您有任何 android 密钥,请启动 About_School 活动,如果没有..启动 main。所以...决定在您创建 Intent 的地方加载哪个活动,而不是在结果活动中。
  • 尝试在空对象引用上调用虚拟方法“java.lang.String android.content.Intent.getStringExtra(java.lang.String)”
  • Notification to activity的可能重复

标签: android firebase android-intent android-activity


【解决方案1】:

好的,因为只有评论可能毫无意义。

不要在你的一项活动中决定去哪里,而是在 创建意图的地方。

像这样:

Intent startIntent;
if(body == null || body.isEmpty()){
    startIntent = new Intent(context, MainActivity.class);
}else{
    startIntent = new Intent(context, About.class);
    startIntent.putExtra("androidkey",body);
}

startIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(startIntent);

【讨论】:

  • 请告诉我原因,否则我帮不了你。
  • 其实我想在 MyFirebaseMessagingService 中实现
  • 所以当一些消息收到然后打开特定的活动,但它不适合我。
  • 那么..您有一个服务可以接收正确的 Firebase 云消息吗?因此,在此服务中,使用给定的密钥启动正确的活动。
  • 请检查一下我真正想要实现的,但它不适合我,然后我会试试这个
【解决方案2】:

发送活动,

    Intent newActivity1 = new Intent(ActivityA.this, ActivityB.class);
newActivity1.putExtra("androidkey", body);
startActivity(newActivity1);

接收活动,

 String value = getIntent().getExtras().getString("androidkey");

或者,

    Bundle extras = getIntent().getExtras(); 
String uid;

if (extras != null) {
    uid= extras.getString("androidkey");

}

对于firebase,试试这样的,

public void onMessageReceived(String from, Bundle data) {
        Bundle bundle = data.getBundle("notification");

                if (bundle != null) {
                   String text = bundle.getString("text");
                    String body = bundle.getString("body");
    }
}

或者,

public void onMessageReceived(RemoteMessage remoteMessage)
    {
        Log.e("dataMessage",remoteMessage.getData().toString());
        try
        {
            Map<String, String> params = remoteMessage.getData();
            JSONObject object = new JSONObject(params);
            Log.e("JSON_OBJECT", object.toString());
      }
   }

【讨论】:

  • 其实我想在 MyFirebaseMessagingService 中实现
  • 所以当一些消息收到然后打开特定的活动,但它不适合我。
  • 哦,好吧,你没有在你的问题中提到这一点
  • 请检查此 URL 及其我的问题,但它不起作用然后我会尝试这个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
相关资源
最近更新 更多