BenjaFriend
BenjaFriend

Reputation: 664

How to use the Vibrator in an Android Library Module

My goal is to make a simple library with some basic vibration methods so that I can have a more diverse and native set of options in some other frameworks. My problem is that I cannot use getActivity() or getSystemService(Context.VIBRATOR_SERVER) inside the library.

Here is the library class I have so far:

import android.os.Vibrator;
public class Vibration {

private Vibrator _vibrator;

     public static void Vibrate(long milliseconds ){
         Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
         //getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
         v.vibrate(VibrationEffect.createOneShot(
            milliseconds,VibrationEffect.DEFAULT_AMPLITUDE));
     }
}

I am getting Cannot resolve symbol 'Context', and the same for getSystemService

Anyone have an idea of to call these methods from the library?

Upvotes: 1

Views: 739

Answers (1)

Rutvik Bhatt
Rutvik Bhatt

Reputation: 3295

you need to pass context object in Vibrate() method as below:

public static void Vibrate(long milliseconds, Context context) {
    Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    v.vibrate(VibrationEffect.createOneShot(
            milliseconds, VibrationEffect.DEFAULT_AMPLITUDE));
}

and for Context you need to importimport android.content.Context;

Upvotes: 2

Related Questions