数据传递的问题

时间:2012-11-21 03:13:25

标签: android listview android-intent notifications

我喜欢通过通知服务将ServicesActivity.java中的数据传递给NotificationView.java。但是数据传递在第一次是正确的,当我向其发送另一个数据时,它不会更新其先前的数据。如您所见,下面的logcat输出,第一次,我发送了两个数据到通知中并输出它在NotificationView.java传递是没有问题的。之后,我重新发送三个数据,它在NotificationView.java的输出仍然是两个数据。无论我向通知发送了多少数据,NotificationView.java的输出仍然是第一次输出。这是logcat输出:

11-21 10:42:03.645: D/String(25694): admin,admin
11-21 10:42:33.645: D/String(25694): admin,admin,admin
11-21 10:42:06.308: D/how(25694): admin,admin
11-21 10:42:36.518: D/how(25694): admin,admin

** Log.d(“String”,str)位于ServicesActivity.java的displayNotification()和Log.d(“how”,i)位于NotificationView.java。

ServicesActivity.java

public class ServicesActivity extends Activity {

IntentFilter intentFilter;
int notification = 1;
String datapassed; 
String str = "";
String[] data;
int dlength;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_services);

    intentFilter = new IntentFilter();
    intentFilter.addAction(MyService.MY_ACTION);
    registerReceiver(intentReceiver, intentFilter);
}

public void startService(View view){
    startService(new Intent(getBaseContext(),MyService.class));
    Toast.makeText(getBaseContext(), "start",
            Toast.LENGTH_SHORT).show();
}

public void stopService(View view){
    stopService(new Intent(getBaseContext(),MyService.class));
    Toast.makeText(getBaseContext(), "stop",
            Toast.LENGTH_SHORT).show();
}


private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
    datapassed = intent.getStringExtra("DATAPASSED");
    if(datapassed.length()>0){

        data = datapassed.split("[*]");
        dlength = data.length;

        for(int i=0;i<dlength;i++){
            if(i==dlength-1){
                str += String.valueOf(data[i]);                 
            }else{
                str += String.valueOf(data[i]) + ",";
            }   
        }
        Log.d("dataServices",str);
        displayNotification();
            str = "";           
    }
    }
    };

    protected void displayNotification(){
        Intent i = new Intent(this,NotificationView.class);
        i.putExtra("notification", notification);
        i.putExtra("name",str);
        Log.d("String",str);
        PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);

        NotificationManager mnotis =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        Notification notis = new Notification(R.drawable.notices2,"Reminder:You have " + dlength + " new friend request",System.currentTimeMillis());
        notis.setLatestEventInfo(this,"Friend Request", str + "has sent you a friend request",pi);
        notis.vibrate = new long[]{100,250,100,500};
        mnotis.notify(0, notis);
    }
  }

NotificationView.java

public class NotificationView extends Activity{

String[] people;
ListView lv;
Button btn1,btn2;

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view);  
    NotificationManager mnotis =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    mnotis.cancel(getIntent().getExtras().getInt("notificationID"));
    String i = getIntent().getExtras().getString("name");
    people = i.split("[,]");
    Log.d("how",i);

    lv= (ListView) findViewById(android.R.id.list);
    btn1 = (Button)findViewById(R.id.acceptbtn);
    btn2 = (Button)findViewById(R.id.closebtn);
    ArrayAdapter<String> list = new ArrayAdapter<String>(NotificationView.this,R.layout.friendlist_item, R.id.friend_name,people);
    lv.setAdapter(list); 

    btn2.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            finish();
        }
    });
}

}

1 个答案:

答案 0 :(得分:1)

默认情况下,如果您只更改附加内容,Android将使用现有(缓存)PendingIntent。使用FLAG_UPDATE_CURRENT标志强制它创建具有指定附加内容的新标记。

http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_UPDATE_CURRENT