Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make ServiceLifeCycle in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 52
    Comment on it

    In the below example code I have created android Service program. In activity_main.xml layout, first I have created a LinearLayout then in LinearLayout added two Button. Now See in coding part Here I have created a new MyService class and then MyService class extend with Service class here I have used Ibinder interface.

    SO when you click on Start button, service will start and you will get service start message on screen. After this when you will need to close the service, by click stop button service will get stoped and you will receive message on your screen service stop.

    In onStart() and onDestroy() method I have used toast function In MainActivity I have implements OnClickListener method and in last step I have added Service in AndroidMainfest.xml . You can see below example code it clearly describe you "How to make ServiceLifeCycle in android".

     

    Step(1)activity_main.xml -

    <?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"
        android:gravity="center">
        <LinearLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:id="@+id/linearLayout1"
            android:gravity="center">
            <Button
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:id="@+id/buttonStart"
                android:background="#f7c4c4"
                android:layout_marginRight="10dp"
                android:text="Start Service"></Button>
            <Button
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:background="#5182de"
                android:id="@+id/buttonStop"
                android:text="Stop Serivce"></Button>
        </LinearLayout>
    </LinearLayout>

    Step(2)-Created a new MyService class-

    public class MyService extends Service {
    
        private final static String TAG="In this method:";
        int mStartMode;
        IBinder mBinder;
        boolean mAllowRebind;
    
        @Override
        public void onCreate(){
            Log.i(TAG,"Service created");
        }
    @Override
    public int onStartCommand(Intent intent,int flags,int startId){
        Log.i(TAG, "Service started");
        Toast.makeText(getBaseContext(),"Service has been started..",Toast.LENGTH_SHORT).show();
        return mStartMode;
    
    }
    
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
    
    
            Log.i(TAG, "Service binded");
            return mBinder;
        }
        @Override
        public boolean onUnbind(Intent intent){
            Log.i(TAG,"Service un-binded");
            return mAllowRebind;
        }
        @Override
        public void onRebind(Intent intent){
            Log.i(TAG,"Service is re-binded");
        }
        @Override
        public void onDestroy() {
            // The service is no longer used and is being destroyed
            Toast.makeText(getBaseContext(),"Service stop.",Toast.LENGTH_SHORT).show();
            Log.i(TAG, "Service destroyed");
    
        }
    }


    Step(3)-MainActivity-

    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
        private final static  String TAG="In this method";
        private Button startService =null;
        private Button stopService =null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
           startService=(Button)findViewById(R.id.buttonStart);
            startService.setOnClickListener(this);
            stopService=(Button)findViewById(R.id.buttonStop);
            stopService.setOnClickListener(this);
    
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            int id = item.getItemId();
    
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    
        @Override
        public void onClick(View v) {
            if(startService==v){
                Log.i(TAG,"Activity starting service.." );
                Intent serviceIntent = new Intent(this,MyService.class);
                startService(serviceIntent);
            }else {
                Intent in = new Intent(this,MyService.class);
                in.setAction("stop");
                stopService(in);
            }
    
        }
    }


     Step(4)-Add Service in  AndroidMainfest.xml file-
         

      <service android:name=".MyService"
                android:enabled="true">
                <intent-filter>
                    <action android:name="com.tiwari.rajshekhar.service"></action>
                </intent-filter>
            </service>
    
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: