IntentService,如何正确管理附加功能?

时间:2012-12-19 08:05:36

标签: android android-service

您好我尝试使用字符串作为额外启动IntentService,但它会抛出一个空指针异常,我搜索但我没找到任何东西,所以我问它。似乎服务无法提取意图的额外内容,我不知道是因为我忘记了什么,是我第一次使用IntentService。

好吧,主要是我抛出意图:

    String url = MY_HOST+"/videos/video.mp4";
    Intent intent = new Intent(this, VideosDownloader.class);
    intent.putExtra("url", url);
    startService(intent);

在服务中我管理额外的:

public class VideosDownloader extends IntentService{


public VideosDownloader() {
    super("VideosDownloader");
    // TODO Auto-generated constructor stub
}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

 @Override
 protected void onHandleIntent(Intent intent) {

        String url = intent.getStringExtra("url");
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
        request.setDescription("Some descrition");
        request.setTitle("Some title");

        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "video.mp4");
        request.setVisibleInDownloadsUi(true);
        // get download service and enqueue file
        DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        manager.enqueue(request);

 }
}

并且在Manifest中我认为一切都好,我宣布为服务:

<uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:debuggable="true" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


    <service android:name=".VideosDownloader"/>
</application>

错误抛出是:

12-19 08:45:50.482: E/AndroidRuntime(15470): FATAL EXCEPTION: main
12-19 08:45:50.482: E/AndroidRuntime(15470): java.lang.RuntimeException: Unable to start service com.example.downloadtest.VideosDownloader@40525e50 with Intent { cmp=com.example.downloadtest/.VideosDownloader (has extras) }: java.lang.NullPointerException
12-19 08:45:50.482: E/AndroidRuntime(15470):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2073)
12-19 08:45:50.482: E/AndroidRuntime(15470):    at android.app.ActivityThread.access$2800(ActivityThread.java:121)
12-19 08:45:50.482: E/AndroidRuntime(15470):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1006)
12-19 08:45:50.482: E/AndroidRuntime(15470):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 08:45:50.482: E/AndroidRuntime(15470):    at android.os.Looper.loop(Looper.java:138)
12-19 08:45:50.482: E/AndroidRuntime(15470):    at android.app.ActivityThread.main(ActivityThread.java:3701)
12-19 08:45:50.482: E/AndroidRuntime(15470):    at java.lang.reflect.Method.invokeNative(Native Method)
12-19 08:45:50.482: E/AndroidRuntime(15470):    at java.lang.reflect.Method.invoke(Method.java:507)
12-19 08:45:50.482: E/AndroidRuntime(15470):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
12-19 08:45:50.482: E/AndroidRuntime(15470):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
12-19 08:45:50.482: E/AndroidRuntime(15470):    at dalvik.system.NativeStart.main(Native Method)
12-19 08:45:50.482: E/AndroidRuntime(15470): Caused by: java.lang.NullPointerException
12-19 08:45:50.482: E/AndroidRuntime(15470):    at android.app.IntentService.onStart(IntentService.java:110)
12-19 08:45:50.482: E/AndroidRuntime(15470):    at android.app.IntentService.onStartCommand(IntentService.java:118)
12-19 08:45:50.482: E/AndroidRuntime(15470):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2060)
12-19 08:45:50.482: E/AndroidRuntime(15470):    ... 10 more

感谢您的回复。

解决:抱歉我的错,David Wasser给了我一个解决方案,询问我是否发布了所有代码,我重写Oncreate()什么都不做,我忘了它应该超级所以我删除它(因为它什么也没做)。谢谢回复!

2 个答案:

答案 0 :(得分:0)

尝试删除方法onBind

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

因为IntentService有一个实现。

答案 1 :(得分:0)

解决:抱歉我的错,David Wasser给了我一个解决方案,询问我是否发布了所有代码,我重写Oncreate()什么都不做,我忘了它应该超级所以我删除它(因为它什么也没做)。谢谢回复!