始终从 Android 通知意图获取空数据

4

经常出现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");
    }
}
 }

1
移除 Intent.FLAG_ACTIVITY_CLEAR_TOP - IntelliJ Amiya
仍然无法在TextView中显示任何内容。 - Vishal Vaishnav
@VishalVaishnav,就像我告诉你的那样,使用那个方法。 - Piyush
@VishalVaishnav 请检查 https://dev59.com/lmw15IYBdhLWcg3w3fjN - IntelliJ Amiya
@IntelliJAmiya 不,这不会造成任何问题。 - Piyush
显示剩余12条评论
2个回答

1
onNewIntent(Intent intent)方法中获取您的数据。
@Override
protected void onNewIntent(Intent intent) 
 {
   super.onNewIntent(intent);
   //code
 }

你说的“不调用”是什么意思?你怎么知道它没有被调用? - Piyush
我在OnCreate()中定义了TextView,但是TextView中没有任何内容。因此我们可以推断这个函数没有被调用。 - Vishal Vaishnav
仍然得到Bundle为空的错误...请查看我的更新代码。 - Vishal Vaishnav
将此代码 Bundle extras = intent.getExtras(); 更改。您是在 NotificationActivity 中传递数据,然后在 ScondActivity 中检索吗? - Piyush
@Piyush 您好,能否请您检查一下我的问题?我已经卡在这里一天了。 - anonymous
显示剩余3条评论

0

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

 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);

请问您能否查看这个问题 - anonymous

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接