0

通知サービスを介して ServicesActivity.java から NotificationView.java にデータを渡すのが好きです。しかし、データの受け渡しは最初は正しく、別のデータを送信すると、以前のデータは更新されません。ご覧のとおり、以下の logcat 出力は、最初に 2 つのデータを通知に送信し、それを出力します。 NotificationView.java で渡されても問題ありません。その後、3 つのデータを再送信しますが、NotificationView.java での出力は 2 つのデータのままです。通知に送信したデータの数が何であれ、 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();
        }
    });
}

}
4

1 に答える 1

1

デフォルトでは、エクストラのみを変更すると、Android は既存の (キャッシュされた) を使用しPendingIntentます。フラグを使用FLAG_UPDATE_CURRENTして、指定されたエクストラで新しいものを強制的に作成します。

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

于 2012-11-21T03:17:55.880 に答える