始终从通知意图Android获取数据为空 [英] Always getting data null from notification intent android

查看:115
本文介绍了始终从通知意图Android获取数据为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总是使包为空....

这是我的代码;

  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

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的Intent.像这样....

I just solve my problem.....I defined Intent for SecondActivity out side the button click event .So, I got bundle blank.My code is look like this....

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

这篇关于始终从通知意图Android获取数据为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