【问题标题】:Android service not starting with a warninigAndroid 服务没有以警告开始
【发布时间】:2014-01-08 06:30:46
【问题描述】:

我有一项应该在后台上传照片的服务。但是当我尝试启动它时,它并没有启动。在 logcat 中,我注意到我收到了警告 Implicit intents with startService are not safe : Intent { .....}。我已经三倍地检查了清单中的操作字符串是否与我开始的内容相同。我的代码:
清单:

<service android:name=".photosupload.services.PhtosUploadService" 
  android:exported="false" android:process=":uploadPhotosServiceProcess">
  <intent-filter>
    <action android:name="com.yoovi.app.photosupload.services.action.START_UPLOAD" />
  </intent-filter>
</service>

启动服务代码:

  Intent i = new Intent(PhtosUploadService.ACTION_START_UPLOAD);
  i.putExtra(PhtosUploadService.ALBUM_ID, albumID);
  context.startService(i);

【问题讨论】:

    标签: android android-service android-implicit-intent


    【解决方案1】:

    您需要了解隐式和显式意图。

    显式意图意味着您需要指定服务意图的确切类。

    您的代码应该类似于

        Intent i = new Intent();  
    i.setClass(this, photosupload.services.PhtosUploadService.class);
    

    【讨论】:

    • 为什么我不能隐式启动它?我在我的另一个应用程序中做了完全相同的事情,它运行良好
    • 可以,但如果您使用 startservice,android 会发出警告。 “使用 startService 的隐式意图不安全”
    • 但它没有启动,我在onCreateonStartCommand 中有断点,但它永远不会到达那里
    • 那是一个不同的问题,变量 ACTION_START_UPLOAD 的值是什么
    【解决方案2】:

    如果您想向服务发送隐式意图 - 请在清单中更改为 android:exported="true"。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-26
      • 2018-10-29
      • 2019-07-28
      • 1970-01-01
      相关资源
      最近更新 更多