The road to Android development and learning-experience at the beginning of Service

Source: Internet
Author: User

The road to Android development and learning-experience at the beginning of Service

The last component of android is the service. Finally, I learned the last component. It took three weeks to build the development environment a year ago, I have learned a lot about the code written in books. I would like to thank the first line of code for this book. I did not study for three weeks except for the first day and the first day. I learned a knowledge point every day and finally learned something about it. Then I continue to learn other things. After talking about this, let's start learning Service components.

A Service literally means a Service, that is, providing services for applications. For example, when we play a song, I want to download it, then we can give it to the Service for processing. Playing a song is running in the background, so it is also a service. Let's take an example. Create a project ServiceTest first. The compiling layout is as follows:

<! -- {Cke_protected} {C} % 3C! % 2D % 2D % 3 Fxml % 20 version % 3D % 221.0% 20 encoding % 3D % 22utf-8% 22% 3F % 2D % 2D % 3E --> <linearlayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" android: layout_margin = "10dp" android: padding = "10dp" tools: context = "com. example. jared. servicetest. mainActivity "> <button android: id =" @ + id/startService "android: text =" Start Service "android: layout_width =" match_parent "android: layout_height =" wrap_content "android: textallcaps = "false"> </button> <button android: id = "@ + id/stopService" android: text = "Stop Service" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: textallcaps = "false"> </button> </linearlayout>
Here, two buttons are implemented to start and stop the Service. Next, create a new MyService class that inherits the Service. The Code is as follows:
package com.example.jared.servicetest;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.support.annotation.Nullable;import android.util.Log;import android.widget.Toast;/** * Created by jared on 16/2/18. */public class MyService extends Service{    private static final String TAG = "MyService";    @Nullable    @Override    public IBinder onBind(Intent intent) {        return null;    }    @Override    public void onCreate() {        super.onCreate();        Log.d(TAG, "onCreate is called");        Toast.makeText(MyService.this, "onCreate is called",Toast.LENGTH_SHORT).show();    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        Log.d(TAG, "onStartCommand is called");        Toast.makeText(MyService.this, "onStartCommand is called",Toast.LENGTH_SHORT).show();        return super.onStartCommand(intent, flags, startId);    }    @Override    public void onDestroy() {        super.onDestroy();        Log.d(TAG, "onDestroy is called");        Toast.makeText(MyService.this, "onDestroy is called",Toast.LENGTH_SHORT).show();    }}
Here the onCreate method, onStartCommand method, and onDestroy method are overwritten. Here some debugging information is added to each method, and then MainActivity code is implemented:
package com.example.jared.servicetest;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity {    private Button mStartServiceBtn;    private Button mStopServiceBtn;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mStartServiceBtn = (Button)findViewById(R.id.startService);        mStopServiceBtn = (Button)findViewById(R.id.stopService);        mStartServiceBtn.setOnClickListener(new myOnClickListener());        mStopServiceBtn.setOnClickListener(new myOnClickListener());    }    private class myOnClickListener implements View.OnClickListener {        @Override        public void onClick(View view) {            switch (view.getId()) {                case R.id.startService:                    Intent mStartIntent = new Intent(getApplicationContext(), MyService.class);                    startService(mStartIntent);                    break;                case R.id.stopService:                    Intent mSopIntent = new Intent(getApplicationContext(), MyService.class);                    stopService(mSopIntent);                    break;                default:                    break;            }        }    }}

Start the service when you press start, and stop the service when you press stop. Run the command to check the effect. Start the Service first. The effect is as follows:


 

The onCreate method is called first, and then the onStartCommand method is called. Then let's take a look at the running service and open the running in apps under the setting of the mobile phone:

The figure shows that ServiceTest is already in use. If we click Start Service again at this time, the onCreate method will not be called, but the onStartCommand method will be called. Only the Service can be stopped, stop the Service first. The effect is as follows:

The onDestroy method is called. Then start the Service again.

There is also a method in the code above, which is the onBind method. It can be understood literally that this method binds Activity and Service, so that you can control Service through Activity. The code below is as follows:

