我想将当前活动的引用传递给不扩展Activity的类的方法。我需要对活动活动使用此引用,以便我可以使用getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
我使用类中的这些setter方法将当前上下文和活动传递给类对象:
public class VersionCheck {
public Context context;
public Activity activity;
//---------------------------------------------------------------------------------------------------------
//set the current context
//---------------------------------------------------------------------------------------------------------
public void setContext(Context context) {
this.context = context;
}
//---------------------------------------------------------------------------------------------------------
//set the current activity
//---------------------------------------------------------------------------------------------------------
public void setActivity(Activity activity) {
this.activity = activity;
}
对活动的引用用于类中的AsyncTask(下面的摘录):
//---------------------------------------------------------------------------------------------------------
//asynchronous task to check if there is a newer version of the app available
//---------------------------------------------------------------------------------------------------------
private class UpdateCheckTask extends AsyncTask<String, Void, String> {
HttpClient httpclient;
HttpPost httppost;
HttpResponse response;
InputStream inputStream;
byte[] data;
StringBuffer buffer;
protected void onPreExecute ()
{
VersionCheck vc = new VersionCheck();
//do not lock screen or drop connection to server on login
activity.getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
//initiate progress dialogue to block user input during initial data retrieval
ProcessingDialog = ProgressDialog.show(context, "Please Wait", "Checking for Updates", true,false);
}
@Override
protected String doInBackground(String... currentVersion)
{
如何获取对Activity的引用,以便将其传递给setActivity(Activity)方法?我使用myActivity.this作为上下文的参考。活动是否相同?
答案 0 :(得分:3)
是的,活动.................
会是一样的http://developer.android.com/reference/android/app/Activity.html
见这里上下文是Activity的超类......
java.lang.Object
↳ android.content.Context
↳ android.content.ContextWrapper
↳ android.view.ContextThemeWrapper
↳ android.app.Activity