【问题标题】:getcontentresolver() returns null in a servicegetcontentresolver() 在服务中返回 null
【发布时间】:2012-02-06 19:57:09
【问题描述】:

在下面的代码中,getcontentresolver() 为空,但在服务的onCreate() 中有效,但在服务中的函数中无效。为什么?请帮助我..谢谢

    final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    final String[] cursor_cols = { MediaStore.Audio.Media._ID,
            MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA };

    final String where = MediaStore.Audio.Media.IS_MUSIC + "=1";

    cursor = getContentResolver()
            .query(uri, cursor_cols, where, null, null);

【问题讨论】:

  • 你是在调用你的ServiceonCreate() 方法之前 执行的函数吗?例如。从构造函数或类似的东西调用它?
  • 只在启动服务后没有,只是在第一个活动中启动服务
  • 您应该添加显示类以及在这种情况下如何调用函数的代码。
  • public ArrayList Scan() { final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; final String[] cursor_cols = { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA };跨度>
  • 最终字符串 where = MediaStore.Audio.Media.IS_MUSIC + "=1"; cursor = getContentResolver() .query(uri, cursor_cols, where, null, null);

标签: android android-contentresolver


【解决方案1】:

确保您与设备的 USB 连接处于 PC 模式,而不是 USB 大容量存储

如果是大容量存储,则返回null

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    • 1970-01-01
    相关资源
    最近更新 更多