0

앱을 제작하고 있습니다. 제 목적은 휴대 전화를 흔들어 카메라 작동을 시작하는 것입니다. 나는 토스트를 시작할 수 있지만 액티비티는 시작할 수 없다. 토스트를 표시하는 대신 카메라 작동을 시작하려고합니다. 여기 내 코드, 흔들어서 카메라 작동을 시작하는 방법입니다.전화를 흔들어서 카메라 작동을 시작하는 방법

Background_service.java

public class Background_service extends Service implements SensorEventListener 
{ 
    boolean flag=false; 
    private long lastUpdate; 
    SensorManager sensorManager; 
    int count=0; 
    final static int cameraData = 0; 
    private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200; 
    private Uri fileUri; 
    ImageView iv; 
    Intent I; 
    Bitmap bmp; 

    @Override 
    public IBinder onBind(Intent intent) 
    { 

     return null; 
    } 
    public void onCreate() 
    { 
     flag=true; 
     Log.d(MainShake.TAG, "onCreate"); 
     super.onCreate(); 
    } 

    public void onDestroy() 
    { 
     flag=false; 
     Log.d(MainShake.TAG, "onDestroy"); 
     super.onDestroy(); 
    } 

    public void onStart(Intent intent, int startId) 
    { 
     sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 
     sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); 
     lastUpdate = System.currentTimeMillis(); 


    } 

    @Override 
    public void onAccuracyChanged(Sensor sensor, int accuracy) 
    { 
    } 

    private void getAccelerometer(SensorEvent event) 
    { 
     if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) 
     { 
      float[] values = event.values; 
      // Movement 
      float x = values[0]; 
      float y = values[1]; 
      float z = values[2]; 

      float accelationSquareRoot = (x * x + y * y + z * z)/(SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH); 
      long actualTime = System.currentTimeMillis(); 
      if (accelationSquareRoot >= 2) 
      { 
       if (actualTime - lastUpdate < 2000) 
       { 
        count++; 
        return; 
       } 
       Context context = getApplicationContext(); 
       CharSequence text = "Please fill all the field...!"; 
       int duration = Toast.LENGTH_SHORT; 

       Toast toast = Toast.makeText(context, text, duration); 
       toast.show(); 

      } 
     } 
    } 


    public void onSensorChanged(SensorEvent event) 
    { 

     getAccelerometer(event); 

    } 
    protected void onResume() 
    { 
     // register this class as a listener for the orientation and 
     // accelerometer sensors 
     sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),    SensorManager.SENSOR_DELAY_NORMAL); 
    } 

    protected void onPause() 
    { 
     // unregister listener 
     sensorManager.unregisterListener(this); 

    } 

} 

ShakeEventListner.java

public class ShakeEventListener implements SensorEventListener { 


     /** Minimum movement force to consider. */ 
     private static final int MIN_FORCE = 10; 

     /** 
     * Minimum times in a shake gesture that the direction of movement needs to 
     * change. 
     */ 
     private static final int MIN_DIRECTION_CHANGE = 3; 

     /** Maximum pause between movements. */ 
     private static final int MAX_PAUSE_BETHWEEN_DIRECTION_CHANGE = 200; 

     /** Maximum allowed time for shake gesture. */ 
     private static final int MAX_TOTAL_DURATION_OF_SHAKE = 400; 

     /** Time when the gesture started. */ 
     private long mFirstDirectionChangeTime = 0; 

     /** Time when the last movement started. */ 
     private long mLastDirectionChangeTime; 

     /** How many movements are considered so far. */ 
     private int mDirectionChangeCount = 0; 

     /** The last x position. */ 
     private float lastX = 0; 

     /** The last y position. */ 
     private float lastY = 0; 

     /** The last z position. */ 
     private float lastZ = 0; 

     /** OnShakeListener that is called when shake is detected. */ 
     private OnShakeListener mShakeListener; 

     /** 
     * Interface for shake gesture. 
     */ 
     public interface OnShakeListener { 

     /** 
     * Called when shake gesture is detected. 
     */ 
     void onShake(); 
     } 

     public void setOnShakeListener(OnShakeListener listener) { 
     mShakeListener = listener; 
     } 

     @Override 
     public void onSensorChanged(SensorEvent se) { 
     // get sensor data 
     float x = se.values[SensorManager.DATA_X]; 
     float y = se.values[SensorManager.DATA_Y]; 
     float z = se.values[SensorManager.DATA_Z]; 

     // calculate movement 
     float totalMovement = Math.abs(x + y + z - lastX - lastY - lastZ); 

     if (totalMovement > MIN_FORCE) { 

      // get time 
      long now = System.currentTimeMillis(); 

      // store first movement time 
      if (mFirstDirectionChangeTime == 0) { 
      mFirstDirectionChangeTime = now; 
      mLastDirectionChangeTime = now; 
      } 

      // check if the last movement was not long ago 
      long lastChangeWasAgo = now - mLastDirectionChangeTime; 
      if (lastChangeWasAgo < MAX_PAUSE_BETHWEEN_DIRECTION_CHANGE) { 

      // store movement data 
      mLastDirectionChangeTime = now; 
      mDirectionChangeCount++; 

      // store last sensor data 
      lastX = x; 
      lastY = y; 
      lastZ = z; 

      // check how many movements are so far 
      if (mDirectionChangeCount >= MIN_DIRECTION_CHANGE) { 

       // check total duration 
       long totalDuration = now - mFirstDirectionChangeTime; 
       if (totalDuration < MAX_TOTAL_DURATION_OF_SHAKE) { 
       mShakeListener.onShake(); 
       resetShakeParameters(); 
       } 
      } 

      } else { 
      resetShakeParameters(); 
      } 
     } 
     } 

     /** 
     * Resets the shake parameters to their default values. 
     */ 
     private void resetShakeParameters() { 
     mFirstDirectionChangeTime = 0; 
     mDirectionChangeCount = 0; 
     mLastDirectionChangeTime = 0; 
     lastX = 0; 
     lastY = 0; 
     lastZ = 0; 
     } 

     @Override 
     public void onAccuracyChanged(Sensor sensor, int accuracy) { 
     } 

    } 

답변

2

그냥 교체

Toast toast = Toast.makeText(context, text, duration); 
toast.show(); 

Intent myIntent = new Intent(Background_service.this, YOUR_ACTIVITY.class);  
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
Background_service.this.startActivity(myIntent); 
0 서비스에서 작업을 시작할 때

FLAG_ACTIVITY_NEW_TASK이 필요합니다.

+0

작동하지 않아 카메라 작동을 시작하고 싶습니다. –

+1

FLAG에 대한 내 의견을 확인하십시오. –