• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Vibrate not working on android app

I am facing problem in the following code. The app is running correctly without any error but phone is not vibrating even if condition is true. Where did I went wrong ??

Code:
package com.example.sample123;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import com.android_examples.com.webbrowser.R;
import java.net.InetAddress;
import java.util.Timer;
import java.util.TimerTask;
import static com.example.sample123.NotificationService.CHANNEL_ID;


public class ExampleService extends Service {
    int flag=0;
    [USER=1021285]@override[/USER]
    public void onCreate() {
        super.onCreate();
    }

    [USER=1021285]@override[/USER]
    public int onStartCommand(Intent intent, int flags, int startId) {

        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, 0);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Fire Alarm Service")
                .setContentText("Alarm system is functional")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pendingIntent)
                .build();

        startForeground(1, notification);


        Timer repeatTask = new Timer();
        repeatTask.scheduleAtFixedRate(new TimerTask() {

            [USER=1021285]@override[/USER]
            public void run() {
                if (isInternetAvailable()){
                    flag = 1;
                }else{
                    flag = 0;
                }
                System.out.println("pingHost flag: " + flag );
                new Thread(new Runnable() {
                    [USER=1021285]@override[/USER]
                    public void run() {
                        if (flag == 1) {
                            Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                                v.vibrate(VibrationEffect.createOneShot(5000, VibrationEffect.DEFAULT_AMPLITUDE));
                            } else {
                                //deprecated in API 26
                                v.vibrate(5000);
                            }
                        }
                    }
                });
            }
        }, 0, 10000);


        return START_NOT_STICKY;
    }

    [USER=1021285]@override[/USER]
    public void onDestroy() {
        super.onDestroy();
    }

    [USER=1996173]@nullable[/USER]
    [USER=1021285]@override[/USER]
    public IBinder onBind(Intent intent) {
        return null;
    }

    public boolean isInternetAvailable() {
        try {
            InetAddress ipAddr = InetAddress.getByName("google.com");
            //You can replace it with your name
            return !ipAddr.equals("");

        }
        catch (Exception e) {
            return false;
        }
    }
}
 
Last edited:
Back
Top Bottom