我想将当前活动的引用传递给不扩展活动的类的方法。我需要使用这个对活动活动的引用,以便我可以使用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 作为上下文的参考。Activity 也一样吗?