计步ok了
This commit is contained in:
@@ -19,7 +19,7 @@
|
||||
<uses-permission android:name="android.permission.CAMERA" />
|
||||
|
||||
<application
|
||||
android:name="com.example.ninefourone.nutritionmaster.NutritionMaster"
|
||||
android:name=".NutritionMaster"
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
@@ -33,6 +33,11 @@
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<receiver
|
||||
android:name=".step.StepStarter"
|
||||
android:enabled="true"
|
||||
android:exported="true"></receiver>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.example.ninefourone.nutritionmaster;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Application;
|
||||
import android.os.Bundle;
|
||||
|
||||
import com.orhanobut.logger.AndroidLogAdapter;
|
||||
import com.orhanobut.logger.Logger;
|
||||
@@ -12,6 +14,7 @@ import com.orhanobut.logger.Logger;
|
||||
|
||||
public class NutritionMaster extends Application {
|
||||
public static NutritionMaster mInstance;
|
||||
private int appCount = 0;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
@@ -25,10 +28,55 @@ public class NutritionMaster extends Application {
|
||||
*/
|
||||
private void init() {
|
||||
Logger.addLogAdapter(new AndroidLogAdapter());
|
||||
Logger.d("Logger初始化成功");
|
||||
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
|
||||
@Override
|
||||
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityStarted(Activity activity) {
|
||||
appCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResumed(Activity activity) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityPaused(Activity activity) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityStopped(Activity activity) {
|
||||
appCount--;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityDestroyed(Activity activity) {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static NutritionMaster getInstance() {
|
||||
return mInstance;
|
||||
}
|
||||
|
||||
/**
|
||||
* app是否在前台
|
||||
*
|
||||
* @return true前台,false后台
|
||||
*/
|
||||
public boolean isForeground() {
|
||||
return appCount > 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,19 +1,16 @@
|
||||
package com.example.ninefourone.nutritionmaster.modules;
|
||||
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.ServiceConnection;
|
||||
import android.os.Bundle;
|
||||
import android.os.IBinder;
|
||||
import android.os.RemoteException;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
|
||||
import com.ToxicBakery.viewpager.transforms.AccordionTransformer;
|
||||
import com.ToxicBakery.viewpager.transforms.CubeInTransformer;
|
||||
import com.ToxicBakery.viewpager.transforms.CubeOutTransformer;
|
||||
import com.ToxicBakery.viewpager.transforms.DepthPageTransformer;
|
||||
import com.ToxicBakery.viewpager.transforms.FlipHorizontalTransformer;
|
||||
import com.ToxicBakery.viewpager.transforms.FlipVerticalTransformer;
|
||||
import com.ToxicBakery.viewpager.transforms.RotateUpTransformer;
|
||||
import com.ToxicBakery.viewpager.transforms.StackTransformer;
|
||||
import com.ToxicBakery.viewpager.transforms.TabletTransformer;
|
||||
import com.ToxicBakery.viewpager.transforms.ZoomInTransformer;
|
||||
import com.example.ninefourone.nutritionmaster.R;
|
||||
import com.example.ninefourone.nutritionmaster.adapter.HomePagerAdapter;
|
||||
import com.example.ninefourone.nutritionmaster.base.BaseActivity;
|
||||
@@ -21,6 +18,9 @@ import com.example.ninefourone.nutritionmaster.ui.NoScrollViewPager;
|
||||
import com.flyco.tablayout.SlidingTabLayout;
|
||||
import com.mxn.soul.flowingdrawer_core.ElasticDrawer;
|
||||
import com.mxn.soul.flowingdrawer_core.FlowingDrawer;
|
||||
import com.today.step.lib.ISportStepInterface;
|
||||
import com.today.step.lib.TodayStepManager;
|
||||
import com.today.step.lib.TodayStepService;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
@@ -75,7 +75,6 @@ public class MainActivity extends BaseActivity {
|
||||
viewPager.setPageTransformer(true, new CubeOutTransformer());
|
||||
slidingTabLayout.setViewPager(viewPager);
|
||||
viewPager.setCurrentItem(1);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -102,4 +101,6 @@ public class MainActivity extends BaseActivity {
|
||||
public void onViewClicked() {
|
||||
mDrawer.openMenu();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,16 +1,28 @@
|
||||
package com.example.ninefourone.nutritionmaster.modules.viewpagerfragments.bodyinformation;
|
||||
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.ServiceConnection;
|
||||
import android.graphics.Color;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
import android.os.Message;
|
||||
import android.os.RemoteException;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.akexorcist.roundcornerprogressbar.IconRoundCornerProgressBar;
|
||||
import com.akexorcist.roundcornerprogressbar.RoundCornerProgressBar;
|
||||
import com.example.ninefourone.nutritionmaster.R;
|
||||
import com.example.ninefourone.nutritionmaster.base.BaseFragment;
|
||||
import com.gelitenight.waveview.library.WaveView;
|
||||
import com.today.step.lib.ISportStepInterface;
|
||||
import com.today.step.lib.TodayStepManager;
|
||||
import com.today.step.lib.TodayStepService;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
@@ -27,8 +39,19 @@ public class BodyInformationFragment extends BaseFragment {
|
||||
@BindView(R.id.progress_2)
|
||||
IconRoundCornerProgressBar progress2;
|
||||
Unbinder unbinder;
|
||||
@BindView(R.id.wave_view)
|
||||
WaveView waveView;
|
||||
@BindView(R.id.step_text_view)
|
||||
TextView stepTextView;
|
||||
|
||||
private int stepCount = 0;
|
||||
private static final int REFRESH_STEP_WHAT = 0;
|
||||
|
||||
//循环取当前时刻的步数中间的间隔时间
|
||||
private long TIME_INTERVAL_REFRESH = 1000;
|
||||
|
||||
private Handler mDelayHandler = new Handler(new TodayStepCounterCall());
|
||||
|
||||
private ISportStepInterface iSportStepInterface;
|
||||
|
||||
|
||||
@Override
|
||||
public int getLayoutResId() {
|
||||
@@ -42,12 +65,6 @@ public class BodyInformationFragment extends BaseFragment {
|
||||
progress1.setMax(70);
|
||||
progress1.setProgress(15);
|
||||
|
||||
int progressColor1 = progress1.getProgressColor();
|
||||
int backgroundColor1 = progress1.getProgressBackgroundColor();
|
||||
int max1 = (int) progress1.getMax();
|
||||
int progress_1 = (int) progress1.getProgress();
|
||||
|
||||
|
||||
progress2.setProgressColor(Color.parseColor("#56d2c2"));
|
||||
progress2.setProgressBackgroundColor(Color.parseColor("#757575"));
|
||||
progress2.setIconBackgroundColor(Color.parseColor("#38c0ae"));
|
||||
@@ -55,13 +72,9 @@ public class BodyInformationFragment extends BaseFragment {
|
||||
progress2.setProgress(147);
|
||||
progress2.setIconImageResource(R.drawable.test_avatar);
|
||||
|
||||
int progressColor2 = progress2.getProgressColor();
|
||||
int backgroundColor2 = progress2.getProgressBackgroundColor();
|
||||
int headerColor2 = progress2.getColorIconBackground();
|
||||
int max2 = (int) progress2.getMax();
|
||||
int progress_2 = (int) progress2.getProgress();
|
||||
|
||||
waveView.setShapeType(WaveView.ShapeType.CIRCLE);
|
||||
initStepCounter();
|
||||
// Logger.d(((MainActivity)getActivity()).getStepCount());
|
||||
}
|
||||
|
||||
|
||||
@@ -82,4 +95,71 @@ public class BodyInformationFragment extends BaseFragment {
|
||||
super.onDestroyView();
|
||||
unbinder.unbind();
|
||||
}
|
||||
|
||||
/**
|
||||
* 计步器初始化
|
||||
*/
|
||||
private void initStepCounter() {
|
||||
TodayStepManager.init(getActivity().getApplication());
|
||||
//开启计步
|
||||
Intent stepCounterStart = new Intent(getActivity(), TodayStepService.class);
|
||||
getActivity().startService(stepCounterStart);
|
||||
getActivity().bindService(stepCounterStart, new ServiceConnection() {
|
||||
@Override
|
||||
public void onServiceConnected(ComponentName name, IBinder service) {
|
||||
iSportStepInterface = ISportStepInterface.Stub.asInterface(service);
|
||||
try {
|
||||
stepCount = iSportStepInterface.getCurrentTimeSportStep();
|
||||
updateStepCount();
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
mDelayHandler.sendEmptyMessageDelayed(REFRESH_STEP_WHAT, TIME_INTERVAL_REFRESH);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceDisconnected(ComponentName name) {
|
||||
|
||||
}
|
||||
}, Context.BIND_AUTO_CREATE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 改变记步UI中的数字
|
||||
*/
|
||||
private void updateStepCount() {
|
||||
stepTextView.setText(stepCount + "步");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 定时器,修改UI
|
||||
*/
|
||||
class TodayStepCounterCall implements Handler.Callback {
|
||||
|
||||
@Override
|
||||
public boolean handleMessage(Message msg) {
|
||||
switch (msg.what) {
|
||||
case REFRESH_STEP_WHAT: {
|
||||
//每隔500毫秒获取一次计步数据刷新UI
|
||||
if (null != iSportStepInterface) {
|
||||
int step = 0;
|
||||
try {
|
||||
step = iSportStepInterface.getCurrentTimeSportStep();
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (stepCount != step) {
|
||||
stepCount = step;
|
||||
updateStepCount();
|
||||
}
|
||||
}
|
||||
mDelayHandler.sendEmptyMessageDelayed(REFRESH_STEP_WHAT, TIME_INTERVAL_REFRESH);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.example.ninefourone.nutritionmaster.step;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
import com.example.ninefourone.nutritionmaster.NutritionMaster;
|
||||
import com.example.ninefourone.nutritionmaster.modules.MainActivity;
|
||||
import com.orhanobut.logger.Logger;
|
||||
|
||||
|
||||
public class StepStarter extends BroadcastReceiver {
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
// TODO: This method is called when the BroadcastReceiver is receiving
|
||||
// an Intent broadcast.
|
||||
// throw new UnsupportedOperationException("Not yet implemented");
|
||||
NutritionMaster nutritionMaster = (NutritionMaster) context.getApplicationContext();
|
||||
if (!nutritionMaster.isForeground()) {
|
||||
Intent mainIntent = new Intent(context, MainActivity.class);
|
||||
mainIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
context.startActivity(mainIntent);
|
||||
}else {
|
||||
Logger.d("已经在计步了");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -31,9 +31,11 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="100dp" />
|
||||
|
||||
<com.gelitenight.waveview.library.WaveView
|
||||
android:id="@+id/wave_view"
|
||||
|
||||
<TextView
|
||||
android:id="@+id/step_text_view"
|
||||
android:textColor="#FFF"
|
||||
android:layout_width="200dp"
|
||||
android:layout_height="200dp" />
|
||||
android:layout_height="50dp" />
|
||||
|
||||
</LinearLayout>
|
||||
Reference in New Issue
Block a user