Today I learned the service, made a progress bar effect, and share with you,
To post the layout file.
1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Xmlns:tools= "Http://schemas.android.com/tools"4Android:layout_width= "Match_parent"5android:layout_height= "Match_parent"6android:orientation= "Vertical"7tools:context= "Com.wuxianedu.serviceoractivity.MainActivity" >8 9<progressbar android:id= "@+id/jindu"TenAndroid:layout_width= "Match_parent" Oneandroid:layout_height= "Wrap_content" AStyle= "? Android:attr/progressbarstylehorizontal" -/> -<button android:id= "@+id/wo" theAndroid:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" -android:text= "Click I download"/> - +</LinearLayout>
Mainactivity.java
1 Packagecom.wuxianedu.serviceoractivity;2 3 ImportAndroid.content.ComponentName;4 Importandroid.content.Intent;5 Importandroid.content.ServiceConnection;6 ImportAndroid.os.IBinder;7 Importandroid.support.v7.app.AppCompatActivity;8 ImportAndroid.os.Bundle;9 ImportAndroid.view.View;Ten ImportAndroid.widget.ProgressBar; One ImportAndroid.widget.Toast; A - Public classMainactivityextendsAppcompatactivityImplementsView.onclicklistener { - //New one Start service class the Privateserviceconnection serviceconnection - ; - PrivateProgressBar Pop; - + @Override - protected voidonCreate (Bundle savedinstancestate) { + Super. OnCreate (savedinstancestate); A Setcontentview (r.layout.activity_main); atPop =(ProgressBar) Findviewbyid (R.id.jindu); -Findviewbyid (R.ID.WO). Setonclicklistener ( This); - - } - - @Override in Public voidOnClick (View v) { - Switch(V.getid ()) { to CaseR.id.wo: + //New has a serviceconnection callback event, - if(Serviceconnection = =NULL){ theServiceconnection =Newserviceconnection () { * @Override $ Public voidonserviceconnected (componentname name, IBinder service) {Panax Notoginseng //The internal class abortion in the service as a subclass, to use the method inside - FinalMyservice.myhui Myhui =(Myservice.myhui) service; the //parameter is new out of static interface +Myhui.startdownload (NewMyservice.onprogresschangelistener () { A @Override the Public voidOnprogresschange (Final intcurrentprogress) { + //assign a value to a progress bar - Pop.setmax (Myservice.max); $ //Multithreading $Runonuithread (NewRunnable () { - @Override - Public voidrun () { the //assign a value to a progress bar - pop.setprogress (currentprogress);Wuyi //determine if the current value is greater than Max, or if it is over or downloaded, the if(Currentprogress >=Myservice.max) { -Toast.maketext (mainactivity. This, "Download done! ", Toast.length_short). Show (); Wu } - } About }); $ - } - }); - } A @Override + Public voidonservicedisconnected (componentname name) { the //Toast.maketext (mainactivity.this, "Download done!) ", Toast.length_short). Show (); - } $ }; the } the //Start service Services theBindservice (NewIntent ( This, MyService.class), serviceconnection,bind_auto_create); the Break; - } in }; the}
Myservice.java
1 Packagecom.wuxianedu.serviceoractivity;2 3 ImportAndroid.app.Service;4 Importandroid.content.Intent;5 ImportAndroid.os.Binder;6 ImportAndroid.os.IBinder;7 Importandroid.support.annotation.Nullable;8 ImportAndroid.util.Log;9 Ten /** One * Created by Administrator on 2016/9/23. A */ - Public classMyServiceextendsService { - Private intcurrentprogress; the Public Static intMAX = 100; - - @Nullable - //Callback Events + @Override - Publicibinder onbind (Intent Intent) { + return NewMyhui (); A } at - classMyhuiextendsbinder{ - - /*Public MyService GetService () { - return myservice.this; - }*/ in Public voidStartdownload (FinalOnprogresschangelistener Onprogresschangelistener) { - NewThread (NewRunnable () { to @Override + Public voidrun () { - while(true){ theCurrentprogress + = 5; *LOG.D ("main", "==================" +currentprogress+ ""); $ //assign values to the methods inside the interface every timePanax Notoginseng Onprogresschangelistener.onprogresschange (currentprogress); - Try { theThread.Sleep (1000L); +}Catch(interruptedexception e) { A e.printstacktrace (); the } + //determines whether the current value is greater than max maximum, if greater than the bounce loop - if(currentprogress>=MAX) { $ Break; $ } - } - } the }). Start (); - }Wuyi } the //gets the current value - InterfaceOnprogresschangelistener { Wu voidOnprogresschange (intcurrentprogress); - } About}
The code is these, the comments are written in very clear, we can see you understand
The following code is attached: tomorrow morning attached, the internet is too slow
Android Learning notes---Using service to mimic download effects