Java 在服务中下载文件(Android)

Java 在服务中下载文件(Android),java,android,service,android-developer-api,Java,Android,Service,Android Developer Api,我希望我的服务在申请完成后仍然有效。它继续支持工作,不会重新启动。我知道这可以在很多应用程序中实现,同时下载文件,服务继续平稳运行,我希望实现同样的效果 示例代码,以明确什么是关键 public class CacheFile extends Service { Context context; NotificationManager mNotificationManager; int id = 100; @Override public void onCreate() { cont

我希望我的服务在申请完成后仍然有效。它继续支持工作,不会重新启动。我知道这可以在很多应用程序中实现,同时下载文件,服务继续平稳运行,我希望实现同样的效果

示例代码,以明确什么是关键

public class CacheFile extends Service {

Context context;
NotificationManager mNotificationManager;
int id = 100;

@Override
public void onCreate() {
    context = this;
    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
}

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    toDownLoad();
    return super.onStartCommand(intent, flags, startId);
}


public void toDownLoad() {
    final NotificationCompat.Builder mBuilder;
    Intent deleteIntent = new Intent(this, ClickNotification.class);
    PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(this, 0, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(android.R.drawable.ic_menu_upload)
            .setContentTitle("Uploading Media...")
            .setTicker("Starting uploads")
            .addAction(android.R.drawable.ic_menu_close_clear_cancel, "Пауза", pendingIntentCancel)
            .addAction(android.R.drawable.ic_menu_close_clear_cancel, "Отмена", pendingIntentCancel);
    Intent notificationIntent = new Intent(this, MyActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(pendingIntent);
    mBuilder.setProgress(id, 0, true);
    new Thread(
            new Runnable() {
                @Override
                public void run() {
                    int incr;
                    for (incr = 0; incr <= 100; incr+=5) {
                        mBuilder.setProgress(100, incr, false);
                        mNotificationManager.notify(id, mBuilder.build());
                        try {
                            Thread.sleep(5*1000);
                        } catch (InterruptedException e) { }
                    }
                    mBuilder.setContentText("Download complete")
                            .addAction(0, null, null)
                            .addAction(0, null, null)
                            .setProgress(0, 0, false);
                    mNotificationManager.notify(id, mBuilder.build());
                }
            }
    ).start();
    mNotificationManager.notify(id, mBuilder.build());
}

@Override
public void onDestroy() {
    mNotificationManager.cancelAll();
    super.onDestroy();
}
公共类缓存文件扩展服务{
语境;
通知经理通知经理;
int id=100;
@凌驾
public void onCreate(){
上下文=这个;
mNotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION\u服务);
}
@凌驾
公共IBinder onBind(意向){
返回null;
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
toDownLoad();
返回super.onStartCommand(intent、flags、startId);
}
public void toDownLoad(){
最终通知建筑商承包商;
Intent deleteIntent=新的Intent(点击通知.class);
PendingEvent PendingEventCancel=PendingEvent.getBroadcast(this,0,deleteIntent,PendingEvent.FLAG_UPDATE_CURRENT);
mBuilder=new NotificationCompat.Builder(此)
.setSmallIcon(android.R.drawable.ic_菜单_上传)
.setContentTitle(“上载媒体…”)
.setTicker(“开始上传”)
.addAction(android.R.drawable.ic菜单关闭清除取消,“Пзззззa”,挂起取消)
.addAction(android.R.drawable.ic菜单、关闭、清除、取消,“аааа”,挂起、取消);
Intent notificationIntent=新意图(this,MyActivity.class);
notificationIntent.setFlags(Intent.FLAG\u ACTIVITY\u SINGLE\u TOP);
PendingEvent PendingEvent=PendingEvent.getActivity(this,0,notificationIntent,PendingEvent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(挂起内容);
mBuilder.setProgress(id,0,true);
新线程(
新的Runnable(){
@凌驾
公开募捐{
国际增量;

对于(incr=0;incr,即使您的应用程序关闭,您的服务也会继续运行。服务在其自己的进程中运行,您不需要新线程。您应该将方法toDownLoad()的代码放在doInBackground()中服务方法。请查看文档:

否,它已重新启动。如果此服务用作音频播放器,则在您关闭应用程序后,服务将重新启动,可以看到歌曲再次开始播放,在许多应用程序中,音乐继续播放官方文档中的一段引用:“系统可以运行服务有两个原因。如果有人调用Context.startService(),系统将检索服务(创建服务并在需要时调用其onCreate()方法),然后调用其onStart命令(Intent、int、int)方法的参数。该服务此时将继续运行,直到调用Context.stopService()或stopSelf()