意向服务未启动

Intent Service not Starting

当我尝试启动我的 IntenetService 时,我没有收到任何错误,但它从未遇到 onHandleIntent

中的断点

MainActivity.java

    package com.peterchappy.filewatcher.activities;


import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.peterchappy.filewatcher.R;
import com.peterchappy.filewatcher.services.FileDownloadService;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class MainActivity extends Activity {


    public static final String INTENT_EXTRA_FILE_URL = "URL";
    private FileDownloadBroadcastReceiver broadcastReceiver;
    private Button go;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        go = (Button) findViewById(R.id.go);
        go.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                downloadFile("http://www.reddit.com/");
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        broadcastReceiver = new FileDownloadBroadcastReceiver();
        registerReceiver(broadcastReceiver, new IntentFilter(FileDownloadService.FILE_DOWNLOADED));
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(broadcastReceiver);
    }

    private void downloadFile(String text) {
        Intent intent = new Intent(this, FileDownloadService.class);
        intent.putExtra(INTENT_EXTRA_FILE_URL, text);
        this.startService(intent);
    }

    private void readFileAndDisplayContent(String fileName) {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(openFileInput(fileName)));

            StringBuilder content = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                content.append(line).append("\n");
            }

            reader.close();
            System.out.println(content.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private class FileDownloadBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String localFileName = intent.getStringExtra(FileDownloadService.INTENT_LOCAL_FILE_NAME);
            readFileAndDisplayContent(localFileName);
        }
    }

}

FileDownloadService.java

package com.peterchappy.filewatcher.services;

import android.app.IntentService;
import android.content.Intent;

import com.peterchappy.filewatcher.activities.MainActivity;
import com.peterchappy.filewatcher.helpers.FileDownloader;

public class FileDownloadService extends IntentService {

public static final String INTENT_LOCAL_FILE_NAME = "local_file_name";
public static final String FILE_DOWNLOADED = "FILE_DOWNLOADED";

public FileDownloadService() {
    super("FILE_DOWNLOAD_SERVICE");
}

@Override
protected void onHandleIntent(Intent intent) {

    FileDownloader fileDownloader = new FileDownloader(this);
    String localFileName = fileDownloader.downloadFile(intent.getStringExtra(MainActivity.INTENT_EXTRA_FILE_URL));

    Intent resultIntent = new Intent(FILE_DOWNLOADED);
    resultIntent.putExtra(INTENT_LOCAL_FILE_NAME, localFileName);
    sendBroadcast(resultIntent);
}

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
<service android:name=".filewatcher.services.FileDownloadService"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

好的,现在我想我在你的清单中看到了。 <service> 标签在 <application> 标签外面,尝试将它移到里面。