当手机被摇动时执行动作。

huangapple go评论75阅读模式
英文:

Perform an Action when phone is shaked

问题

我正在使用shake-detector-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.
    }
});
英文:

I'm using shake-detector-librart to detect whether my phone is being shared or not. It is working fine, gives the exact results as needed in the background as well as while the application is opened. I need to perform any action when the phone is being shaken, but it isn't working for that.
Here's my code:

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


  
}

答案1

得分: 1

我测试了以下内容,它可以正常工作(“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();
            }
        });

    }
}

尽管它可以工作,但我必须 vigorously 摇晃设备才能被检测到。

英文:

I tested the following and it works fine (The Toast gets called/displayed).

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

    }
}

Even though it worked, I had to shake the device vigorously before it was picked up.

huangapple
  • 本文由 发表于 2020年5月5日 12:33:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/61605844.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定