卸载后如何停止DownloadManager? Package_removed的广播接收器不起作用

时间:2013-08-09 08:05:26

标签: android broadcastreceiver

我有以下广播接收器:

 public class UninstallReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
    String action = intent.getAction();
    Log.i("UninstallReceiver", "---------------------> onUNINSTALL():" + action);
    DownloadManager dm = (DownloadManager) context.getSystemService(Activity.DOWNLOAD_SERVICE);
    VideoDataDAO videosDao = VideoDataDAO.getInstance(context);
    final List<VideoData> undownloaded_new_videos = videosDao.getUndownloadedVideosFromDB();
    if (undownloaded_new_videos.size() > 0) {
        long[] videos = new long[undownloaded_new_videos.size()];
        for (int i = 0; i < undownloaded_new_videos.size(); i++) {
            videos[i] = undownloaded_new_videos.get(i).getId();
            Log.i("UninstallReceiver", "---------------------> onUNINSTALL():" + videos[i]);
        }
        if (videos != null) {
            Log.i("UninstallReceiver", "---------------------> != null: " + Arrays.toString(videos));
            dm.remove(videos);
        }
    }
    final List<VideoData> redownloadVideos = videosDao.redownloadVideosFromDB();
    if (redownloadVideos.size() > 0) {
        long[] videos = new long[redownloadVideos.size()];
        for (int i = 0; i < redownloadVideos.size(); i++) {
            videos[i] = redownloadVideos.get(i).getId();
            Log.i("UninstallReceiver", "---------------------> onUNINSTALL():" + videos[i]);
        }
        if (videos != null) {
            Log.i("UninstallReceiver", "---------------------> != null2");
            dm.remove(videos);
        }
    }
}
}

它在我的清单中声明如下:

  <receiver android:name=".receivers.UninstallReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_REMOVED" />
            <action android:name="android.intent.action.PACKAGE_FULLY_REMOVED" />

            <data android:scheme="package" />
        </intent-filter>
    </receiver>

现在我的问题是当我卸载应用程序时,没有调用广播接收器,然后我的应用程序继续通过DownloadService下载视频。如何让它识别我的卸载接收器?奇怪的是,如果不是卸载,我将我的应用程序从Eclipse部署到已安装的应用程序(这将卸载当前的应用程序,上传新的apk并安装它,这将调用我的卸载接收器)。

1 个答案:

答案 0 :(得分:0)

我说这个:

 <uses-permission android:name="android.permission.BROADCAST_PACKAGE_REMOVED" />

但后来我不得不改变lint设置,以这种方式将其声明为警告而非ERROR,因此我可以构建应用程序。