问题描述
我想用下面的类名为VibrationManager访问振动器和类不延长活动
I'm trying to access the vibrator using the following in class called VibrationManager and the class is not extending Activity
Vibrator v =(Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
但是Eclipse罚球和错误,如
but eclipse throws and error like
该方法getSystemService(字符串)是未定义的类型VibrationManager
The method getSystemService(String) is undefined for the type VibrationManager
这是我的全班
public class VibrationManager {
private static VibrationManager me = null;
Vibrator v = null;
private Vibrator getVibrator(){
if(v == null){
v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
}
return v;
}
public static VibrationManager getManager() {
if(me == null){
me = new VibrationManager();
}
return me;
}
public void vibrate(long[] pattern){
}
}
请帮忙
推荐答案
您类没有方法 getSystemService
,因为这个类这么想的扩展活动
。
Your class doesn't have the method getSystemService
since this class dosen't extend a Activity
.
如果您wan't使用你需要你的类 getSystemService
方法 VibrationManager
扩展活动,或者你需要接收的一个方面。
If you wan't to use the getSystemService
method you need your class VibrationManager
to extend an activity or you need to receive a context for that.
只要改变你的code使用一个背景下,你需要在你的静态调用也获得了环境。
Just change your code to use a context, for that you will need to also get a context in your static call.
public class VibrationManager {
private static VibrationManager me;
private Context context;
Vibrator v = null;
private Vibrator getVibrator(){
if(v == null){
v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
}
return v;
}
public static VibrationManager getManager(Context context) {
if(me == null){
me = new VibrationManager();
}
me.setContext(context);
return me;
}
private void setContext(Context context){
this.context = context;
}
public void vibrate(long[] pattern){
}
}
这篇关于如何访问振动器在android系统在不延长活性的一类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!