1

Now, I want to make the device vibrate when device show result on screen

some parts of 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);
}

How can I do this?

Thanks!

3 Answers3

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

Note: Don't forget to include permission in AndroidManifest.xml file:

<uses-permission android:name="android.permission.VIBRATE"/>
Taras Yurkiv
  • 126
  • 4
2

Try this. Vibrate for 100 milliseconds

Vibrator v = (Vibrator) this.mContext.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(100);
Prashant M
  • 330
  • 2
  • 10
0

use this code when you receive results

Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            vibrator.vibrate(100);
Nitesh Pareek
  • 362
  • 2
  • 10