【问题标题】:Always getting data null from notification intent android总是从通知意图android获取数据null
【发布时间】:2017-08-11 06:26:23
【问题描述】:

总是让bundle null....

这是我的代码;

  edt=(EditText)findViewById(R.id.edt);

    String msg=edt.getText().toString();

    //create Intent
     Intent myIntent = new Intent(this, ScondActivity.class);
    myIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    //Here I pass Data;
    myIntent.putExtra("msg",msg);

    //Initialize PendingIntent
    pendingIntent = PendingIntent.getActivity(this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    //Initialize NotificationManager using Context.NOTIFICATION_SERVICE
    notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    //Create listener to listen button click
    OnClickListener listener = new OnClickListener() {
        public void onClick(View view) {
            //Prepare Notification Builder
            notificationBuilder = new NotificationCompat.Builder(MainActivity.this)
                    .setContentTitle("Notification Demo").setSmallIcon(R.mipmap.ic_launcher).setContentIntent(pendingIntent)
                    .setContentText(edt.getText().toString());
            //add sound
            Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            notificationBuilder.setSound(uri);
            //vibrate
            long[] v = {500,1000};
            notificationBuilder.setVibrate(v);
            notificationBuilder .setContentIntent(pendingIntent);
            notificationManager.notify(1, notificationBuilder.build());
        }
    };
    Button btn = (Button)findViewById(R.id.button);
    btn.setOnClickListener(listener);

ScondActivity.java

public class ScondActivity extends Activity {
TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.msglayout);

}

@Override
public void onNewIntent(Intent intent){

    Bundle extras = getIntent().getExtras();
    textView = (TextView)findViewById(R.id.msg);

    if(extras != null) {
        String tabNumber = extras.getString("msg");
        textView.setText(tabNumber);
        Log.d("TEMP", "Tab Number: " + tabNumber);

    } else {
        Log.d("TEMP", "Extras are NULL");
    }
}
 }

【问题讨论】:

  • 在第二个活动中,当我点击通知时
  • 我在编辑文本中输入文本,该文本由单击按钮时的通知显示,当我单击通知时,我想在 SecondActivity 中的 Textview 中显示该文本
  • 删除Intent.FLAG_ACTIVITY_CLEAR_TOP
  • 我仍然得到空白的 TextView
  • @VishalVaishnav 正如我告诉你的那样使用该方法。

标签: java android push-notification notifications android-notifications


【解决方案1】:

onNewIntent(Intent intent) 方法中获取您的数据。

@Override
protected void onNewIntent(Intent intent) 
 {
   super.onNewIntent(intent);
   //code
 }

【讨论】:

  • 不打电话是什么意思?你怎么知道它没有打电话?
  • 因为我在 OnCreate() 中定义了 TextView,但在 TextView 中什么也没有。所以我们可以预期这个函数没有调用
  • 这是覆盖方法,您需要在您的活动中实现。也可以通过onNewIntent方法中的String tabNumber = intent.getStringExtra("msg"); textView.setText(tabNumber);检索数据。
  • 我仍然总是得到 Bundle null...查看我更新的代码
  • 更改此Bundle extras = intent.getExtras(); 然后您在NotificationActivity 中传递数据并在ScondActivity 中检索?
【解决方案2】:

我刚刚解决了我的问题.....我在按钮单击事件之外定义了 SecondActivity 的 Intent 。所以,我得到了捆绑空白。我的代码看起来像这样......

 edt=(EditText)findViewById(R.id.edt);

    //create Intent

    //Create listener to listen button click
    OnClickListener listener = new OnClickListener() {
        public void onClick(View view) {
            //Prepare Notification Builder

            Intent myIntent = new Intent(MainActivity.this, NotificationActivity.class);
            myIntent.putExtra("msg",edt.getText().toString());
            myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );

            //Initialize PendingIntent
            pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            //Initialize NotificationManager using Context.NOTIFICATION_SERVICE
            notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);


            notificationBuilder = new NotificationCompat.Builder(MainActivity.this)
                    .setContentTitle("Notification Demo").
                            setSmallIcon(R.mipmap.ic_launcher).
                            setContentIntent(pendingIntent)
                    .setContentText(edt.getText().toString());
            //add sound
            Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            notificationBuilder.setSound(uri);
            notificationBuilder .setWhen(System.currentTimeMillis());
            //vibrate
            long[] v = {500,1000};
            notificationBuilder.setVibrate(v);
            notificationManager.notify(1, notificationBuilder.build());
        }
    };
    Button btn = (Button)findViewById(R.id.button);
    btn.setOnClickListener(listener);

【讨论】:

猜你喜欢
  • 2015-04-30
  • 1970-01-01
  • 2016-11-15
  • 1970-01-01
  • 2020-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-21
相关资源
最近更新 更多