【问题标题】:How to make an Android device vibrate when device show result on screen? [duplicate]当设备在屏幕上显示结果时如何使 Android 设备振动? [复制]
【发布时间】:2016-10-26 11:28:21
【问题描述】:

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

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

我该怎么做?

谢谢!

【问题讨论】:

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


    【解决方案1】:
    Vibrator v = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
    // Vibrate for 1 seconds
    v.vibrate(1000);
    

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

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

    【讨论】:

      【解决方案2】:

      试试这个。振动 100 毫秒

      Vibrator v = (Vibrator) this.mContext.getSystemService(Context.VIBRATOR_SERVICE);
      v.vibrate(100);
      
      【解决方案3】:

      收到结果时使用此代码

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-30
        • 1970-01-01
        • 1970-01-01
        • 2011-03-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多