我已经创建了一个服务,该服务从主活动中调用,并将其传递给一个简单的变量,以便从服务内部访问并烘烤屏幕。我似乎无法找到正确的代码从服务内部访问变量。任何帮助将不胜感激。谢谢。Android:访问变量传递到服务
主要活动从调用点击一个按钮监听器里的服务:
@Override
public void onClick(View v) {
Intent eSendIntent = new Intent(getApplicationContext(), eSendService.class);
eSendIntent.putExtra("extraData", "somedata");
startService(eSendIntent);
}
eSendService服务类代码:
public class eSendService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
// This is the code that I am struggling with. Not sure how to access the
// variable that was set in the intent. Please advise.
// The below code gives the following error in eclipse:
// The method getIntent() is undefined for the type eSendService
Bundle extras = getIntent().getExtras();
}
}
再次感谢您的任何和所有帮助。我似乎无法找到一个简单的例子,告诉我如何做到这一点。谢谢。
Hi E Z Hart。我在super.onCreate()方法之后尝试了它,并且没有任何更改。 Eclipse强调“getIntent()”,当我将鼠标移到它上面时,我得到这个错误“方法getIntent()未定义类型eSendService”。谢谢你的帮助。 – RayJamesFun 2011-03-09 23:31:23