将Obj类从服务发送到活动

时间:2014-10-09 14:38:28

标签: android android-activity service parcel

我从网上下载这个例子,我想修改一个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];
        }
    };
}

非常感谢!!!

1 个答案:

答案 0 :(得分:2)

可能是您从服务发送的问题: intent.putExtra("studente", student);

并希望在活动中收到: Student student = (Student) getIntent().getParcelableExtra("student");

学生 - 学生 E
让密钥相同,然后再试一次,

希望有所帮助

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中提供的updateUI(Intent intent)

所以,请改变这一行:
Student student = getIntent().getParcelableExtra("student");

Student student = intent.getParcelableExtra("student");
再试一次