2014-10-09 80 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

相关问题