当设备在屏幕上显示结果时如何让Android设备振动?

时间:2016-10-26 11:28:22

标签: java android android-studio vibrate android-vibration

现在,我想让设备在屏幕上显示结果时振动

MainActivity.java

的某些部分
public void onResult(String result) {
    String tres = "\n";
    try {
        JSONObject j = new JSONObject(result);
        JSONObject j1 = j.getJSONObject("status");
        int j2 = j1.getInt("code");
        if(j2 == 0){
            JSONObject metadata = j.getJSONObject("metadata");
            if (metadata.has("custom_files")) {
                JSONArray musics = metadata.getJSONArray("custom_files");
                for(int i=0; i<musics.length(); i++) {
                    JSONObject tt = (JSONObject) musics.get(i); 
                    String title = tt.getString("title");
                    tres = tres + (i+1) + ".  Title: " + title + "\n";
                }
            }
        }else{
            tres = result;
        }
    } catch (JSONException e) {
        tres = result;
        e.printStackTrace();
    }

    mResult.setText(tres);
}

我该怎么做?

谢谢!

3 个答案:

答案 0 :(得分:7)

Vibrator v = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 1 seconds
v.vibrate(1000);

注意: 别忘了在AndroidManifest.xml文件中包含权限:

<uses-permission android:name="android.permission.VIBRATE"/>

答案 1 :(得分:2)

试试这个。振动100毫秒

Vibrator v = (Vibrator) this.mContext.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(100);

答案 2 :(得分:0)

收到结果时使用此代码

Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            vibrator.vibrate(100);