1
Context mcontext;
this.Context= mcontext;
//private Context context;
//this.context=context;
//super(mcontext);
Vibrator mVibrator = (Vibrator) mcontext.getSystemService(Context.VIBRATOR_SERVICE);
mVibrator.vibrate(300)

I searched online a lot for simple code to play a beep and I found one. But I am unable to add and compile few line of code to make the phone vibrate. All the threads I have come across uses getsystemservice(). Link to some code to vibrate on stackoverflow

First getsystemservice() function alone does not work and somewhere I read it needs to be called by a context object. I made a context object. Then it says context needs to be initialized. I tried this.context = context and that gives another error. I am getting into these cycle of compilation errors for what should be a simple job to just vibrate the phone. Please tell me what I am doing wrong here.

This question is different as I took the experience from other threads. As other threads give answer to use getsystemservice() alone but that does not work because it needs a context.

Update: came across this thread here How to pass context from MainActivity to another class in Android?

So I did this

public class MyNonActivityClass{

// variable to hold context
private Context context;

//save the context received via constructor in a local variable

public MyNonActivityClass(Context context){
this.context=context;
}

}

But then I get error mcallback might not have been initialized here.

private final Callback mCallback;

And then where do I add this

MyNonActivityClass mClass = new MyNonActivityClass(this);
threewire
  • 471
  • 1
  • 4
  • 13
  • you need to add permission in manifest file and add Bellow Code it think it should work // Vibrate for 150 milliseconds private void shakeItBaby() { if (Build.VERSION.SDK_INT >= 26) { ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(150, VibrationEffect.DEFAULT_AMPLITUDE)); } else { ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(150); } } – Vivek Barai Dec 07 '17 at 13:30
  • I did add the permission to vibrate in androidmanifest.xml – threewire Dec 07 '17 at 13:37
  • Where are u calling it from? – Ruann Reis Dec 07 '17 at 13:51
  • You seem to lack basic knowledge. It's better to go slow and do some tutorials to learn instead of rushing to make something you don't understand – Tim Dec 07 '17 at 14:39
  • I took some basic source code and put my code into that. The app is working well I just need to add the vibration of the phone...thats it,,, – threewire Dec 07 '17 at 15:09

1 Answers1

1

From where you are calling the Vibrator service ?

If its from activity then,

Vibrator mVibrator = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
    mVibrator.vibrate(300)

or if its from Frgament then,

 Vibrator mVibrator = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE);
    mVibrator.vibrate(300)

or update this in your code like,

void vibrate(Context mcontext ){
    Vibrator mVibrator = (Vibrator) 
    mcontext.getSystemService(Context.VIBRATOR_SERVICE);
    mVibrator.vibrate(300)
}
Durga M
  • 534
  • 5
  • 17
  • this.getsystem... does not work as this gets highlighted in red. getActivity also does not work..I am trying to beep from a user define class not in mainactivity.java. Pardon my little knowledge as my android programming experience is limited. Problem is getsystemservice is not getting resolved. – threewire Dec 07 '17 at 13:37
  • Then you need to pass the context form MainActivity to the user define class. See my updated answer. – Durga M Dec 07 '17 at 13:38
  • So can I not assign it the context in the user defined class. Or if I have to pass it from mainactivity.java then how shall I do it because I am using this to indicate the end of a recording activity in a recording thread. The main thread will not know what is happening in this thread. So can I not take a context itself from the class which it is defined in? Is it the only way. Can I not use view.getcontext or activity.getapplicationactivity or something like that? – threewire Dec 07 '17 at 14:10
  • I am creating an object for a class private VoiceRecorder mVoiceRecorder; And the record function defined in the class voice recorder which is running a thread runnable->record( ). Now specifically how do I manage context here....? – threewire Dec 07 '17 at 14:13
  • I updated my issue further. – threewire Dec 07 '17 at 14:35