-1
신청서를 작성한 후에도 서비스가 제대로 작동되기를 바랍니다. 그것은 작업을 계속 지원하고 다시 시작하지 않습니다. 나는 지분서비스에서 파일 다운로드 (Android)
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();
}
}에 무엇인지는 명확하게 동일한
샘플 코드를 달성하려면, 파일을 다운로드하는 서비스가 원활하게 모두 계속 실행하는 동안이 많은 애플리케이션에서 달성 할 수있어
아니요, 다시 시작됩니다. 이 서비스를 오디오 플레이어로 사용하는 경우 응용 프로그램을 닫으면 서비스가 다시 시작되고 노래가 다시 재생되기 시작한 것으로 보입니다. 많은 응용 프로그램에서 음악이 계속 재생됩니다 –
공식에서 인용 한 내용입니다 doc : "시스템에서 서비스를 실행할 수있는 이유는 두 가지입니다. 누군가 Context.startService()를 호출하면 시스템에서 서비스를 검색하고 (필요한 경우 해당 메소드를 호출하고 onCreate() 메소드를 호출 한 다음) 해당 onStartCommand (Intent, int, int) 메서드를 클라이언트가 제공 한 인수와 함께 사용하면 Context.stopService() 또는 stopSelf()가 호출 될 때까지 계속 서비스가 실행됩니다. " – issathink