在Android应用中,以下方法有什么问题:
public class MyApp extends android.app.Application {
    private static MyApp instance;
    public MyApp() {
        instance = this;
    }
    public static Context getContext() {
        return instance;
    }
}
并传递它到处(例如SQLiteOpenHelper)需要上下文(当然不泄漏)?
我知道Context.getApplicationContext()和View.getContext()的可用性,通过它我可以实际调用Context.getPackageName()来检索应用程序的包名.
如果我从一个View或一个Activity对象可用的方法调用它们,但是如果我想从一个完全独立的类中找到包名而没有View或者Activity,有没有办法(直接或间接)?
Google 正在弃用 Android 11 中的 Android AsyncTask API,并建议java.util.concurrent改用。你可以在这里查看提交
 *
 * @deprecated Use the standard <code>java.util.concurrent</code> or
 *   <a href="https://developer.android.com/topic/libraries/architecture/coroutines">
 *   Kotlin concurrency utilities</a> instead.
 */
@Deprecated
public abstract class AsyncTask<Params, Progress, Result> {
如果您在 Android 中维护带有异步任务的旧代码库,则将来可能需要对其进行更改。我的问题是,使用java.util.concurrent. 它是一个 Activity 的静态内部类。我正在寻找可以使用的东西minSdkVersion 16
private static class LongRunningTask extends AsyncTask<String, Void, MyPojo> {
        private static final String TAG = MyActivity.LongRunningTask.class.getSimpleName();
        private WeakReference<MyActivity> activityReference;
        LongRunningTask(MyActivity context) {
            activityReference = new WeakReference<>(context);
        }
        @Override
        protected MyPojo doInBackground(String... params) { …我目前正在开发一个Android应用程序.当应用程序第一次启动时,我需要做一些事情,即代码仅在程序第一次启动时运行.
我以编程方式创建这样的元素的列表(没有ListView,只是将它们添加到父级):
    <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" 
    android:orientation="vertical" android:layout_weight="1">
    <TextView android:id="@+id/filiale_name"
    android:layout_width="fill_parent" android:layout_height="wrap_content"/>
    <TextView android:id="@+id/lagerstand_text"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:textSize="10sp" android:textColor="@color/red"/>
</LinearLayout>
另外,我在values/colors.xml中定义了一些颜色.如您所见,ID为"lagerstand_text"的TextView默认将其颜色设置为红色.这样可行.
用Java创建元素时,我做到了
lagerstandText.setText("bla");
对于某些元素,我也这样做
lagerstandText.setTextColor(R.color.red);
和其他颜色.虽然我没有调用setTextColor()的元素是红色的,但是所有其他元素都是灰色的,无论我选择哪种颜色(即使它再次是相同的红色).
这是为什么?
我经常发现自己需要访问需要引用某些活动的方法.例如,要使用getWindowManager,我需要访问一些Activity.但是,我使用这些方法的代码通常在其他一些没有引用活动的类中.到目前为止,我已经存储了对主要活动的引用,或者将某些活动的上下文传递给了该类.有没有更好的方法来做到这一点?
我在Android上使用扩展应用程序类来在整个应用程序中共享我的数据.
我可以使用getApplication()我所有活动中的方法.
但是,我创建了一些自定义帮助程序类; 例如,一个XMLHelper不从任何活动/服务类继承的类.
这里的getApplication()方法不可用.
我该如何解决这个问题以及解决这个问题的最佳设计方法是什么?
我有一个枚举,我需要将值显示为本地化字符串.我目前的做法是:
public enum MyEnum {
    VALUE1(R.string.VALUE1),
    VALUE2(R.string.VALUE2),
    .
    .
    VALUE10(R.string.VALUE10);
    private int mResId = -1;
    private MuEnum(int resId) {
        mResId = resId;
    }
    public String toLocalizedString(Resources r) {
        if (-1 != mResId) return (r.getString(mResId));
        return (this.toString());
    }
}
有没有更简单的方法来做到这一点?如果我能以某种方式根据枚举值名称(即'VALUE1')查找资源,我会喜欢它.
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="VALUE1"/>My string</string>
    <string name="VALUE2"/>My string 2</string>
    .
    .
    <string name="VALUE10"/>My string 3</string>
</resources>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
编辑:为了将来参考,这是最适合我的解决方案:
public enum MyEnum {
    VALUE1, 
    VALUE2, 
    . 
    . 
    VALUE10; 
    /**
     * Returns a localized label used to represent this enumeration value. …我正在尝试在没有Dagger的情况下实现MVP(用于学习目的).但我遇到了问题 - 我使用Repository模式从缓存(共享首选项)或网络获取原始数据:
Shared Prefs| 
            |<->Repository<->Model<->Presenter<->View
     Network|
但要把手放在共享首选项上,我必须把它放在某个地方
presenter = new Presenter(getApplicationContext());
我使用onRetainCustomNonConfigurationInstance/ getLastCustomNonConfigurationInstancepair来保持Presenter"保留".
public class MyActivity extends AppCompatActivity implements MvpView {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //...
        presenter = (MvpPresenter) getLastCustomNonConfigurationInstance();
        if(null == presenter){
            presenter = new Presenter(getApplicationContext());
        }
        presenter.attachView(this);
    }
    @Override
    public Object onRetainCustomNonConfigurationInstance() {
        return presenter;
    }
    //...
}
那么如何在没有Dagger的MVP中使用共享首选项并且不会导致Presenter依赖于上下文?
mvp dependencies android dependency-injection sharedpreferences
在Android中,有没有办法获得静态方式的应用程序的上下文?例如,从后台线程中检索它.
谢谢