Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Android Service Lifecycle and usage     - Linux Network Programming - libnet Guide (Programming)

- Upgrade installation manual CentOS6.5 GCC4.8.2 (Linux)

- Linux Oracle environment variable is invalid Problem Solving (Database)

- Java Foundation - The relationship between abstract classes and interfaces (Programming)

- Docker build private warehouse (Server)

- Install the latest Eclipse IDE in Ubuntu (Linux)

- The several technical presentation Raid under Linux (Linux)

- Close common port to protect server security (Linux)

- RedHat / CentOS ext4 partition can not be formatted large supplementary ext4 formatting (Linux)

- Graphical interface for the CentOS 6.4 installed and connected by remote VNC (Linux)

- File upload via AngularJS and ASP.NET MVC5 (Programming)

- Mistakenly deleted redo log file group being given the lead to start the database ORA-03113 (Database)

- Mount NFS network file system (Linux)

- Linux find command usage summary (Linux)

- Linux character device - user mode and kernel mode data transfer data (Linux)

- 10 Nginx safety tips (Linux)

- CentOS 6.4 under PXE + Kickstart unattended operating system installation (Programming)

- java.net.NoRouteToHostException of Hadoop problem: no route to host (Server)

- How to use the command line to obtain Freely RSS source on Linux (Linux)

- shell script error dirname: invalid option - b (Database)

 
         
  Android Service Lifecycle and usage
     
  Add Date : 2018-11-21      
         
         
         
  Service First, we need to know specifically what, and when to use? And its life cycle.

Service concepts and uses:

Android in the service, it Activity different, it can not interact with the user, not their own start, run the program in the background, if we exit the application, Service process does not end, it is still running in the background, what are we Service will be used when it? For example, we play music, may want to listen to music while doing something else, when we quit playing music application, if not Service, we can not hear the song, so this time you have to use the Service, and For example, when an application is data we obtain through the network, and at different times (for some time) data is different this time we can use the Service regularly updated in the background, without having to open the application every time to get in.

Service life cycle:

Android Service Activity life cycle is not as complicated as it only inherits the onCreate (), onStart (), onDestroy () method three, when we first start Service, has called onCreate (), onStart () these two methods, when stopping the Service, the execution onDestroy () method, to note here is that if the Service has been launched, when we start the Service again without executing onCreate () method, but direct execution onStart () method, specific examples can be seen below.

Service and Communication Activity:

Service backend data eventually to be presented on the front end of Activity, because when you start Service, the system will re-open a new process, which involves the problem of communication between different processes (AIDL) I do not make too much of this section description, when we want to get the Service instance starts, we can use bindService and onBindService methods, which are implemented in Service IBinder () and onUnbind () method.

To make it easier for everyone to understand, I wrote a simple Demo, you can imitate me, step by step.

Step one: Create a new Android project, I've named ServiceDemo.

Step Two: main.xml modify the code, I've added four buttons, as follows:

 < ? Xml version = "1.0" encoding = "utf-8"?>
< LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
    android: orientation = "vertical"
    android: layout_width = "fill_parent"
    android: layout_height = "fill_parent"
    >
 < TextView
  android: id = "@ + id / text"
    android: layout_width = "fill_parent"
    android: layout_height = "wrap_content"
    android: text = "@ string / hello"
    />
 < Button
  android: id = "@ + id / startservice"
  android: layout_width = "fill_parent"
  android: layout_height = "wrap_content"
  android: text = "startService"
 />
 < Button
  android: id = "@ + id / stopservice"
  android: layout_width = "fill_parent"
  android: layout_height = "wrap_content"
  android: text = "stopService"
 />
 < Button
  android: id = "@ + id / bindservice"
  android: layout_width = "fill_parent"
  android: layout_height = "wrap_content"
  android: text = "bindService"
 />
 < Button
  android: id = "@ + id / unbindservice"
  android: layout_width = "fill_parent"
  android: layout_height = "wrap_content"
  android: text = "unbindService"
 />
< / LinearLayout>

Step three: Create a new Service, named MyService.java code is as follows:

package com.tutor.servicedemo;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.text.format.Time;
import android.util.Log;
public class MyService extends Service {
 // Define a label a Tag
 private static final String TAG = "MyService";
 // This defines a bar Binder class used in onBind () method in there, so you can get to the other side of Activity
 private MyBinder mBinder = new MyBinder ();
 @Override
 public IBinder onBind (Intent intent) {
  Log.e (TAG, "start IBinder ~~~");
  return mBinder;
 }
 @Override
 public void onCreate () {
  Log.e (TAG, "start onCreate ~~~");
  super.onCreate ();
 }
 
 @Override
 public void onStart (Intent intent, int startId) {
  Log.e (TAG, "start onStart ~~~");
  super.onStart (intent, startId);
 }
 
 @Override
 public void onDestroy () {
  Log.e (TAG, "start onDestroy ~~~");
  super.onDestroy ();
 }
 
 
 @Override
 public boolean onUnbind (Intent intent) {
  Log.e (TAG, "start onUnbind ~~~");
  return super.onUnbind (intent);
 }
 
