如何让 Android 设备在屏幕上显示结果时振动?
How to make an Android device vibrate when device show result on screen?
现在,我想让设备在屏幕上显示结果时振动
部分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);
}
我该怎么做?
谢谢!
试试这个。振动 100 毫秒
Vibrator v = (Vibrator) this.mContext.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(100);
收到结果时使用此代码
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(100);
Vibrator v = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 1 seconds
v.vibrate(1000);
注:
不要忘记在 AndroidManifest.xml 文件中包含权限:
<uses-permission android:name="android.permission.VIBRATE"/>
现在,我想让设备在屏幕上显示结果时振动
部分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);
}
我该怎么做?
谢谢!
试试这个。振动 100 毫秒
Vibrator v = (Vibrator) this.mContext.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(100);
收到结果时使用此代码
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(100);
Vibrator v = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 1 seconds
v.vibrate(1000);
注: 不要忘记在 AndroidManifest.xml 文件中包含权限:
<uses-permission android:name="android.permission.VIBRATE"/>