<! -- {Cke_protected} {C} % 3C! % 2D % 2D % 3 Fxml % 20 version % 3D % 221.0% 20 encoding % 3D % 22utf-8% 22% 3F % 2D % 2D % 3E --> <linearlayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" android: layout_margin = "10dp" android: padding = "10dp" tools: context = "com. example. jared. servicetest. mainActivity "> <button android: id =" @ + id/startService "android: text =" Start Service "android: layout_width =" match_parent "android: layout_height =" wrap_content "android: textallcaps = "false"> </button> <button android: id = "@ + id/stopService" android: text = "Stop Service" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: textallcaps = "false"> </button> <button android: id = "@ + id/startDownloadService" android: text = "bind Service Download" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: textallcaps = "false"> </button> <button android: id = "@ + id/stopDownloadService" android: text = "Unbind Service Download" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: textallcaps = "false"> </button> <button android: id = "@ + id/startGetProgressService" android: text = "binding Service GetProgress" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: textallcaps = "false"> </button> <button android: id = "@ + id/stopGetProgressService" android: text = "Unbind Service GetProgress" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: textallcaps = "false"> </button> </linearlayout>

The download start service and Download Status Service are added here. Add a Binder to the Service. The Code is as follows:
package com.example.jared.servicetest;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.support.annotation.Nullable;import android.util.Log;import android.widget.Toast;/** * Created by jared on 16/2/18. */public class MyService extends Service{    private static final String TAG = "MyService";    private DownloadBinder mDownloadBinder = new DownloadBinder();    class DownloadBinder extends Binder {        private int progress = 0;        public void startDownload() {            Log.d(TAG, "startDownload is called");            Toast.makeText(MyService.this, "startDownload is called",Toast.LENGTH_SHORT).show();            progress = 0;            new Thread(new Runnable(){                @Override                public void run() {                    for (int i=0; i<=100; i++) {                        try {                            Thread.sleep(1000);                            progress++;                        } catch (InterruptedException e) {                            e.printStackTrace();                        }                    }                }            }).start();        }        public int getProgress() {            Log.d(TAG, "getProgress is called");            Toast.makeText(MyService.this, "getProgress is called",Toast.LENGTH_SHORT).show();            return progress;        }    }    @Nullable    @Override    public IBinder onBind(Intent intent) {        return mDownloadBinder;    }    @Override    public void onCreate() {        super.onCreate();        Log.d(TAG, "onCreate is called");        Toast.makeText(MyService.this, "onCreate is called",Toast.LENGTH_SHORT).show();    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        Log.d(TAG, "onStartCommand is called");        Toast.makeText(MyService.this, "onStartCommand is called",Toast.LENGTH_SHORT).show();        return super.onStartCommand(intent, flags, startId);    }    @Override    public void onDestroy() {        super.onDestroy();        Log.d(TAG, "onDestroy is called");        Toast.makeText(MyService.this, "onDestroy is called",Toast.LENGTH_SHORT).show();    }}

The download process is simulated here. After the download is started, the thread updates the values per second, and then the value can be obtained through getProgress. Then modify the MainActivity Code as follows:
package com.example.jared.servicetest;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.Toast;public class MainActivity extends AppCompatActivity {    private Button mStartServiceBtn;    private Button mStopServiceBtn;    private Button mBindService1Btn;    private Button mUnbindService1Btn;    private Button mBindService2Btn;    private Button mUnbindService2Btn;    private MyService.DownloadBinder downloadBinder;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mStartServiceBtn = (Button)findViewById(R.id.startService);        mStopServiceBtn = (Button)findViewById(R.id.stopService);        mBindService1Btn = (Button)findViewById(R.id.startDownloadService);        mUnbindService1Btn = (Button)findViewById(R.id.stopDownloadService);        mBindService2Btn = (Button)findViewById(R.id.startGetProgressService);        mUnbindService2Btn = (Button)findViewById(R.id.stopGetProgressService);        mStartServiceBtn.setOnClickListener(new myOnClickListener());        mStopServiceBtn.setOnClickListener(new myOnClickListener());        mBindService1Btn.setOnClickListener(new myOnClickListener());        mUnbindService1Btn.setOnClickListener(new myOnClickListener());        mBindService2Btn.setOnClickListener(new myOnClickListener());        mUnbindService2Btn.setOnClickListener(new myOnClickListener());    }    private class myOnClickListener implements View.OnClickListener {        @Override        public void onClick(View view) {            switch (view.getId()) {                case R.id.startService:                    Intent mStartIntent = new Intent(getApplicationContext(), MyService.class);                    startService(mStartIntent);                    break;                case R.id.stopService:                    Intent mSopIntent = new Intent(getApplicationContext(), MyService.class);                    stopService(mSopIntent);                    break;                case R.id.startDownloadService:                    Intent mBindIntent = new Intent(getApplicationContext(), MyService.class);                    bindService(mBindIntent, startDownloadInService, BIND_AUTO_CREATE);                    break;                case R.id.stopDownloadService:                    unbindService(startDownloadInService);                    break;                case R.id.startGetProgressService:                    Intent mGetProgress = new Intent(getApplicationContext(), MyService.class);                    bindService(mGetProgress, getProgressInService, BIND_AUTO_CREATE);                    break;                case R.id.stopGetProgressService:                    unbindService(getProgressInService);                    break;                default:                    break;            }        }    }    private ServiceConnection startDownloadInService = new ServiceConnection() {        @Override        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {            downloadBinder = (MyService.DownloadBinder)iBinder;            downloadBinder.startDownload();        }        @Override        public void onServiceDisconnected(ComponentName componentName) {        }    };    private ServiceConnection getProgressInService = new ServiceConnection() {        @Override        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {            downloadBinder = (MyService.DownloadBinder)iBinder;            String progressValue = String.valueOf(downloadBinder.getProgress());            Toast.makeText(getApplicationContext(), "ProgressNow is:"+progressValue, Toast.LENGTH_SHORT).show();        }        @Override        public void onServiceDisconnected(ComponentName componentName) {        }    };}
ServiceConnection is instantiated here. When the bindService method is used, the onServiceConnected method of ServiceConnection is used to call the method of the newly created download class in Service. Run the command to check the effect. First, start the download and then obtain progress. The running effect is as follows:

