2013-05-14 50 views
0

我在发送待处理意向附加信息时遇到问题。我在我的StateCh服务中添加了额外的字符串并发送到我的MainActivityMainActivity按预期启动,但我放在那里的额外字符串总是丢失。收到时未填写意图额外信息

MainActivity.java:

public class MainActivity extends Activity { 

public void onResume() { 
    super.onResume(); 

String recMessage = this.getIntent().getStringExtra("message"); 

if(recMessage.equals("")) { 
    Log.v("recMessage", "none"); 
    } else { 
    Log.v("recMessage", "something"); 
    } 
    // .. 
} 
} 

StateCh.java:

public class StateCh extends Service { 

//... 

    private void notificationU(String title, String text) { 

    //The intent to launch when the user clicks the expanded notification 
    Intent intent = new Intent(this, MainActivity.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    intent.putExtra("message", "ssss"); 
    intent.setAction("actionstring" + System.currentTimeMillis()); 

    PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

    Notification noti2 = new NotificationCompat.Builder(this) 
    .setContentTitle(title) 
    .setContentText(text) 
    .setSmallIcon(R.drawable.warning) 
    .setContentIntent(pendIntent) 
    .build(); 

    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    mNotificationManager.notify(123456, noti2); 
    } 

    // ...  

} 
+0

什么是'eguals'?它应该是'equals' – 2013-05-14 05:00:25

+0

为什么'OnCreate'?在OnCreate之后总是调用OnResume。 – Kristopher 2013-05-14 05:05:09

+0

解决!我在这里找到答案:http://stackoverflow.com/questions/6352281/getintent-extras-always-null – Kristopher 2013-05-14 11:21:31

回答

0

使用TextUtils

if (!TextUtils.isEmpty(recMessage)) { 

// here your require condition 

} 
+0

这不是问题的原因。如果我用'Log.v(“message =”,recMessage)替换这个签入代码;'我会得到'message ='。 – Kristopher 2013-05-14 07:55:13