«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

올해는 머신러닝이다.

안드로이드 만보기 소스 본문

Android/Tip&Tech

안드로이드 만보기 소스

행복한 수지아빠 2011. 9. 28. 16:08

안녕하세요~ 이번에 안드로이드에 대해 독학으로 공부하고 있는 학생입니다.

 

기본 지식이 많이 없는 편이라 이렇게 질문을 올리게 되었습니다.

 

귀찮더라두 끝까지 봐주시고 저에게 좋은 스승이 되어주세요

 

질문은 가속도 센서를 이용해서 만보기를 만들어 볼라고 하는데요

 

여차여차 소스를 구하게 되었습니다. 그래서 이것을 토대로 공부를 하고 싶어하는데요

 

소스들이 무엇을 의미하는지 몰라서 이렇게 질문을 하게 되었습니다 스승님들 저에게 가르침을 주세요~

 

자세한 설명 부탁드리겠습니다!! (__)꾸벅!

 

 

 

[MainActivity.java]

 

package com.androday.test;

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.view.View;
import! android.view.View.OnClickListener;
import! android.widget.Button;
import! android.widget.TextView;
import! android.widget.Toast;

public class MainActivity extends Activity {
   
 
    Button btnStopService;
   
    Intent intentMyService;       

    BroadcastReceiver receiver;
   
    boolean flag = true;
   
    Toast toast;
   
    TextView CountText;
   
    String serviceData;
   
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);     
          
        intentMyService = new Intent(this,MyServiceIntent.class);   
        //실행되기 원하는 서비스 등록
               
      
        receiver = new MyMainLocalRecever();       
       
        CountText = (TextView)findViewById(R.id.TextView01);      
              
        btnStopService = (Button)findViewById(R.id.btnStopService);
        //서비스 중지
       
       
        btnStopService.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    
    
    if(flag)
    {
     
     
     btnStopService.setText("Stop !!");
     
     // TODO Auto-generated method stub
     try{
      
      IntentFilter mainFilter = new IntentFilter("com.androday.test.step");       
           
      registerReceiver(receiver, mainFilter);
      
      startService(intentMyService);     
      //txtMsg.setText("After stoping Service:\n"+service.getClassName()); 
      Toast.makeText(getApplicationContext(), "서비스 시작", 1).show();
     }
     catch (Exception e) {
      // TODO: handle exception
      Toast.makeText(getApplicationContext(), e.getMessage(), 1).show();
     }
    }
    
    
    else
    {

    
     btnStopService.setText("Go !!");
     
     // TODO Auto-generated method stub
     try{
      
      unregisterReceiver(receiver);
      
      stopService(intentMyService);      
      
      Toast.makeText(getApplicationContext(), "서비스 중지", 1).show();
      //txtMsg.setText("After stoping Service:\n"+service.getClassName());     
     }
     catch (Exception e) {
      // TODO: handle exception
      Toast.makeText(getApplicationContext(), e.getMessage(), 1).show();
     }     
    }  
    
    flag = !flag;
    
   }
  });
       
        }
   
   
    class MyMainLocalRecever extends BroadcastReceiver {

     @Override
     public void onReceive(Context context, Intent intent) {
      // TODO Auto-generated method stub
      
      serviceData = intent.getStringExtra("serviceData");
      
      CountText.setText(serviceData);
      
      Toast.makeText(getApplicationContext(), "Walking . . . ", 1).show();    

     }

    }
   
   
   
}


 

 

[MyServiceIntent.java]

 

package com.androday.test;

import! android.app.Service;


import! android.content.Intent;
import! android.hardware.Sensor;
import! android.hardware.SensorEvent;
import! android.hardware.SensorEventListener;
import! android.hardware.SensorManager;
import! android.os.IBinder;
import! android.util.Log;

public class MyServiceIntent extends Service implements SensorEventListener  {
 
 
 int count = values.Step;
 
 private long lastTime;
    private float speed;
    private float lastX;
    private float lastY;
    private float lastZ;
  

    private float x, y, z;
    private static final int SHAKE_THRESHOLD = 800;
  

    private static final int DATA_X = SensorManager.DATA_X;
    private static final int DATA_Y = SensorManager.DATA_Y;
    private static final int DATA_Z = SensorManager.DATA_Z;
  

    private SensorManager sensorManager;
    private Sensor accelerormeterSensor;
 
   
 @Override
 public IBinder onBind(Intent intent) {
  // TODO Auto-generated method stub
  return null;
 }

 @Override
 public void onCreate() {
  // TODO Auto-generated method stub  
  super.onCreate();
  Log.i("MyServiceIntent","Service is Create"); 
  
   sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
         accelerormeterSensor = sensorManager
                 .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);    

     }    
 
 @Override
 public void onStart(Intent intent, int startId) {
  // TODO Auto-generated method stub
  super.onStart(intent, startId);
  Log.i("MyServiceIntent","Service is started"); 
  

        if (accelerormeterSensor != null)
            sensorManager.registerListener(this, accelerormeterSensor,
                    SensorManager.SENSOR_DELAY_GAME);
 }

 @Override
 public void onDestroy() {
  // TODO Auto-generated method stub
  super.onDestroy();    
  Log.i("MyServiceIntent","Service is destroy");  
  
    if (sensorManager != null)
             sensorManager.unregisterListener(this);

 }

 @Override
 public void onAccuracyChanged(Sensor sensor, int accuracy) {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void onSensorChanged(SensorEvent event) {
  // TODO Auto-generated method stub
     if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
             long currentTime = System.currentTimeMillis();
             long gabOfTime = (currentTime - lastTime);
   

             if (gabOfTime > 100) {
                 lastTime = currentTime;
   

                 x = event.values[SensorManager.DATA_X];
                 y = event.values[SensorManager.DATA_Y];
                 z = event.values[SensorManager.DATA_Z];
   

                 speed = Math.abs(x + y + z - lastX - lastY - lastZ) / gabOfTime * 10000;

   

if (speed > SHAKE_THRESHOLD) {
                  
Log.e("Step!", "SHAKE");
                  
Intent myFilteredResponse = new Intent("com.androday.test.step");
                  
values.Step = count++;
                
String msg = values.Step + "" ;
                  myFilteredResponse.putExtra("serviceData", msg);
                  
                  sendBroadcast(myFilteredResponse);

                 }
                 lastX = event.values[DATA_X];
                 lastY = event.values[DATA_Y];
                 lastZ = event.values[DATA_Z];
             }
         }

 } 

}

 

[values.java]

 

package com.androday.test;

public class values {

 public static int Step = 0;
}

 

 

스승님들 좋은 가름침 부탁드리겠습니다!!