在WallpaperManager的广播接收器上额外的意图

问题描述:

我正在构建一个壁纸应用程序。 我有一个设置壁纸的按钮。 我想要做的是检查壁纸是否下载,如果是设置壁纸 - 如果没有,下载并设置壁纸。在WallpaperManager的广播接收器上额外的意图

我检查是否存在带有ID的文件(例如26748.jpg),如果是,我成功设置壁纸,如果它不存在,我下载它 - 但我无法设置它。

我有一个BroadcastReceiver设置:

<receiver android:name=".SinglePhotoActivity$CheckDownloadComplete"> 
     <intent-filter> 
      <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/> 
     </intent-filter> 
    </receiver> 

它显示一个简单的保存消息:

public static class CheckDownloadComplete extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Toast.makeText(context, "Saved!", Toast.LENGTH_SHORT).show(); 
    } 

} 

的问题是,我有两种类型设置壁纸:一,如果壁纸已经下载了,如果没有,壁纸也会下载。我做了一点研究,发现这种类型的广播接收机不能真正包含任何附加的意图。我唯一能做的就是在我的DownloadManager的请求中设置一个描述,然后检查onReceive中的描述。

所以,如果图像已经下载,我想显示一个简单的吐司。如果没有,然后下载它,然后在OnReceive下载完成后运行我的设置壁纸的代码。

还有没有更好的做法呢?

+1

你从哪里读到你不能在这个广播中有一个额外的启示者? –

检查壁纸是否已经下载。

创建一个至少包含两列的数据库表。

1.Downloaded wallpaper url 2.Downloaded image local path(from where you will set it to wallpaper)

每次尝试下载壁纸时,首先请从数据库表中检查中是否已存在url。对于这个我使用的是sugarorm,你可以使用任何数据库。你可以在sugarorm这样做。

DownloadedWallpapers mDownloadedWallpaper = null; 

    try { 
     mDownloadedWallpaper = DownloadedWallpapers.find(DownloadedWallpapers.class, "url=?", imageUrl).get(0); 
    } catch (Exception ex) {} 

    if (mDownloadedWallpaper == null) 
     // run your downloading code here and on download set wallpaper 
    else 
     // run set wallpaper code here 

在其他数据库中,过程是相同的。只需检查网址是否存在于数据库中。

下载后发送包含本地文件路径的广播。

Intent mIntent = new Intent(context,WallpaperReceiver.class); 
mIntent.putExtra("local_path", downloadedFilePath); 
sendBroadcast(mIntent); 

在广播接收机运行墙纸设置代码。

对我来说,这是一个干净,工作和简单的解决方案。欢呼声:)