This article describes the usage of Android Service controls. Share it for your reference, as follows:
1. Service is a component of an application
2. Service does not have a graphical interface
3. Used to process long-term functions (download, play MP3)
4. Update ContentProvider, Intent and system startup
Service is not a separate process, not a thread
Defining a Service is simpler. Just inherit the Service class and implement its life cycle method. A defined service must be declared in a file through <service> to use
<service android:name="MyService"> <intent-filter> <action android:name=""/> </intent-filter> </service>
Note: MyService must inherit the Service class and the name and class name are the same.
The name of the action is customized and can be captured as long as it bindService or operates Intent.
public class MyService extends Service { public class MyBinder extends Binder{ public MyService getMyService(){ return ; } } public void test(){ ("test"); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub ("onBind"); return new MyBinder(); } @Override public void onCreate() { // TODO Auto-generated method stub ("onCreate"); (); } @Override public boolean onUnbind(Intent intent) { ("I am unbind"); return (intent); } }
public class MainActivity extends Activity { private static final String MYSERVICE = ""; private boolean flag = false; //bindService() method requires the ServiceConnection interface as a parameter, so this variable is defined, with the purpose of implementing the two methods in it ServiceConnection conn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { ("onServiceDisconnected"); } @Override public void onServiceConnected(ComponentName name, IBinder service) { ("onServiceConnected"); MyBinder myBinder = (MyBinder)service; MyService myService = (); (); } }; @Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); findViewById().setOnClickListener(new (){ @Override public void onClick(View v) { Intent intent = new Intent(); (MYSERVICE); //First call MyService's onCreate() method, then call onBind() method, and finally call onServiceConnected() method // Therefore, the value returned by IBinder is the object returned by the onBind() method. The getMyService() method is defined in order to get the MyService object. The user adds some methods he needs to in this class, so that some logical processing can be done. bindService(intent, conn, Service.BIND_AUTO_CREATE); flag = true; } }); findViewById().setOnClickListener(new (){ @Override public void onClick(View v) { if(flag){ //Unbinding event will be called MyService's onUnbind() method, but the onServiceDisconnected() method will not be called for unknown reasons. unbindService(conn); flag = false; } } }); } }
For more information about Android related content, please check out the topic of this site:Android file operation skills summary》、《A summary of SD card operation methods for Android programming and development》、《Android development introduction and advanced tutorial》、《Android resource operation skills summary》、《Android View View Tips Summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.