振动和通知命令不起作用

问题描述 投票:1回答:1

我正在申请火警。基本上我要做的是创建一个后台服务,它会在特定网站启动时通知用户并振动设备。该网站由Node mcu创建,由烟雾或火焰传感器触发。我是android studio的新手,但经过一些研究后我编写了以下代码,但似乎没有用。试图调试它但失败了谁能告诉我出了什么问题?

这是我为服务写的代码 -

 public class ExampleService extends Service {
    int flag=0;
    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

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

        final 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();


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

            @Override
            public void run() {
                if (isInternetAvailable()){
                    flag = 1;
                }else{
                    flag = 0;
                }
                System.out.println("pingHost flag: " + flag );
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        if (flag == 1) {
                            startForeground(1, notification);
                            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;
    }
 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;
        }
    }
}

振动和通知命令都在Timertask之外工作,所以我试图在计时器任务之外运行代码,但它似乎不起作用。此标志和互联网可用逻辑正常工作....在此先感谢!

android timertask
1个回答
0
投票

更新:调试后我做了一些更改,现在代码正常工作。不知道为什么以前它不是,但现在仍然很好。谢谢!

更改后的代码是: -

public class ExampleService extends Service {
    int flag2=0;
    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

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

            @Override
            public void run() {
                if (isInternetAvailable()){
                    flag2 = 1;
                }else{
                    flag2 = 0;
                }
                System.out.println("pingHost flag2: " + flag2 );
                if (flag2 == 1) {
                    notif();
                }
            }
    }, 0, 10000);

        return START_STICKY; }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Nullable
    @Override
    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;
        }
    }

    public void notif() {
        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("Fire Detected in D building")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pendingIntent)
                .build();

        startForeground(1, notification);

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

© www.soinside.com 2019 - 2024. All rights reserved.