2014-10-09 43 views
0

我從網上下載這個例子,我想修改從一個服務發送一個Obj學生到已經運行的活動。 這是代碼,從服務到活動的通信它的工作正常,但對象到達空。從服務發送一個對象類到活動

主要活動:

package com.websmithing.broadcasttest; 

import com.websmithing.broadcasttest.Student; 
import android.app.Activity; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TextView; 

public class BroadcastTest extends Activity { 
    private static final String TAG = "BroadcastTest"; 
    private Intent intent; 

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

     intent = new Intent(this, BroadcastService.class); 
    } 

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      updateUI(intent); 
     } 
    }; 

    @Override 
    public void onResume() { 
     super.onResume(); 
     startService(intent); 
     registerReceiver(broadcastReceiver, new IntentFilter(
       BroadcastService.BROADCAST_ACTION)); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     unregisterReceiver(broadcastReceiver); 
     stopService(intent); 
    } 

    private void updateUI(Intent intent) { 
     // String counter = intent.getStringExtra("studente"); 
     Student student = (Student) getIntent().getParcelableExtra("student"); 
     Log.d(TAG, "Ricevuto"); 

     if (student != null) { 
      TextView txtCounter = (TextView) findViewById(R.id.txtCounter); 
      txtCounter.setText(student.mSAge); 
     } else { 
      Log.d(TAG, "Sudente Vuoto"); 
     } 
    } 
} 

的服務:

package com.websmithing.broadcasttest; 

import com.websmithing.broadcasttest.Student; 

import android.app.Service; 
import android.content.Intent; 
import android.os.Handler; 
import android.os.IBinder; 
import android.util.Log; 

public class BroadcastService extends Service { 
    private static final String TAG = "BroadcastService"; 
    public static final String BROADCAST_ACTION = "com.websmithing.broadcasttest.displayevent"; 
    private final Handler handler = new Handler(); 
    Intent intent; 
    int counter = 0; 
    public Student student = new Student("Mario", 20, "Calatafimi", "Informatica"); 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     intent = new Intent(BROADCAST_ACTION); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) { 

     handler.removeCallbacks(sendUpdatesToUI); 
     handler.postDelayed(sendUpdatesToUI, 1000); // 1 second 

    } 

    private Runnable sendUpdatesToUI = new Runnable() { 
     public void run() { 
      DisplayLoggingInfo(); 
      handler.postDelayed(this, 5000); // 10 seconds 
     } 
    }; 

    private void DisplayLoggingInfo() { 
     Log.d(TAG, "entered DisplayLoggingInfo"); 

     intent.putExtra("studente", student); 
     sendBroadcast(intent); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public void onDestroy() { 
     handler.removeCallbacks(sendUpdatesToUI); 
     super.onDestroy(); 
    } 
} 

類學生:

package com.websmithing.broadcasttest; 

import android.os.Parcel; 
import android.os.Parcelable; 
import android.util.Log; 

public class Student implements Parcelable{ 

    String mSName; 
    int mSAge; 
    String mSAddress; 
    String mSCourse;   

    @Override 
    public int describeContents() { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    /* 
    * Storing the Student data to Parcel object 
    */ 
    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     dest.writeString(mSName); 
     dest.writeInt(mSAge); 
     dest.writeString(mSAddress); 
     dest.writeString(mSCourse);  
    } 

    /* 
    * A constructor that initializes the Student object 
    */ 
    public Student(String sName, int sAge, String sAddress, String sCourse){ 
     this.mSName = sName; 
     this.mSAge = sAge; 
     this.mSAddress = sAddress; 
     this.mSCourse = sCourse;   
    } 

    /* 
    * Retrieving Student data from Parcel object 
    * This constructor is invoked by the method createFromParcel(Parcel source) of 
    * the object CREATOR 
    */ 
    private Student(Parcel in){ 
     this.mSName = in.readString(); 
     this.mSAge = in.readInt(); 
     this.mSAddress = in.readString(); 
     this.mSCourse = in.readString();  
    } 

    public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() { 

     @Override 
     public Student createFromParcel(Parcel source) {    
      return new Student(source); 
     } 

     @Override 
     public Student[] newArray(int size) {   
      return new Student[size]; 
     } 
    }; 
} 

非常感謝!

+0

謝謝!但還是到了空的物體! :( – 2014-10-09 15:05:56

回答

2

可能的問題是你從服務發送: intent.putExtra("studente", student);

,並希望在活動接受: Student student = (Student) getIntent().getParcelableExtra("student");

學生 - 學生Ë
使鍵相同,然後再試一次,請

希望它有幫助

UPD:的問題是在你的活動updateUI方法:

private void updateUI(Intent intent) { 
     // String counter = intent.getStringExtra("studente"); 
     Student student = (Student) getIntent().getParcelableExtra("student"); 
     Log.d(TAG, "Ricevuto"); 

     if (student != null) { 
      TextView txtCounter = (TextView) findViewById(R.id.txtCounter); 
      txtCounter.setText(student.mSAge); 
     } else { 
      Log.d(TAG, "Sudente Vuoto"); 
     } 
    } 

您使用getIntent()但你應該使用方法提供intent PARAMS updateUI(Intent intent)

因此,請改變這一行:
Student student = getIntent().getParcelableExtra("student");

Student student = intent.getParcelableExtra("student");
,然後再試一次

+0

謝謝!但仍然到達空物體!:( – 2014-10-09 15:06:36

+0

非常感謝!!!!!!!現在工作好!!!!!致謝 – 2014-10-10 08:18:06

相關問題