如何从 Android 监听器访问应用程序上下文
我有一个 Android 小部件(扩展了 AppWidgetProvider
类),并且我正在尝试从服务侦听器中访问应用程序上下文。我需要访问上下文,以便可以更改应用程序壁纸。这是我得到的错误:无法引用不同方法中定义的内部类内的非最终变量上下文
。我尝试了建议的修复将修改器上下文更改为 Final,但我创建的位图返回为 null。有人可以建议适当的方法来处理这个问题吗?这是代码片段:
private void startWallpaperService(Context context) {
Intent wpIntent = new Intent(context, Wallpaper_Service.class);
context.startService(wpIntent);
Wallpaper_Service wpSrv = Wallpaper_Service.getService();
if(wpSrv != null) {
Log.i(TAG, "Wallpaper Service is not null");
Wallpaper_Service.repetitionInterval=30*1000;
wpSrv.setWallpaperListener(new Wallpaper_Service.WallpaperListener() {
@Override
public void onWallpaperUpdate(int imageId) {
if(wpm == null)
Log.w(TAG, "wpm or context is null");
else {
Bitmap background = BitmapFactory.decodeResource(context.getResources(), R.drawable.image1); // I get a syntax error here
if(background==null){
Log.w(TAG, "background is null");
}else{
try {
wpm.setBitmap(background);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
} else {
Log.e(TAG, "Wallpaper Service failed");
}
}
I have an Android widget (extends the AppWidgetProvider
class) and I'm trying to access the application context from within a service Listener. I need to access the context so that I can change the application wallpaper. Here's the error I get: Cannot refer to a non-final variable context inside an inner class defined in a different method
. I tried the suggested fix to change the modifier context to Final but the Bitmap I create comes back as null. Can someone suggestion the appropriate way to handle this? Here's a snippet of the code:
private void startWallpaperService(Context context) {
Intent wpIntent = new Intent(context, Wallpaper_Service.class);
context.startService(wpIntent);
Wallpaper_Service wpSrv = Wallpaper_Service.getService();
if(wpSrv != null) {
Log.i(TAG, "Wallpaper Service is not null");
Wallpaper_Service.repetitionInterval=30*1000;
wpSrv.setWallpaperListener(new Wallpaper_Service.WallpaperListener() {
@Override
public void onWallpaperUpdate(int imageId) {
if(wpm == null)
Log.w(TAG, "wpm or context is null");
else {
Bitmap background = BitmapFactory.decodeResource(context.getResources(), R.drawable.image1); // I get a syntax error here
if(background==null){
Log.w(TAG, "background is null");
}else{
try {
wpm.setBitmap(background);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
} else {
Log.e(TAG, "Wallpaper Service failed");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在方法的第三行中创建一个最终的应用程序上下文实例吗,例如:
或者
我想您现在可以在内部方法减速中使用“ctx”。
这听起来像是 java 语法限制,而不是代码问题。
希望这有帮助。
-塞尔坎
Can you just create a final application context instance in the third line of your method like:
or
And I presume you can now use the "ctx" in your inner method deceleration.
This just sounds like a java syntax limitation and not a code issue.
Hope this helps.
-serkan
在 95% 的情况下,并且始终在侦听器中,您可以直接使用 getApplicationContext() 来获取上下文。
如果您需要上下文来获取系统资源,请使用
您可以在应用程序中的任何地方使用它!
In 95% of situations and always in Listeners you can use directly getApplicationContext() for getting the context.
If you need context for getting system resources, use
You can use it everywhere in your application!