| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- package basepedo.ui;
- import android.app.ActivityManager;
- import android.app.NotificationManager;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.IBinder;
- import android.os.Message;
- import android.os.Messenger;
- import android.os.RemoteException;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.TextView;
- import com.baidu.android.pushservice.PushManager;
- import com.xzjmyk.pm.activity.MyApplication;
- import com.xzjmyk.pm.activity.R;
- import com.xzjmyk.pm.activity.ui.base.BaseActivity;
- import com.xzjmyk.pm.activity.ui.me.SettingActivity;
- import com.xzjmyk.pm.activity.util.PreferenceUtils;
- import basepedo.config.Constant;
- import basepedo.service.StepService;
- /**
- * Created by FANGlh on 2016/12/30.
- */
- public class MyPedometerActivity extends BaseActivity implements Handler.Callback {
- //循环取当前时刻的步数中间的间隔时间
- private long TIME_INTERVAL = 500;
- private TextView text_step;
- private Messenger messenger;
- private Messenger mGetReplyMessenger = new Messenger(new Handler(this));
- private Handler delayHandler;
- private String uustep_service_name = "basepedo.service.StepService";
- public static String UU_STEP_NOTICE = "UUSTEPNOTICE";
- ServiceConnection conn = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- try {
- messenger = new Messenger(service);
- Message msg = Message.obtain(null, Constant.MSG_FROM_CLIENT);
- msg.replyTo = mGetReplyMessenger;
- messenger.send(msg);
- } catch (RemoteException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void onServiceDisconnected(ComponentName name) {
- }
- };
- @Override
- public boolean handleMessage(Message msg) {
- switch (msg.what) {
- case Constant.MSG_FROM_SERVER:
- // 更新界面上的步数
- int step_num = msg.getData().getInt("step");
- if (step_num < 2000){
- text_step.setText("今天的你已经走了"+ step_num + "\t步,继续锻炼运动喔!");
- }
- else if (step_num < 3000 && step_num >= 2000){
- text_step.setText("今天的你已经走了"+ step_num + "\t步,还不错!继续努力!");
- }else if (step_num < 6000 && step_num >= 3000){
- text_step.setText("今天的你已经走了"+ step_num + "\t步,加油!快到6000步了!");
- }else if (step_num > 6000 && step_num <=10000){
- text_step.setText("今天的你已经走了"+ step_num + "\t步,真!,快到10000步了!!");
- }else {
- text_step.setText("今天的你已经走了"+ step_num + "\t步,太棒了!天呐,你一定是用了洪荒之力吧,注意休息哦!");
- }
- delayHandler.sendEmptyMessageDelayed(Constant.REQUEST_SERVER, TIME_INTERVAL);
- break;
- case Constant.REQUEST_SERVER:
- try {
- Message msg1 = Message.obtain(null, Constant.MSG_FROM_CLIENT);
- msg1.replyTo = mGetReplyMessenger;
- messenger.send(msg1);
- } catch (RemoteException e) {
- e.printStackTrace();
- }
- break;
- }
- return false;
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_pedometer);
- init();
- PreferenceUtils.putInt(MyPedometerActivity.UU_STEP_NOTICE, 1);
- PushManager.resumeWork(MyApplication.getInstance());
- }
- private void init() {
- text_step = (TextView) findViewById(R.id.text_step);
- delayHandler = new Handler(this);
- setupService();
- NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- manager.cancelAll();
- }
- private void setupService() {
- Intent intent = new Intent(mContext, StepService.class);
- bindService(intent, conn, Context.BIND_AUTO_CREATE);
- startService(intent);
- }
- @Override
- protected void onResume() {
- super.onResume();
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- return super.onCreateOptionsMenu(menu);
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- if (item.getItemId() == android.R.id.home) {
- if (isServiceRunning(uustep_service_name)){
- unbindService(conn);
- startService(new Intent(mContext,StepService.class));
- }
- startActivity(new Intent(mContext, SettingActivity.class));
- }
- return super.onOptionsItemSelected(item);
- }
- @Override
- public void onBackPressed() {
- if (isServiceRunning(uustep_service_name)){
- unbindService(conn);
- startService(new Intent(mContext, StepService.class));
- }
- startActivity(new Intent(mContext, SettingActivity.class));
- super.onBackPressed();
- }
- private boolean isServiceRunning(String servicename) { //判断UU运动服务是否已经运行
- ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
- for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
- if (servicename.equals(service.service.getClassName())) {
- return true;
- }
- }
- return false;
- }
- }
|