2012-11-21 1 views
0

알림 서비스를 통해 ServicesActivity.java에서 NotificationView.java로 데이터를 전달하고자합니다. 하지만 전달하는 데이터가 처음에는 정확합니다. 다른 데이터를 전송하면 이전 데이터가 업데이트되지 않습니다. 앞에서 보았 듯이 logcat 출력은 처음에는 두 개의 데이터를 알림으로 보내고 출력 NotificationView.java에서 전달 된 내용은 아무런 문제가되지 않습니다. 그런 다음 세 개의 데이터를 다시 보내고 NotificationView.java의 출력은 여전히 ​​두 개의 데이터입니다. 무엇이든 내가 얼마나 많은 데이터 통지로, NotificationView.java에서의 출력은 여전히 ​​output.Here가 로그 캣의 출력 첫 시간에 따라 전송 :데이터 전달 문제

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 ("문자열", STR)에있다 ServicesActivity.java 및 Log.d ("how", i)의 displayNotification()은 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

, 당신은 단지 엑스트라를 변경하는 경우, 안드로이드는 기존의 (캐시) PendingIntent을 사용합니다. FLAG_UPDATE_CURRENT 플래그를 사용하여 지정된 추가 기능으로 새 플래그를 만들도록합니다.

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

+0

작동합니다! 감사. 다른 참조 : PendingIntent pi = PendingIntent.getActivity (this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT); –