Android get activity from within anonymous class(Android 从匿名类中获取活动)
问题描述
总的来说,我是 Android 开发和 Java 的新手.这是基本设置:我有一个带有 AsyncTask
的启动画面,用于检查服务器可用性.在 this 线程之后,我在我的活动中进行了回调.这比在 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 下的 Activity 中的代码:
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()
.该呼叫不可用.使用 this
会引发错误(我理解,因为我在 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);
}
}
推荐答案
Use CurrentClassName.this
Intent i = new Intent(CurrentClassName.this, LoginActivity.class);
getActivity()
:与 Fragments 一起使用
getActivity()
: used with Fragments
返回当前与此片段关联的 Activity.
Return the Activity this fragment is currently associated with.
class.this
与嵌套类时的 this
不同.
class.this
isn't the same as this
when you have nested class.
这篇关于Android 从匿名类中获取活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android 从匿名类中获取活动
基础教程推荐
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 降序排序:Java Map 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01