Android从匿名类中获取活动 [英] Android get activity from within anonymous class
问题描述
我是Android开发和Java的新手。这是基本设置:我有一个带有 AsyncTask
的启动画面来检查服务器可用性。在这个主题之后,我在我的活动中进行了回调。这比在 OnPostExecute()
中完成工作更有意义,因为我想在不同的活动中重复使用此任务。
I'm super new to Android development and Java in general. Here's the basic setup: I have a splash screen with an AsyncTask
to check server availability. Following this thread I made a callback in my activity. This makes more sense than doing the work in OnPostExecute()
as I want to reuse this task in different activities.
但是,在我的回调中,我会检查状态是否正常。如果是,它应该启动下一个活动。但是从我的回调的上下文中,我不知道如何获得我的 Activity
引用,我需要它作为Intent的参数。
However, in my callback I do a check if the status is okay. If it is, it should launch the next activity. But from the context of my callback, I don't get how I can get my Activity
reference, which I need as a parameter for the Intent.
这是OnCreate下我的活动中的代码:
This is the code in my Activity under OnCreate:
//Check server status
CheckServiceTask t = new CheckServiceTask(new OnTaskCompleted<ShaggyServiceStatus>() {
@Override
public void onTaskCompleted(ShaggyServiceStatus result) {
Log.i(TAG, "Callback. Result: " + result.getStatus());
ProgressBar pb = (ProgressBar) findViewById(R.id.splash_progress);
pb.setVisibility(View.INVISIBLE);
if (result.getStatusCode() == 999){
TextView t = (TextView) findViewById(R.id.splash_status_text);
t.setText(result.getStatus());
return;
}
Intent i = new Intent(getActivity(), LoginActivity.class);
startActivity(i);
finish();
}
});
t.execute();
失败的部分是 getActivity()
。那个电话不可用。使用这个
会抛出一个错误(我理解,因为我在 OnTaskCompleted
的上下文中)。
The part where this fails is at getActivity()
. That call is not available. Using this
throws an error (which I understand, as I'm in the context of OnTaskCompleted
).
为了完整性,这是OnTaskCompleted的接口:
For completeness, this is the interface for OnTaskCompleted:
public interface OnTaskCompleted<T> {
public void onTaskCompleted(T result);
}
这是CheckServiceTask类:
And this is the CheckServiceTask class:
public class CheckServiceTask extends AsyncTask<Void, Void, ShaggyServiceStatus>{
private static final String TAG = "coo";
public OnTaskCompleted<ShaggyServiceStatus> listener;
public CheckServiceTask (OnTaskCompleted<ShaggyServiceStatus> l){
this.listener = l;
}
@Override
protected ShaggyServiceStatus doInBackground(Void... params) {
try {
Log.i(TAG, "Connecting to server...");
//TODO: Make this a setting
final String url = "https://someplace.com/status";
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
//restTemplate.postForObject("url", bodyObject, ShaggySer.class);
ShaggyServiceStatus sss = restTemplate.getForObject(url, ShaggyServiceStatus.class);
Log.d(TAG, "Got the status.");
return sss;
} catch (Exception e) {
//TODO: Exception handling
Log.e(TAG, e.getMessage(), e);
}
//If we're here, it's not okay.
ShaggyServiceStatus r = new ShaggyServiceStatus("Cannot connect to server", 999, "none");
return r;
}
@Override
protected void onPostExecute(ShaggyServiceStatus result) {
super.onPostExecute(result);
listener.onTaskCompleted(result);
}
}
推荐答案
使用 CurrentClassName.this
Intent i = new Intent(CurrentClassName.this, LoginActivity.class);
getActivity()
:与片段一起使用
返回此片段当前与之关联的活动。
Return the Activity this fragment is currently associated with.
$ b $当你有嵌套的类时,b
class.this
与这个
不同。
这篇关于Android从匿名类中获取活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!