gpt4 book ai didi

java - 意向服务未启动

转载 作者:行者123 更新时间:2023-12-01 11:40:11 26 4
gpt4 key购买 nike

当我尝试启动 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);
}
}

}

文件下载服务.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" />

最佳答案

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

关于java - 意向服务未启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29593860/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com