抖动和GPS问题的背景感应 [英] background sensing of shake and GPS problems

查看:118
本文介绍了抖动和GPS问题的背景感应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MainAcivity.java

MainAcivity.java

    package com.example.background_shake;

import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;


public class MainActivity extends Activity implements OnClickListener {
    Button ON,OFF;
private SensorManager sensorManager;
private long lastUpdate;

Intent i1;
public static final String TAG = "ServicesDemo";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    //reseting the window propertys
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    //super method
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ON=(Button)findViewById(R.id.ON);
    OFF=(Button)findViewById(R.id.OFF);
    ON.setOnClickListener(this);
    OFF.setOnClickListener(this);
}
public void onClick(View src) {
    switch (src.getId()) {
    case R.id.ON:
      Log.d(TAG, "onClick: starting srvice");
      startService(new Intent(this, Background_service.class));
      break;
    case R.id.OFF:
      Log.d(TAG, "onClick: stopping srvice");
      stopService(new Intent(this, Background_service.class));
      break;
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
  }

Background_service.java

Background_service.java

    package com.example.background_shake;

import java.util.List;
import java.util.Locale;
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.location.Address;
import android.location.Geocoder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class Background_service extends Service implements SensorEventListener{
    boolean flag=false;
    private long lastUpdate;
    SensorManager sensorManager;
            int count=0;
@Override
public IBinder onBind(Intent intent) {

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

}
public void onDestroy() {

    flag=false;
    Log.d(MainActivity.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];
    double latitude=0;
    double longitude=0;
    String location_message;
    GPSTracker gps;

    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;
      }

      lastUpdate = actualTime;

      {
          gps=new GPSTracker(Background_service.this);
          if((gps.canGetLocation())&&(count==3)){
                      count=0;    
              latitude = gps.getLatitude();
              longitude = gps.getLongitude();
        //  \n is for new line
        //  Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();    
              Geocoder gcd = new Geocoder(Background_service.this, Locale.getDefault());
              try{
                  List<Address> addresses = gcd.getFromLocation(latitude, longitude, 1);
                  location_message= addresses.get(0).getLocality();
                  if ((addresses.size() > 0)&&(flag))
                  {
                      Toast.makeText(getApplicationContext(), "Your Location is " +addresses.get(0).getLocality()
                              , Toast.LENGTH_LONG).show();
                                      count=0;
                  }

              }catch(Exception e)
              {
                  System.out.print(e);
              }
          }else{
        //  can't get location
        //  GPS or Network is not enabled
        // Ask user to enable GPS/network in settings
        Toast.makeText(getApplicationContext(), "Switch on gps"
                , Toast.LENGTH_LONG).show();
        gps.showSettingsAlert();
    }}


      /*Toast.makeText(this, "Device was shuffed", Toast.LENGTH_SHORT)
          .show();
      if (color) {
        view.setBackgroundColor(Color.GREEN);

      } else {
        view.setBackgroundColor(Color.RED);
      }*/

    }}
  }
public void onSensorChanged(SensorEvent event) {

    getAccelerometer(event);


}
protected void onResume() {
    //super.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);

}

}

上面的代码用于感测震动参数并为您提供当前位置的应用。
但是问题是即使我的GPS关闭了,我也正在获取当前位置。

The above code is for an app that senses the shake parameter and gives you the current location. But the problem is even though my GPS is off i am getting my current location.

我还想知道两件事
1.是否即使您锁定了手机,服务也可以正常工作。如果没有,则必须对我的代码进行哪些修改才能使其正常工作?
2.如果我摇动手机一次,我的应用程序就会输出输出,但是我希望我的应用程序只有连续摇动3-4次,才能给出想要的结果。是否可以做到?

Also i want to know two things 1. Does a Service work even if you lock your phone?If no, what modifications must be made to my code so that it works? 2. My app gives the output if i shake my mobile once but i want my app to give the desired result only if i shake it continuously for 3-4 times.Is it possible to do that?

推荐答案

akajaymo已经在这里回答了Q1。

Q1 is already answered here by akajaymo.

对于Q2,而不是每次摇动手机时,应用都会产生所需的输出,创建两个静态变量 int count long last

For Q2, instead of having the app produce the desired output every time the phone is shaken, create two static variables int count and long last.

每次摇动电话时,您都要检查(System.currentTimeMillis()-last)是否<考虑到抖动的1000 / *相隔不超过1秒* / ,如果是,则增加计数。

Every time the phone is shaken, you check to see if (System.currentTimeMillis() - last) < 1000/* considering the shakes are not more than 1 second apart */ and, if so, you increase count.

如果计数达到3或4,就可以了:只需生成所需的输出即可。如果不是,则什么也不会产生。

If count reaches 3 or 4, you're done: just produce the desired output. If not, produce nothing.

如果抖动间隔超过1000ms,只需将 count 设置为1。

If the shakes are more than 1000ms apart, just set count to 1.

此外,每次检测到抖动时,请使 last = System.currentTimeMillis()。最后一次更新必须在我提到的第一个检查之后 完成。

Also, every time you detect a shake, make last = System.currentTimeMillis(). This update to last must be done after the first check that I mentioned.

这篇关于抖动和GPS问题的背景感应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