Java 如何在不扩展活动的类中访问android中的可控震源?
我正在尝试使用名为可控震源管理器的类中的以下命令访问可控震源,但该类没有扩展活动Java 如何在不扩展活动的类中访问android中的可控震源?,java,android,android-vibration,Java,Android,Android Vibration,我正在尝试使用名为可控震源管理器的类中的以下命令访问可控震源,但该类没有扩展活动 Vibrator v =(Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 但是eclipse抛出了一些错误,比如 未为类型振动管理器定义方法getSystemService(String) 这是我的全班同学 public class VibrationManager { private static VibrationManager me = null
Vibrator v =(Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
但是eclipse抛出了一些错误,比如
未为类型振动管理器定义方法getSystemService(String)
这是我的全班同学
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
,因为该类没有扩展活动
如果不想使用getSystemService
方法,则需要使用类StribrationManager
来扩展活动,或者需要接收该活动的上下文
只需将代码更改为使用上下文,因为您还需要在静态调用中获取上下文
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){
}
}
您的类没有方法
getSystemService
,因为该类没有扩展活动
如果不想使用getSystemService
方法,则需要使用类StribrationManager
来扩展活动,或者需要接收该活动的上下文
只需将代码更改为使用上下文,因为您还需要在静态调用中获取上下文
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){
}
}
您的类没有方法
getSystemService
,因为该类没有扩展活动
如果不想使用getSystemService
方法,则需要使用类StribrationManager
来扩展活动,或者需要接收该活动的上下文
只需将代码更改为使用上下文,因为您还需要在静态调用中获取上下文
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){
}
}
您的类没有方法
getSystemService
,因为该类没有扩展活动
如果不想使用getSystemService
方法,则需要使用类StribrationManager
来扩展活动,或者需要接收该活动的上下文
只需将代码更改为使用上下文,因为您还需要在静态调用中获取上下文
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){
}
}
如果从不同视图访问上下文时遇到问题,可以执行以下操作:
- 创建扩展应用程序的类(例如MyApplication)
- 在清单中将该类声明为应用程序类,如下所示:
<application android:name="your.project.package.MyApplication" ...
public class MyApplication extends Application { private static MyApplication instance; public void onCreate() { instance = this; } public static MyApplication getInstance() { return instance; } }
MyApplication app = MyApplication.getInstance()
Vibrator v = (Vibrator) app.getSystemService(Context.VIBRATOR_SERVICE);
现在,您不仅可以调用可控震源服务,还可以调用您想要的任何服务…如果从不同视图访问上下文时遇到问题,您可以执行以下操作:
- 创建扩展应用程序的类(例如MyApplication)
- 在清单中将该类声明为应用程序类,如下所示:
<application android:name="your.project.package.MyApplication" ...
public class MyApplication extends Application { private static MyApplication instance; public void onCreate() { instance = this; } public static MyApplication getInstance() { return instance; } }
MyApplication app = MyApplication.getInstance()
Vibrator v = (Vibrator) app.getSystemService(Context.VIBRATOR_SERVICE);
现在,您不仅可以调用可控震源服务,还可以调用您想要的任何服务…如果从不同视图访问上下文时遇到问题,您可以执行以下操作:
- 创建扩展应用程序的类(例如MyApplication)
- 在清单中将该类声明为应用程序类,如下所示:
<application android:name="your.project.package.MyApplication" ...
public class MyApplication extends Application { private static MyApplication instance; public void onCreate() { instance = this; } public static MyApplication getInstance() { return instance; } }
MyApplication app = MyApplication.getInstance()
Vibrator v = (Vibrator) app.getSystemService(Context.VIBRATOR_SERVICE);
现在,您不仅可以调用可控震源服务,还可以调用您想要的任何服务…如果从不同视图访问上下文时遇到问题,您可以执行以下操作:
- 创建扩展应用程序的类(例如MyApplication)
- 在清单中将该类声明为应用程序类,如下所示:
<application android:name="your.project.package.MyApplication" ...
public class MyApplication extends Application { private static MyApplication instance; public void onCreate() { instance = this; } public static MyApplication getInstance() { return instance; } }
MyApplication app = MyApplication.getInstance()
Vibrator v = (Vibrator) app.getSystemService(Context.VIBRATOR_SERVICE);
现在,您不仅可以调用可控震源服务,还可以调用任何您想要的服务…将其作为参数传递给构造函数。将其作为参数传递给构造函数。将其作为参数传递给构造函数。将其作为参数传递给构造函数。很抱歉,请查看您的代码,您没有复制正确的代码,试着理解我在这里所做的只是试图复制我的代码而不理解这个问题对于YouTunks mate来说不是最好的解决方案。所以,无论何时我们想要使用systemService,我们都需要传递上下文变量?或者我们不能创建一个并执行吗?@Beast我刚刚发布了您的hole类,其中包含一些更改,以便您可以复制代码,是的,您需要上下文来访问此方法,以便您可以从任何活动传递此方法关于此代码的注意事项:如果您将activty作为上下文传递,即使活动进入后台并被系统破坏(由于间接静态引用),它也会将其保留在内存中。很抱歉,看看你的代码,你没有复制正确的代码,试着理解我在这里做了什么,只是试图复制我的代码而不理解这个问题对你的配偶来说不是最好的解决方案。所以,无论何时我们想要使用systemService,我们都需要传递上下文变量?或者我们不能创建一个并执行吗?@Beast我刚刚发布了您的hole类,其中包含一些更改,以便您可以复制代码,是的,您需要上下文来访问此方法,以便您可以从任何活动传递此方法关于此代码的注意事项:如果您将activty作为上下文传递,即使活动进入后台并被系统破坏(由于间接静态引用),它也会将其保留在内存中。很抱歉,看看你的代码,你没有复制正确的代码,试着理解我在这里做了什么,只是试图复制我的代码而不理解这个问题对你的配偶来说不是最好的解决方案。所以,无论何时我们想要使用systemService,我们都需要传递上下文变量?或者我们不能创建一个并执行吗?@Beast我刚刚发布了您的hole类,其中包含一些更改,以便您可以复制代码,是的,您需要上下文来访问此代码