Android 为什么我的服务在其活动被破坏后不起作用?

Android 为什么我的服务在其活动被破坏后不起作用?,android,android-service,Android,Android Service,我有一个主要活动和一个下载服务。在MainActivity被破坏之后,DownloadService似乎被冻结了。我没有看到任何进展。当我进入设置>管理应用程序>运行服务时,我在运行列表中再也看不到我的服务,但是,我也看不到logcat中的[onDestroy]。有什么问题吗 public class DownloadService extends Service { public static final String ACTION = "action";

我有一个主要活动和一个下载服务。在MainActivity被破坏之后,DownloadService似乎被冻结了。我没有看到任何进展。当我进入设置>管理应用程序>运行服务时,我在运行列表中再也看不到我的服务,但是,我也看不到logcat中的[onDestroy]。有什么问题吗

public class DownloadService extends Service {

    public static final String  ACTION          = "action";
    public static final int     ACTION_DOWNLOAD = 0;
    private NotificationManager notificationManager;
    private Notification        notification;
    private final int           NOTIFICATION_ID = 1;

    @Override
    public void onCreate() {
        super.onCreate();
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notification = new Notification();
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int action = intent.getIntExtra(ACTION, ACTION_DOWNLOAD);
        switch (action) {
            case ACTION_DOWNLOAD:
                startDownload(intent);
                break;
        }
        return START_NOT_STICKY;
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        Log.i("leapkh", "[onLowMemory]");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i("leapkh", "[onDestroy]");
    }

    private void startDownload(Intent intent) {
        String fileUrl = intent.getIntExtra(FILE_URL, "");
        FileDownloadAsyncTask task = new FileDownloadAsyncTask(fileUrl);
        task.execute();
    }

    private class FileDownloadAsyncTask extends AsyncTask<Void, Integer, Boolean> {

        private String     fileUrl;

        public FileDownloadAsyncTask(String fileUrl) {
            this.fileUrl = fileUrl;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            startNotification(station);
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            try {
                URL url = new URL(fileUrl);
                URLConnection conection = url.openConnection();
                conection.connect();
                int fileLength = conection.getContentLength();

                InputStream input = new BufferedInputStream(url.openStream(), 8192);

                String fileName = FileManager.getDownloadFileName(fileUrl);
                OutputStream output = new FileOutputStream(fileName);

                byte data[] = new byte[1024];

                long total = 0;
                int count, tmpPercentage = 0;
                while ((count = input.read(data)) != -1) {
                    total += count;
                    output.write(data, 0, count);
                    int percentage = (int) ((total * 100) / fileLength);
                    if (percentage > tmpPercentage) {
                        publishProgress(percentage);
                        tmpPercentage = percentage;
                    }
                }

                output.flush();
                output.close();
                input.close();
                return true;
            }
            catch (Exception e) {
                Log.i("leapkh", "[Download Error]: " + e.getMessage());
                return false;
            }
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            int percentage = values[0];
            updateNotification(station, percentage);
        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            Log.i("leapkh", "Download finished: " + result);
            completeNotification(station, result);
            stopSelf();
        }

        private void startNotification(Station station) {
            int icon = android.R.drawable.stat_sys_download;
            CharSequence tickerText = "Start downloading...";
            long when = System.currentTimeMillis();
            notification = new Notification(icon, tickerText, when);
            String contentTitle = "Downloading from " + fileUrl;
            CharSequence contentText = "0%";
            Intent notificationIntent = new Intent();
            PendingIntent contentIntent = PendingIntent.getActivity(DownloadService.this, 0, notificationIntent, 0);
            notification.setLatestEventInfo(DownloadService.this, contentTitle, contentText, contentIntent);
            notification.flags = Notification.FLAG_ONGOING_EVENT;
            notificationManager.notify(NOTIFICATION_ID, notification);
        }

        private void updateNotification(Station station, int percentage) {
            String contentTitle = "Downloading from " + fileUrl;
            CharSequence contentText = percentage + "%";
            Intent notificationIntent = new Intent();
            PendingIntent contentIntent = PendingIntent.getActivity(DownloadService.this, 0, notificationIntent, 0);
            notification.setLatestEventInfo(DownloadService.this, contentTitle, contentText, contentIntent);
            notificationManager.notify(NOTIFICATION_ID, notification);
        }

        private void completeNotification(Station station, boolean success) {
            String contentTitle = "Downloaded from " + fileUrl;
            Intent notificationIntent = new Intent();
            PendingIntent contentIntent = PendingIntent.getActivity(DownloadService.this, 0, notificationIntent, 0);
            String statusText = success ? "Completed" : "Fail";
            notification.icon = success ? android.R.drawable.stat_sys_download_done : android.R.drawable.stat_notify_error;
            notification.flags = Notification.FLAG_AUTO_CANCEL;
            notification.setLatestEventInfo(DownloadService.this, contentTitle, statusText, contentIntent);
            notificationManager.notify(NOTIFICATION_ID, notification);
        }

    }
}
公共类下载服务扩展服务{
公共静态最终字符串ACTION=“ACTION”;
公共静态最终int操作\u下载=0;
私人通知经理通知经理;
私人通知;
私人最终int通知_ID=1;
@凌驾
public void onCreate(){
super.onCreate();
notificationManager=(notificationManager)getSystemService(通知服务);
通知=新通知();
}
@凌驾
公共IBinder onBind(意图arg0){
返回null;
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
int action=intent.getIntExtra(action,action\u下载);
开关(动作){
个案行动下载:
开始下载(意图);
打破
}
返回开始时间不粘;
}
@凌驾
public void onLowMemory(){
super.onLowMemory();
Log.i(“leapkh”,“onLowMemory]”;
}
@凌驾
公共空间{
super.ondestory();
Log.i(“leapkh”,“onDestroy]”;
}
专用void startDownload(意图){
字符串fileUrl=intent.getIntExtra(文件URL,“”);
FileDownloadAsyncTask任务=新建FileDownloadAsyncTask(fileUrl);
task.execute();
}
私有类FileDownloadAsyncTask扩展了AsyncTask{
私有字符串文件URL;
公共文件下载异步任务(字符串文件URL){
this.fileUrl=fileUrl;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
启动通知(车站);
}
@凌驾
受保护的布尔doInBackground(Void…params){
试一试{
URL URL=新URL(文件URL);
URLConnection conconnection=url.openConnection();
conconnect.connect();
int fileLength=conconnect.getContentLength();
InputStream输入=新的BufferedInputStream(url.openStream(),8192);
字符串fileName=FileManager.getDownloadFileName(fileUrl);
OutputStream输出=新文件OutputStream(文件名);
字节数据[]=新字节[1024];
长总计=0;
整数计数,tmpPercentage=0;
而((计数=输入。读取(数据))!=-1){
总数+=计数;
输出.写入(数据,0,计数);
整数百分比=(整数)((总计*100)/文件长度);
如果(百分比>t百分比){
出版进度(百分比);
tmpPercentage=百分比;
}
}
output.flush();
output.close();
input.close();
返回true;
}
捕获(例外e){
Log.i(“leapkh”,“[下载错误]:”+e.getMessage());
返回false;
}
}
@凌驾
受保护的void onProgressUpdate(整型…值){
super.onProgressUpdate(值);
整数百分比=数值[0];
更新信息(站点、百分比);
}
@凌驾
受保护的void onPostExecute(布尔结果){
super.onPostExecute(结果);
Log.i(“leapkh”,“下载完成:+结果”);
完成化(站、结果);
stopSelf();
}
专用无效启动通知(车站){
int icon=android.R.drawable.stat\u sys\u下载;
CharSequence tickerText=“开始下载…”;
长时间=System.currentTimeMillis();
通知=新通知(图标,tickerText,何时);
String contentTitle=“下载自”+文件URL;
CharSequence contentText=“0%”;
意向通知意向=新意向();
PendingEvent contentIntent=PendingEvent.getActivity(DownloadService.this,0,notificationIntent,0);
notification.setLatestEventInfo(DownloadService.this、contentTitle、contentText、contentIntent);
notification.flags=notification.FLAG\u持续事件;
notificationManager.notify(通知ID,通知);
}
私有无效更新通知(站点,整数百分比){
String contentTitle=“下载自”+文件URL;
CharSequence contentText=百分比+“%”;
意向通知意向=新意向();
PendingEvent contentIntent=PendingEvent.getActivity(DownloadService.this,0,notificationIntent,0);
notification.setLatestEventInfo(DownloadService.this、contentTitle、contentText、contentIntent);
notificationManager.notify(通知ID,通知);
}
私有void completeNotification(Station,布尔成功){
String contentTitle=“下载自”+文件URL;
意向通知意向=新意向();
PendingEvent contentIntent=PendingEvent.getActivity(DownloadService.this,0,notificationIntent,0);
字符串statusText=成功?“已完成”:“失败”;
notification.icon=success?android.R.drawable.stat\u sys\u download\u done:android.R.drawable.stat\u notify\u error;
notification.flags=notification.FLAG\u AUTO\u CANCEL;
通知。setLatestEventInfo(下载)