1

總是收到捆綁零....總是從通知意圖獲取數據null 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

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"); 
    } 
} 
} 
+0

在第二項活動中,當我點擊通知 –

+0

時,我在編輯文本中輸入文字,通過按鈕單擊時發出通知,當我點擊通知時,我想在SecondActivity中顯示Textview中的文字 –

+0

remove'Intent.FLAG_ACTIVITY_CLEAR_TOP ' –

回答

0

我解決我的問題.....我定義爲意向進行SecondActivity側按鈕點擊事件。所以,我得到了捆綁blank.My代碼是這個樣子....

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

獲取onNewIntent(Intent intent)方法您的數據。

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

這個覆蓋函數不是調用.....在我的代碼 –

+0

你是什麼意思不調用?你怎麼知道它不叫? – Piyush

+0

cozzz我在OnCreate()中定義了TextView,而我在TextView中什麼都沒有。因此我們可以預料這個函數不會調用 –