Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 使用Application上下文而不是Activity Context大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这个问题是我昨天发布的 question的扩展.我发现了一个可能的内存泄漏,并以正确的方式解决了这个问题.其中一个内存泄漏由非静态内部类引起,更精确的是非静态Handler.

但我今天面临新的内存泄漏问题.我已经阅读了这篇post,其实质是基于Context,Activity或Application Context.

这是我从Eclipse Memory Analyzer获得的饼图

我已经看到此饼图的剩余部分发生了变化,在解决了Handler的问题之后,剩余部分增加了10.6 MB.

但是对于上下文问题,问题可疑2给了我一个提示在哪里看

35 instances of "android.widget.FrameLayout",loaded by "<system class loader>" occupy 39 21@R_197_11209@64 (39,94%) bytes. 

Biggest instances:

•android.widget.FrameLayout @ 0x425aa258 - 3 266 728 (3,33%) bytes. 
•android.widget.FrameLayout @ 0x425d9b20 - 3 266 728 (3,33%) bytes. 
•android.widget.FrameLayout @ 0x42609258 - 3 266 728 (3,33%) bytes. 
•android.widget.FrameLayout @ 0x4260a248 - 3 266 728 (3,33%) bytes. 
•android.widget.FrameLayout @ 0x42925960 - 3 266 728 (3,33%) bytes. 
•android.widget.FrameLayout @ 0x429808e0 - 3 266 728 (3,33%) bytes. 
•android.widget.FrameLayout @ 0x429a4350 - 3 266 728 (3,33%) bytes. 
•android.widget.FrameLayout @ 0x429d9b20 - 3 266 728 (3,33%) bytes. 
•android.widget.FrameLayout @ 0x429e5710 - 3 266 728 (3,33%) bytes. 
•android.widget.FrameLayout @ 0x42a28c98 - 3 266 728 (3,33%) bytes. 
•android.widget.FrameLayout @ 0x42a51b80 - 3 266 728 (3,33%) bytes. 
•android.widget.FrameLayout @ 0x46a8caf0 - 3 266 728 (3,33%) bytes.

它告诉我内存泄漏发生在我使用FrameLayout的地方.我使用FrameLayout的唯一地方是在我的启动画面中.

@Override
public void onCreate(Bundle savedInstanceStatE) {

    super.onCreate(savedInstanceStatE);
    setContentView(R.layout.splash);
    utils = new AppUtils(getApplicationContext());
    pd = (ProgressBar) findViewById(R.id.pd);
    progress = (TextView) findViewById(R.id.progress);
    version = (TextView)findViewById(R.id.version);
    prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

    SharedPreferences.Editor prefEditor = prefs.edit();
    prefEditor.putString("firstpw","first");
    prefEditor.commit();

    folder = new CreateApplicationFolder(getApplicationContext(),truE); 
    Boolean isSDPresent = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);

    if (!isSDPresent) {
        ErrorDialog dialog = new ErrorDialog(getApplicationContext(),getresources().getString(R.String.sdCardtitlE),getresources().getString(R.String.sdCardContent),getresources().getString(R.String.ok),2);
        Log.i("SDCARD: ","Is Not Present");
        dialog.canCancelDialog(false);

    } else {
        Log.i("SDCARD: ","Is Present");

        version.setText("Version: " + utils.getAppVersion()); 

        try {
            new ReadConfig(Splash.this,pd,progress).execute("");
        } catch (ParserConfigurationException E) {
            Log.e("Parser: ",e.getmessage());
        } catch (SAXException E) {
            Log.e("Sax: ",e.getmessage());
        } catch (IOException E) {
            Log.e("IO: ",e.getmessage());
        }
    }
}

我怀疑当我将Activity上下文发送到AsyncTask ReadConfig会发生内存泄漏,如下所示:

try {
            new ReadConfig(Splash.this,e.getmessage());
        }

ReadConfig类中,我也使用此上下文在onPostExecute方法中启动一个新的Activity,这将使该上下文保持活动状态.

protected void onPostExecute(String result) {

        if (result.equals("noConfig")) {
            Toast.makeText(context,context.getresources().getString(R.String.configNotFound),Toast.LENGTH_LONG).show();
            Intent mainIntent = new Intent(context,MainClass.class);
            context.startActivity(mainIntent);
        }
        else if(result.equals("pathnotFound")) {

            new ErrorDialog(context,context.getresources().getString(R.String.noBTFoldertitlE),context.getresources().getString(R.String.noBtFolderContent),context.getresources().getString(R.String.exit),2);

        } else {
            Intent mainIntent = new Intent(context,MainClass.class);
            context.startActivity(mainIntent);
        }
    }

我需要上下文来启动这个新的Activity,从资源中获取字符串等等.有什么方法可以做到这一点,以避免内存泄漏?感谢阅读这篇重要文章,如果您需要更多代码或其他任何内容,请添加评论.

提前致谢.

解决方法

@H_607_39@ 您可能应尽可能尝试使用应用程序上下文而不是活动上下文(并非总是可行).

要从任何地方访问应用程序上下文,您可以定义自己的应用程序类

class MyApp extends Application
{
private static MyApp mInstance;

@Override
public void onCreate()
{
   mInstance = this;
}

public static Context context()
{
   return mInstance.getApplicationContext();
}

}

然后在清单中声明此类

<application android:icon="@drawable/ic_launcher" android:label="@String/app_name" android:theme="@style/AppTheme" android:name="com.myapp.MyAppClass">

并在适用的地方使用MyApp.context()

大佬总结

以上是大佬教程为你收集整理的android – 使用Application上下文而不是Activity Context全部内容,希望文章能够帮你解决android – 使用Application上下文而不是Activity Context所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。