Note that the service is a singleton. A singleton instance only has one instance and can only be called once by new processes. When we call bind download, We will directly call onCreate of Service. When we unbind it, we will also call the onDestroy method of Service. However, if we call the start Service and binder, we have to call both the ubind and stop methods.

If we do not unbind the above information when obtaining the SS, all subsequent operations will be invalid. It is valid only when it is obtained once, unbound, and then obtained.

When the memory of the android system is insufficient, services with lower priority will be automatically recycled. Therefore, in order to keep the service running, the service will not be recycled due to insufficient memory, you need to use the front-end service. The front-end service is always running and displayed in the status bar. music players like this will always be saved in the status bar. The Simple Service Modification code is as follows:

Package com. example. jared. servicetest; import android. app. pendingIntent; import android. app. service; import android. content. intent; import android. OS. binder; import android. OS. IBinder; import android. support. annotation. nullable; import android. support. v4.app. notificationCompat; import android. util. log; import android. widget. toast;/*** Created by jared on 16/2/18. */public class MyService extends Service {private static final String TAG = "MyService"; private static final int icationication_id = 1; private DownloadBinder mDownloadBinder = new DownloadBinder (); class DownloadBinder extends Binder {private int progress = 0; public void startDownload () {Log. d (TAG, "startDownload is called"); Toast. makeText (MyService. this, "startDownload is called", Toast. LENGTH_SHORT ). show (); progress = 0; new Thread (new Runnable () {@ Override public void run () {for (int I = 0; I <= 100; I ++) {try {Thread. sleep (1000); progress ++;} catch (InterruptedException e) {e. printStackTrace ();}}}}). start ();} public int getProgress () {Log. d (TAG, "getProgress is called"); Toast. makeText (MyService. this, "getProgress is called", Toast. LENGTH_SHORT ). show (); return progress ;}@ Nullable @ Override public IBinder onBind (Intent intent) {return mDownloadBinder ;}@ Override public void onCreate () {super. onCreate (); Log. d (TAG, "onCreate is called"); Toast. makeText (MyService. this, "onCreate is called", Toast. LENGTH_SHORT ). show (); icationicationcompat. builder notification = new NotificationCompat. builder (this); notification. setSmallIcon (R. mipmap. ic_launcher); notification. setContentTitle ("front-end service"); notification. setContentText ("this is a front-end service"); Intent intent = new Intent (this, MainActivity. class); PendingIntent pendingIntent = PendingIntent. getActivity (this, 0, intent, 0); notification. setContentIntent (pendingIntent); startForeground (icationication_id, notification. build () ;}@ Override public int onStartCommand (Intent intent, int flags, int startId) {Log. d (TAG, "onStartCommand is called"); Toast. makeText (MyService. this, "onStartCommand is called", Toast. LENGTH_SHORT ). show (); return super. onStartCommand (intent, flags, startId) ;}@ Override public void onDestroy () {super. onDestroy (); Log. d (TAG, "onDestroy is called"); Toast. makeText (MyService. this, "onDestroy is called", Toast. LENGTH_SHORT ). show (); stopForeground (true );}}
The notification is used here, and a foreground service is started using the startForegroid method. The following figure shows the result:

It can be seen that when the front-end service is started, an icon is displayed in the status bar. When the activity exits, it still exists and disappears only when the service is stopped, then you can understand why QQ music is still playing songs after exiting, And you can click on the front-end service to enter the app. This is the truth.

Learn about the Service first. Finally, I learned about the Service. Continue to learn other knowledge points.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.