Skip to content

Commit

Permalink
进程间通信将messager替换成aidl
Browse files Browse the repository at this point in the history
  • Loading branch information
jiahongfei committed Jun 26, 2017
1 parent f4f5aeb commit 393878e
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 223 deletions.

This file was deleted.

45 changes: 21 additions & 24 deletions app/src/main/java/com/today/step/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,23 @@
import android.util.Log;
import android.widget.TextView;

import com.today.step.lib.BaseConstantDef;
import com.today.step.lib.Logger;
import com.today.step.lib.ISportStepInterface;
import com.today.step.lib.VitalityStepService;

public class MainActivity extends AppCompatActivity {

private static String TAG = "MainActivity";

private static final int REFRESH_STEP_WHAT = 0;

//循环取当前时刻的步数中间的间隔时间
private long TIME_INTERVAL_REFRESH = 500;

private Messenger messenger = new Messenger(new Handler(new TodayStepCounterCall()));
private Messenger mServiceMessenger = null;
private Handler mDelayHandler = new Handler(new TodayStepCounterCall());
private int mStepSum;

private ISportStepInterface iSportStepInterface;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -40,15 +41,15 @@ protected void onCreate(Bundle savedInstanceState) {
bindService(intent, new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iSportStepInterface = ISportStepInterface.Stub.asInterface(service);
try {
Logger.d(TAG, "send msg to fetch step count when onServiceConnected");
mServiceMessenger = new Messenger(service);
Message msg = Message.obtain(null, BaseConstantDef.MSG_FROM_CLIENT);
msg.replyTo = messenger;
mServiceMessenger.send(msg);
mStepSum = iSportStepInterface.getCurrTimeSportStep();
updateStepCount();
} catch (RemoteException e) {
e.printStackTrace();
}
mDelayHandler.sendEmptyMessageDelayed(REFRESH_STEP_WHAT, TIME_INTERVAL_REFRESH);

}

@Override
Expand All @@ -63,28 +64,24 @@ class TodayStepCounterCall implements Handler.Callback{
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case BaseConstantDef.MSG_FROM_SERVER:
// 更新界面上的步数
int type = msg.getData().getInt(VitalityStepService.VITALITY_STEP_TYPE);
if(VitalityStepService.VITALITY_STEP_TYPE_REFRESH_SHOW == type) {
int step = msg.getData().getInt("step");
case REFRESH_STEP_WHAT: {

if (null != iSportStepInterface) {
int step = 0;
try {
step = iSportStepInterface.getCurrTimeSportStep();
} catch (RemoteException e) {
e.printStackTrace();
}
if (mStepSum != step) {
mStepSum = step;
updateStepCount();
}
}
mDelayHandler.sendEmptyMessageDelayed(BaseConstantDef.REQUEST_SERVER, TIME_INTERVAL_REFRESH);
break;
case BaseConstantDef.REQUEST_SERVER:
try {
Message msg1 = Message.obtain(null, BaseConstantDef.MSG_FROM_CLIENT);
msg1.replyTo = messenger;
mServiceMessenger.send(msg1);
} catch (RemoteException e) {
e.printStackTrace();
}
mDelayHandler.sendEmptyMessageDelayed(REFRESH_STEP_WHAT, TIME_INTERVAL_REFRESH);

break;
}
}
return false;
}
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/java/com/today/step/UploadSportStepNetworkImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.today.step;

import android.app.Application;

import com.today.step.lib.Logger;
import com.today.step.lib.UploadSportStepNetwork;
import com.today.step.lib.UploadSportStepResponse;

/**
* Created by jiahongfei on 2017/6/26.
*/

public class UploadSportStepNetworkImpl extends UploadSportStepNetwork {

public UploadSportStepNetworkImpl(Application application) {
super(application);
}

@Override
public void postSportStepNum(String sportStepJson, UploadSportStepResponse uploadSportStepResponse) {
Logger.e("","request : " + sportStepJson);
uploadSportStepResponse.onSuccess("上传成功");
}
}
12 changes: 12 additions & 0 deletions app/src/main/java/lib/ISportStepInterface.aidl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// ISportStepInterface.aidl
package com.today.step.lib;

// Declare any non-default types here with import statements

interface ISportStepInterface {
/**
* 获取当前时间运动步数
*/
int getCurrTimeSportStep();

}
17 changes: 0 additions & 17 deletions app/src/test/java/com/today/step/ExampleUnitTest.java

This file was deleted.

1 change: 1 addition & 0 deletions todaystepcounterlib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ dependencies {
compile 'com.android.support:appcompat-v7:25.3.1'
compile files('libs/lite-orm-1.7.0.jar')
compile 'joda-time:joda-time:2.9.9'
compile 'com.alibaba:fastjson:1.2.33'

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// ISportStepInterface.aidl
package com.today.step.lib;

interface ISportStepInterface {
/**
* 获取当前时间运动步数
*/
int getCurrTimeSportStep();

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.today.step.lib;

import android.app.Application;

/**
* Created by jiahongfei on 2017/6/26.
*/

public class UploadSportStepNetwork {

public UploadSportStepNetwork(Application application){

}

public void postSportStepNum(String sportStepJson,UploadSportStepResponse uploadSportStepResponse){
try {
throw new Exception("如果想上传步数需要继承这个方法");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.today.step.lib;

/**
* Created by jiahongfei on 2017/6/26.
*/

public interface UploadSportStepResponse {

void onSuccess(String response);

void onFails(String error);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.today.step.lib;

import android.os.Parcel;
import android.os.Parcelable;

import com.litesuits.orm.db.annotation.Column;
import com.litesuits.orm.db.annotation.PrimaryKey;
import com.litesuits.orm.db.annotation.Table;
Expand All @@ -9,7 +12,7 @@


@Table("vitalitystepdata")
public class VitalityStepData implements Serializable {
public class VitalityStepData implements Serializable,Parcelable {

// 指定自增,每个对象需要有一个主键
@PrimaryKey(AssignType.AUTO_INCREMENT)
Expand All @@ -20,6 +23,40 @@ public class VitalityStepData implements Serializable {
@Column("step")
private long step;

public VitalityStepData(){

}

protected VitalityStepData(Parcel in) {
id = in.readInt();
date = in.readLong();
step = in.readLong();
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeLong(date);
dest.writeLong(step);
}

@Override
public int describeContents() {
return 0;
}

public static final Creator<VitalityStepData> CREATOR = new Creator<VitalityStepData>() {
@Override
public VitalityStepData createFromParcel(Parcel in) {
return new VitalityStepData(in);
}

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

public int getId() {
return id;
}
Expand Down
Loading

0 comments on commit 393878e

Please sign in to comment.