【问题标题】:android text view not updatingandroid文本视图不更新
【发布时间】:2012-03-22 09:39:16
【问题描述】:

我在更新我的 textview 时遇到问题。 我正在制作一个 c2dm 应用程序。设备正在接收我发送的消息(因为我已将消息显示在通知栏中。我也有许多日志表明已收到消息)。但是,一旦我单击通知区域中的消息以打开我的视图,它只会显示我的 1 条消息。例如,我发送一条消息,然后我想仅在第一条消息显示时发送另一条消息。

我发现我可能需要onNewIntent 但是我不确定我是否以正确的方式绕过它。

 public class MessageReceivedActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_result);
        Bundle extras = getIntent().getExtras();            

        if (extras != null) {
            String message = extras.getString("payload");
            if (message != null && message.length() > 0) {
                TextView view = (TextView) findViewById(R.id.result);
                view.setText(message);
            }
        }

        super.onCreate(savedInstanceState);     
    }
     @Override
     protected void onNewIntent(Intent intent) {             

            setContentView(R.layout.activity_result);
            Bundle extras = getIntent().getExtras();                

            if (extras != null) {
                String message = extras.getString("payload");
                if (message != null && message.length() > 0) {
                    TextView view = (TextView) findViewById(R.id.result);
                     setIntent(intent);
                    view.setText(message);    
                }
            }               
//         setIntent(intent);          
     }
}

我的布局是 activity_result,它只包含 1 个文本视图 我的文本视图称为结果 有效载荷是我从服务器发送的消息

任何帮助将不胜感激

【问题讨论】:

  • Tuffy,据我所知,如果您使用 startActivity() 开始此活动,您需要 android:launchMode="singleTop" 才能获得 MessageReceivedActivity AndroidManifest' or Intent.FLAG_ACTIVITY_SINGLE_TOP`
  • 已经想到了。我的启动模式是单顶boss
  • 好的,然后使用Intent intent2=getIntent(); String str=intent2.getData(); 而不是Bundle extras = getIntent().getExtras();
  • 我在 getData("payload"); "Intent 类型中的 getData() 方法不适用于参数 (String)"
  • String str = getIntent().getStringExtra("payload", "");

标签: android android-layout android-intent android-c2dm android-2.2-froyo


【解决方案1】:

Update Textview from function Android how to change text in Android TextView

希望他们能提供帮助。 在这种情况下是否有理由不使用edittext?我是新手。

【讨论】:

    【解决方案2】:

    我认为您需要注意您的通知 ID。您发送消息,但所有消息都具有相同的通知 ID。您应该以某种方式增加通知 ID。

    【讨论】:

    • 并非如此。它只显示第一次打开应用程序时收到的消息。我可以在通知中看到消息,但没有更新我的文本视图
    【解决方案3】:

    在 onNewIntent() 方法中,你应该在 Bundle extras = getIntent().getExtras(); 之前使用 setIntent(intent)或者 extras 为 null

    并且 setContentView(R.layout.activity_result) 在 onNewIntent() 中不是必需的

    【讨论】:

    • 那是负面的。不工作。我试过:protected void onNewIntent(Intent intent) { // setContentView(R.layout.activity_result);设置意图(意图);捆绑附加服务 = getIntent().getExtras(); if (extras != null) { String message = extras.getString("payload"); if (message != null && message.length() > 0) { TextView view = (TextView) findViewById(R.id.result); view.setText(消息); } }
    【解决方案4】:

    在A市:

    Intent mIntent = new Intent();  
            mIntent.setClass(mContext, MessageReceivedActivity.class);  
    
            mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
    
            mIntent.putExtra("payload", "fdfsfnj");  
            mContext.startActivity(mIntent);  
    

    在 Acitity MessageReceivedActivity 中:

        @Override  
            protected void onNewIntent(Intent intent)  
            {  
    
                String message  = intent.getData();  
                //Or you can use
                String message  = getIntent().getStringExtra("payload", ""); 
    super.onNewIntent(intent);  
            }  
    

    【讨论】:

      【解决方案5】:

      对于所有有这个问题的人。 我看了另一个堆栈溢出问题,这是答案。 在 createNotification 内的消息接收器中添加此

          PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,intent, 
                  PendingIntent.FLAG_CANCEL_CURRENT);
      

      不要忘记最后取消当前视图的标志

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-02
        • 2020-11-21
        • 2021-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多