手机震动时执行动作

  • 本文关键字:执行 手机 java android
  • 更新时间 :
  • 英文 :


我正在使用抖动检测器librart来检测我的手机是否被共享。它运行良好,在后台以及打开应用程序时都能根据需要提供准确的结果。当手机摇晃时,我需要执行任何操作,但它不起作用。这是我的代码:

ShakeOptions options = new ShakeOptions()
.background(true)
.interval(1000)
.shakeCount(2)
.sensibility(3.0f);
this.shakeDetector = new ShakeDetector(options);
shakeDetector.start(this, new ShakeCallback() {
@Override
public void onShake() {
Toast.makeText(getApplicationContext(), "locationDetails", Toast.LENGTH_SHORT).show();
String msg_txt = "THis is a sammple sms";
String phoneNo = "03036018110";
System.out.println("jamoodgreat");
//sendSMS(phoneNo,msg_txt);
Log.d("event", "onShake");//this is not being printed
Toast.makeText(getApplicationContext(), "Jamshaid", Toast.LENGTH_LONG).show();//this toast is not being displayed as well.
}
});

}

我测试了以下内容,它运行良好(调用/显示Toast(。

public class MainActivity extends AppCompatActivity {
private ShakeDetector shakeDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ShakeOptions options = new ShakeOptions()
.background(true)
.interval(1000)
.shakeCount(2)
.sensibility(2.0f);
shakeDetector = new ShakeDetector(options).start(this, new ShakeCallback() {
@Override
public void onShake() {
Toast.makeText(MainActivity.this, "onShake", Toast.LENGTH_SHORT).show();
}
});
}
}

尽管它有效,但在它被拿起之前,我不得不用力摇晃它。

最新更新