 // Here I wrote a function to obtain the current time, but not formatted so the bar on the first
 public String getSystemTime () {
  
  Time t = new Time ();
  t.setToNow ();
  return t.toString ();
 }
 
 public class MyBinder extends Binder {
  MyService getService ()
  {
   return MyService.this;
  }
 }
}

Step four: Modify ServiceDemo.java, the code is as follows:

package com.tutor.servicedemo;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class ServiceDemo extends Activity implements OnClickListener {
  
 private MyService mMyService;
 private TextView mTextView;
 private Button startServiceButton;
 private Button stopServiceButton;
 private Button bindServiceButton;
 private Button unbindServiceButton;
 private Context mContext;
 
 // Here you will need to use in Context.bindService ServiceConnection and context.unBindService () in
 private ServiceConnection mServiceConnection = new ServiceConnection () {
  // When I bindService, let TextView display MyService in getSystemTime () method's return value
  public void onServiceConnected (ComponentName name, IBinder service) {
   // TODO Auto-generated method stub
   mMyService = ((MyService.MyBinder) service) .getService ();
   mTextView.setText ( "I am frome Service:" + mMyService.getSystemTime ());
  }
  
  public void onServiceDisconnected (ComponentName name) {
   // TODO Auto-generated method stub
   
  }
 };
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.main);
        setupViews ();
    }
    
    public void setupViews () {
    
     mContext = ServiceDemo.this;
     mTextView = (TextView) findViewById (R.id.text);
     
     
     
     startServiceButton = (Button) findViewById (R.id.startservice);
     stopServiceButton = (Button) findViewById (R.id.stopservice);
     bindServiceButton = (Button) findViewById (R.id.bindservice);
     unbindServiceButton = (Button) findViewById (R.id.unbindservice);
     
     startServiceButton.setOnClickListener (this);
     stopServiceButton.setOnClickListener (this);
     bindServiceButton.setOnClickListener (this);
     unbindServiceButton.setOnClickListener (this);
    }
  
 public void onClick (View v) {
  // TODO Auto-generated method stub
  if (v == startServiceButton) {
   Intent i = new Intent ();
   i.setClass (ServiceDemo.this, MyService.class);
   mContext.startService (i);
  } Else if (v == stopServiceButton) {
   Intent i = new Intent ();
   i.setClass (ServiceDemo.this, MyService.class);
   mContext.stopService (i);
  } Else if (v == bindServiceButton) {
   Intent i = new Intent ();
   i.setClass (ServiceDemo.this, MyService.class);
   mContext.bindService (i, mServiceConnection, BIND_AUTO_CREATE);
  } Else {
   mContext.unbindService (mServiceConnection);
  }
 }
 
}

Step Five: Modify AndroidManifest.xml Code (our new MyService registered into the following code on line 14 :)

< ? Xml version = "1.0" encoding = "utf-8"?>
< Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
      package = "com.tutor.servicedemo"
      android: versionCode = "1"
      android: versionName = "1.0">
    < Application android: icon = "@ drawable / icon" android: label = "@ string / app_name">
        < Activity android: name = ". ServiceDemo"
                  android: label = "@ string / app_name">
            < Intent-filter>
                < Action android: name = "android.intent.action.MAIN" />
                < Category android: name = "android.intent.category.LAUNCHER" />
            < / Intent-filter>
        < / Activity>
  < Service android: name = android "MyService.": Exported = "true"> < / service>
    < / Application>
    < Uses-sdk android: minSdkVersion = "7" />
< / Manifest>
     
         
         
         
  More:      
 
- Installation and configuration to compile MySQL 5.6.10 under CentOS 5.9 (Database)
- Linux, see picture not resolve the problem (Linux)
- How to network to share files between Windows, MAC and Linux (Linux)
- Programmers Do not neglect debugging techniques (Programming)
- Lazarus IDE Start Basics Tutorial (Linux)
- Source encountered problems and solutions when installing SaltStack (Server)
- Comparison of Nginx and Nginx + (Server)
- MySQL optimization resulting order by using filesort (Database)
- TCP network communication Java Programming (Programming)
- Java Foundation - The relationship between abstract classes and interfaces (Programming)
- Android LayoutInflater source parsing (Programming)
- Ubuntu installed racing game Speed Dreams 2.1 (Linux)
- Installation and configuration under Linux SVN server - Backup - Recovery - Scheduled Tasks (Server)
- iOS constants, variables, properties and characteristics (Programming)
- Android Unzip the ZIP / GZIP data (based on the InflaterInputStream implementation) (Programming)
- OS X CAOpenGLLayer how to enable OpenGL 3.2 core profile (Programming)
- CentOS set up FTP server (Server)
- How to monitor Nginx (Database)
- OpenSUSE GPG Comments (Linux)
- MySQL Online DDL tools of pt-online-schema-change (Database)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.