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