我使用Accelerometer Sensor来检测手机的动作,当它检测到动作时会发送通知。 当它发送通知我想停止加速计传感器。 好吧,我不知道该怎么做。任何想法的家伙? 我知道我需要取消注册,但我不知道如何以及在哪里取消注册它。
主要活动代码
public class MainActivity extends Activity implements SensorEventListener, Listen {
private BroadcastReceiver statusReceiver;
Sensor accelerometer;
SensorManager sm;
TextView acceleration;
SendValues sv;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
acceleration = (TextView) findViewById(R.id.sensorTxt);
statusReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String type = intent.getStringExtra("message"); //get the type of message from MyGcmListenerService 1 - lock or 0 -Unlock
Log.d(TAG, "TypeMessage: " + type);
if (type == "1") // 1 == lock
{
stopService();
} else {
onPause();
}
}
};}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onPause()
{
super.onPause();
sm.unregisterListener(this);
}
public void stopService()
{
sm.unregisterListener(this);
}
@Override
public void onResume()
{
super.onResume();
sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onSensorChanged(SensorEvent event) {
acceleration.setText("X: " + event.values[0] +
"\nY: " + event.values[1] +
"\nZ: " + event.values[2]);
// get the phone number from the login
SharedPreferences sh = getSharedPreferences("BikePhone", Context.MODE_PRIVATE);
String phone = sh.getString(Params.PHONE, null);
if (event.values[0] >= 5.000 || (event.values[0] <= -5.000) || (event.values[1] >= 5.000) || (event.values[1] <= -5.000)) {
sv = new SendValues(phone, "1");
Gson g = new Gson();
String ans = g.toJson(sv, SendValues.class);
send(sv);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void send(SendValues sv) {
SendThreadCommunication con;
ServerRequest ser = new ServerRequest();
ser.setResponse(this);
Gson gson = new Gson();
String send = gson.toJson(sv, SendValues.class);
ser.addParamaters(Params.VALUES, send);
ser.addServerName(Params.SERVER_URL);
ser.addServletName(Params.BIKE_TO_USER);
con = new SendThreadCommunication(ser);
con.start();
}
@Override
public void good() {
Toast.makeText(getApplication(), "successful transfer", Toast.LENGTH_LONG).show();
}
@Override
public void notGood() {
Toast.makeText(getApplication(), "UNsuccssful transfer", Toast.LENGTH_LONG).show();
}
@Override
public void userGcmNotRegistered() {
Toast.makeText(getApplication(), "There is some problem, please register again to the App", Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:0)
您需要使用unregisterListener (SensorEventListener listener)并在onPause()和onStop()中执行此操作。看起来你很亲密。
查看the latest documentation(因为答案中的某些信息可能已被弃用)以及这些链接:
(How to detect shake event with android?)