【问题标题】:How to retrieve data passed thru an intent?如何检索通过意图传递的数据?
【发布时间】:2012-11-28 16:57:33
【问题描述】:

我正在通过通知中的意图发送一些数据(字符串)。但是,当我尝试在通知启动的活动中接收它时,我什么也没收到。这是我的代码。请帮忙。

这是通知的代码:

int icon=R.drawable.ic_launcher;
                String ticket="Encrypted message";
                long when=System.currentTimeMillis();
                NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                Context context1 =getApplicationContext();
                String expandedText="You've received an encrypted message, click to view";
                String expandedTitle="Encrypted Message";
                Notification notification = new Notification(icon, ticket, when);
                Intent intent1=new Intent(context,Try.class);
                intent1.putExtra("MSG","Jumbo");
                PendingIntent launchIntent=PendingIntent.getActivity(context,0, intent1,0);

notification.setLatestEventInfo(context,expandedTitle,expandedText,launchIntent);
                mNotificationManager.notify(1,notification);

这就是我在 Try.class 中接收它的方式:

t = (TextView)findViewById(R.id.dec);
        String d = "";
        Intent I = getIntent();
        d = I.getStringExtra("MSG");
        String text = "This is an example";
        t.setText(d);

没有错误 - 当我运行 TextView 时它被设置为空

【问题讨论】:

    标签: android string android-intent android-notifications


    【解决方案1】:

    getIntent() 在那里不起作用,请尝试在您的活动中覆盖 onNewIntent 方法。

    【讨论】:

      【解决方案2】:

      你有没有打电话 setResult(Activity.RESULT_OK,意图) 在您的通知程序中?您需要它,否则 Android 将永远不会重新启动您的“通话”活动。

      【讨论】:

      • 不,我没有。我刚才加了,说不适用。
      • 您能详细说明一下吗?您是遇到语法错误、运行时错误还是什么?
      【解决方案3】:

      而不是

      Intent intent1=new Intent(context,Try.class);
      

      改成

      Intent intent1=new Intent(YourActivity.this,Try.class);
      

      onCreate()方法试试

      t = (TextView)findViewById(R.id.dec);
      
      Bundle extras = getIntent().getExtras(); 
      String d;
      
       if (extras != null) {
            d = extras.getString("MSG");
        }   
      t.setText(d);
      

      【讨论】:

      • onCreate()方法中
      【解决方案4】:

      应该可以,尝试记录“d”的值

      Log.d("Value of D", d);
      

      我认为您的问题是您的 TextView t 设置不正确,因此没有显示结果。

      【讨论】:

      • 我设置了 d = "displayText" 并执行了它——效果很好。 TextView 没问题!就是这个意图! :(
      猜你喜欢
      • 1970-01-01
      • 2018-09-11
      • 2014-06-27
      • 2017-11-06
      • 2016-04-21
      • 1970-01-01
      • 2017-08-25
      • 2022-06-11
      • 1970-01-01
      相关资源
      最近更新 更多