意图服务无法启动

时间:2015-04-12 19:45:10

标签: java android intentservice android-intentservice

当我尝试启动我的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" />

1 个答案:

答案 0 :(得分:0)

好的,现在我觉得我在你的清单中看到了它。 <service>标记位于<application>标记之外,请尝试将其移至内部。