在服务中使用 picasso 加载图像 class
Load the image with picasso in a service class
我对 android 应用程序中的一点印象深刻。我正在开发一个应用程序,我在其中使用服务 class,并且我通过我的服务 class 接收通知。现在有一个场景,我想更改我的 phone 应用程序的墙纸,我在我的代码中成功地做到了这一点,但是当我投入服务 class 时,应用程序崩溃并给出错误 picasso should 运行 在主线程中。实际上在我的应用程序中有一个场景,如果我收到通知,即使我的应用程序在后台,那么壁纸应该被更改。这是我的代码:
public class FirebaseMessagingServices extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
RemoteMessage.Notification notification = remoteMessage.getNotification();
if (data.isEmpty()) {
System.out.println("FCM type is Notification");
parseNotificationMessage(notification);
}
else {
System.out.println("FCM type is Data");
parseDataMessage(remoteMessage);
}
}
这里我正在更改接收消息的方式,在接收消息时我会检查消息并生成通知
private void parseDataMessage(RemoteMessage remoteMessage) {
String notification_title =remoteMessage.getData().get("title");
String notification_message = remoteMessage.getData().get("message");
if(notification_title.contains("wallpaper"))
{
changeWallpaper(notification_message);
}
else
{
generateNotification(remoteMessage.getData().get("title"),
remoteMessage.getData().get("message"));
}
}
public void changeWallpaper(String url)
{
final WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(activity);
Picasso.with(activity).load(url).into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Log.i("ok", "The image was obtained correctly");
try {
myWallpaperManager.setBitmap(bitmap);
Toast.makeText(activity, "Image has been set as Desktop Wallpaper", Toast.LENGTH_SHORT).show();
// ((Activity) activity).findViewById(R.id.progressbar_wallpaper).setVisibility(View.GONE);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
Log.e("ok", "The image was not obtained");
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
Log.e("ok", "Getting ready to get the image");
//Here you should place a loading gif in the ImageView
//while image is being obtained.
}
});
}
这已成功更改主线程上的壁纸,但我想在服务 class 中更改壁纸,即使应用程序在后台也是如此。任何形式的帮助将不胜感激
我通过使用 glide 库代替 picasso 解决了这个问题,即使在应用程序处于后台时它也能顺利运行
final WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(this);
Glide.with(getApplicationContext())
.asBitmap()
.load(url)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
try {
myWallpaperManager.setBitmap(resource);
} catch (IOException e) {
e.printStackTrace();
}
}
});
我对 android 应用程序中的一点印象深刻。我正在开发一个应用程序,我在其中使用服务 class,并且我通过我的服务 class 接收通知。现在有一个场景,我想更改我的 phone 应用程序的墙纸,我在我的代码中成功地做到了这一点,但是当我投入服务 class 时,应用程序崩溃并给出错误 picasso should 运行 在主线程中。实际上在我的应用程序中有一个场景,如果我收到通知,即使我的应用程序在后台,那么壁纸应该被更改。这是我的代码:
public class FirebaseMessagingServices extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
RemoteMessage.Notification notification = remoteMessage.getNotification();
if (data.isEmpty()) {
System.out.println("FCM type is Notification");
parseNotificationMessage(notification);
}
else {
System.out.println("FCM type is Data");
parseDataMessage(remoteMessage);
}
}
这里我正在更改接收消息的方式,在接收消息时我会检查消息并生成通知
private void parseDataMessage(RemoteMessage remoteMessage) {
String notification_title =remoteMessage.getData().get("title");
String notification_message = remoteMessage.getData().get("message");
if(notification_title.contains("wallpaper"))
{
changeWallpaper(notification_message);
}
else
{
generateNotification(remoteMessage.getData().get("title"),
remoteMessage.getData().get("message"));
}
}
public void changeWallpaper(String url)
{
final WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(activity);
Picasso.with(activity).load(url).into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Log.i("ok", "The image was obtained correctly");
try {
myWallpaperManager.setBitmap(bitmap);
Toast.makeText(activity, "Image has been set as Desktop Wallpaper", Toast.LENGTH_SHORT).show();
// ((Activity) activity).findViewById(R.id.progressbar_wallpaper).setVisibility(View.GONE);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
Log.e("ok", "The image was not obtained");
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
Log.e("ok", "Getting ready to get the image");
//Here you should place a loading gif in the ImageView
//while image is being obtained.
}
});
}
这已成功更改主线程上的壁纸,但我想在服务 class 中更改壁纸,即使应用程序在后台也是如此。任何形式的帮助将不胜感激
我通过使用 glide 库代替 picasso 解决了这个问题,即使在应用程序处于后台时它也能顺利运行
final WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(this);
Glide.with(getApplicationContext())
.asBitmap()
.load(url)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
try {
myWallpaperManager.setBitmap(resource);
} catch (IOException e) {
e.printStackTrace();
}
}
});