Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android.content.Context与android.app.Activity_Android_Android Activity_Android Asynctask_Android Context - Fatal编程技术网

android.content.Context与android.app.Activity

android.content.Context与android.app.Activity,android,android-activity,android-asynctask,android-context,Android,Android Activity,Android Asynctask,Android Context,我想将当前活动的引用传递给不扩展活动的类的方法。我需要将此引用用于活动活动,以便使用getWindow().addFlags(LayoutParams.FLAG\u KEEP\u SCREEN\u打开) 我使用类中的这些setter方法将当前上下文和活动传递给类对象: public class VersionCheck { public Context context; public Activity activity; //---------------------

我想将当前活动的引用传递给不扩展活动的类的方法。我需要将此引用用于活动活动,以便使用
getWindow().addFlags(LayoutParams.FLAG\u KEEP\u SCREEN\u打开)

我使用类中的这些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(摘录如下):

//---------------------------------------------------------------------------------------------------------
//异步任务,用于检查是否有更新版本的应用程序可用
//---------------------------------------------------------------------------------------------------------
私有类UpdateCheckTask扩展了AsyncTask{
HttpClient-HttpClient;
HttpPost-HttpPost;
HttpResponse响应;
输入流输入流;
字节[]数据;
字符串缓冲区;
受保护的void onPreExecute()
{
VersionCheck vc=新的VersionCheck();
//登录时不要锁定屏幕或断开与服务器的连接
activity.getWindow().addFlags(LayoutParams.FLAG\u保持屏幕打开);
//在初始数据检索期间启动进度对话以阻止用户输入
ProcessingDialog=ProgressDialog.show(上下文,“请稍候”,“检查更新”,true,false);
}
@凌驾
受保护的字符串doInBackground(字符串…当前版本)
{

如何获取对活动的引用,以便将其传递给setActivity(Activity)方法?我使用myActivity.this作为上下文的引用。它与活动相同吗?

是,它与活动相同

看这里上下文是超类活动

java.lang.Object

   ↳    android.content.Context

       ↳    android.content.ContextWrapper

           ↳    android.view.ContextThemeWrapper

               ↳    android.app.Activity

这就是我最终使用的,但是为什么getWindow()的类型是android.app.Activity和getPackageManager()类型为android.content.Context。为什么会有区别?看看什么是Context:这是一个抽象类,其实现由android系统提供。它允许访问特定于应用程序的资源和类。因此,对于活动,仅此类可以管理资源和类。Window getWindows():检索“活动”的当前窗口。这可用于直接访问“活动/屏幕”中不可用的部分窗口API。返回“窗口”当前窗口,如果“活动”不可见,则返回null。
java.lang.Object

   ↳    android.content.Context

       ↳    android.content.ContextWrapper

           ↳    android.view.ContextThemeWrapper

               ↳    android.app.Activity