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

Sup*_*ong 1 java android vibration android-vibration android-studio

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

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);
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

谢谢!

Tar*_*kiv 7

Vibrator v = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 1 seconds
v.vibrate(1000);
Run Code Online (Sandbox Code Playgroud)

注意: 不要忘记在AndroidManifest.xml文件中包含权限:

<uses-permission android:name="android.permission.VIBRATE"/>
Run Code Online (Sandbox Code Playgroud)