0

Is there any way to check the device's ability to vibrate before calling "VIBRATOR_SERVICE". I want to check this ability before:

  • Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
  • declaration some variables
  • some scale calculations

It could save some resources, I guess.

Now I see only this way:

if (vibrator.hasVibrator()) {
   /// declaration some variables
   /// some scale calculations
}
PiRocks
  • 1,708
  • 2
  • 18
  • 29
  • "It could save some resources, I guess." can you clarify what you mean by this? If you can't comment you can edit your question. – PiRocks Sep 25 '20 at 03:58
  • If I can check it before the declaration "Vibrator vibrator =", I could avoid unnecessary declaration "Vibrator vibrator =" on devices without the ability to vibrate. – Sergey Terentyev Sep 30 '20 at 18:40
  • "vibrator.hasVibrator()" can check the ability only after "Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);" – Sergey Terentyev Sep 30 '20 at 18:50
  • Okay but why do you want to avoid the declaration? Are you concerned about permissions requests or similar? Or are you concerned about the performance cost? – PiRocks Sep 30 '20 at 23:07
  • I am concerned about the cost of performance, following my friend's advice on the importance of conserving system resources – Sergey Terentyev Oct 01 '20 at 17:20
  • Individual declarations take a tiny percentage of total system resources, unless you have millions of them on the stack(only really happens with deeply nested recursive functions). Additionally the actual declaration may/likely will be optimized out by the compiler. As for the cost of `getSystemService`, I would time the call to see if it does take a substantial amount of time, but is most likely efficient. In general you shouldn't change your code substantially to conserve resources, unless you have measured the impact, or really know what you are doing. – PiRocks Oct 01 '20 at 23:09
  • See here https://stackoverflow.com/questions/15137247/how-does-getsystemservice-work-exactly for more details on what exactly `getSystemService` does. – PiRocks Oct 01 '20 at 23:10
  • Thanks, now I see it. Could you post your answer as an answer to my question? I can't mark it as a correct answer, otherwise. – Sergey Terentyev Oct 05 '20 at 10:08

0 Answers0