Skip to content

要不要把某些依赖context的函数改成不依赖的? #17

@PaleNeutron

Description

@PaleNeutron

我发现大部分的Context只是用来获取preference而不是用来确定生命周期。
那么对于这部分的context是不是可以用application context代替? 大致做法如下:

public class zjsApplication extends Application{

    private static zjsApplication instance;
    private static Context context;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        context = getApplicationContext();
        startService(new Intent(this, TimerService.class));
    }

    public static zjsApplication getInstance() {
        return instance;
    }
    public static Context getAppContext() {
        return zjsApplication.context;
    }
}

参考了http://stackoverflow.com/questions/2002288/static-way-to-get-context-on-android, 不知道弊端是不是可以忽略的。

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions