在我的片段中,我从另一个类调用了一个静态方法
if (getActivity() != null) {
Main.bindMusicService(getActivity().getApplicationContext(), position, songList);
}
主类:
private static Context context;
private static ArrayList<Song> songList;
private static int songIndex;
public static void bindMusicService(Context c, int songPos, ArrayList<Song> songs){
context = c;
songIndex = songPos;
songList = songs;
/*mediaPlayerServiceIntent binds our connection to the MediaPlayerService. */
if (!mServiceIsBound) {
try {
mediaPlayerServiceIntent = new Intent(c, MediaPlayerService.class);
c.bindService(mediaPlayerServiceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
} catch (Exception e) {
Log.e("Main", "Service is not bound!");
}
}else{
Main.mediaPlayerService.startActionPlay(context, songList, songIndex);
}
Log.i("Main","Service is bound!");
}
我收到针对上下文的警告
Do not place Android context classes in static fields; this is a memory leak
将我的数组列表,适配器位置和上下文发送到另一个类中的另一个方法的正确方法是什么?
答案 0 :(得分:3)
您的问题不是发送Context
。您的问题是:
private static Context context;
如果您完全确定需要这样的东西,请替换为:
private static Application context;
调整您的方法以将Application
作为参数,并使对该方法的调用使用getApplication()
而不是getApplicationContext()
。
IOW,您的代码相当安全-您正在使用Application
上下文-但是代码的详细信息使Lint感到紧张。
答案 1 :(得分:0)
在Java中,将不会垃圾收集静态变量或常量。因此最好避免将Context存储在静态变量中
public static void bindMusicService(Context c, int songPos, ArrayList<Song> songs){
context = c;//remove this line
songIndex = songPos;
songList = songs;
/*mediaPlayerServiceIntent binds our connection to the MediaPlayerService. */
if (!mServiceIsBound) {
try {
mediaPlayerServiceIntent = new Intent(c, MediaPlayerService.class);
c.bindService(mediaPlayerServiceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
} catch (Exception e) {
Log.e("Main", "Service is not bound!");
}
}else{
Main.mediaPlayerService.startActionPlay(context, songList, songIndex);
}
Log.i("Main","Service is bound!");
}
将Context传递给静态方法很好,只需请勿将其存储在静态变量中即可。
主要来说,我认为您需要一种比将Context传递给方法更好的方法,可以使用匕首,最后,如果您别无选择,则最好使用Singleton模式或传统的全局Application Context。
匕首的例子:
@Module
public class MainActivityModule {
private final Context context;
public MainActivityModule (Context context) {
this.context = context;
}
@Provides //scope is not necessary for parameters stored within the module
public Context context() {
return context;
}
}
@Component(modules={MainActivityModule.class})
@Singleton
public interface MainActivityComponent {
Context context();
void inject(MainActivity mainActivity);
}
然后
MainActivityComponent mainActivityComponent = DaggerMainActivityComponent.builder()
.mainActivityModule(new MainActivityModule(MainActivity.this))
.build();
以Singleton模式为例
private static SingletonDemo instance;
public static SingletonDemo get() {
if(instance == null) instance = getSync();
return instance;
}
private static synchronized SingletonDemo getSync() {
if(instance == null) instance = new SingletonDemo();
return instance;
}
private SingletonDemo(){
App.get();
}
应用程序级上下文:
public class App extends Application {
private static App instance;
public static App get() { return instance; }
@Override
public void onCreate() {
super.onCreate();
instance = this;
}
}
您必须在AndroidManifest.xml
上定义应用程序类
<application
...
android:name="com.example.YourApplication" >
...
</application>
答案 2 :(得分:0)
另一种简单的方法是在应用程序类中提供公共getter方法,该方法将返回类实例成员上下文,并且该上下文是在应用程序的应用程序类的oncreatw方法中初始化的。