Add vibration when ringing

This commit is contained in:
Alessandro Ferro 2022-10-27 12:55:31 +02:00
parent c58fd719b3
commit 884996ca71
2 changed files with 23 additions and 1 deletions

View File

@ -13,6 +13,8 @@
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"

View File

@ -3,11 +3,14 @@ package com.xfarrow.locatemydevice;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
@ -15,7 +18,7 @@ import android.widget.Button;
public class RingerActivity extends AppCompatActivity {
private Ringtone ringtoneManager;
private Vibrator v;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -56,10 +59,27 @@ public class RingerActivity extends AppCompatActivity {
ringtoneManager = RingtoneManager.getRingtone(this, ringtone);
ringtoneManager.setVolume(1f);
ringtoneManager.play();
v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Start without a delay
// Vibrate for 500 milliseconds
// Sleep for 500 milliseconds
long[] pattern = {0, 500, 500};
// The '0' here means to repeat indefinitely
// '0' is actually the index at which the pattern keeps repeating from (the start)
// To repeat the pattern from any other point, you could increase the index, e.g. '1'
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
v.vibrate(VibrationEffect.createWaveform(pattern, 0));
}
else{
v.vibrate(pattern,0);
}
}
private void stopRinging(){
ringtoneManager.stop();
v.cancel();
finishAffinity();
}