Android App Development With AndroidCource: how to create services call in android
Showing posts with label how to create services call in android. Show all posts
Showing posts with label how to create services call in android. Show all posts

Monday, 21 August 2017

Services Example in Android

ServicesActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:id="@+id/activity_services"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:paddingBottom="@dimen/activity_vertical_margin"
              android:paddingLeft="@dimen/activity_horizontal_margin"
              android:paddingRight="@dimen/activity_horizontal_margin"
              android:paddingTop="@dimen/activity_vertical_margin"
              android:orientation="vertical"
              android:background="#965302"
              tools:context="com.bhaumik.programmingstudy.ServicesActivity">

    <Button
            android:id="@+id/btn_start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Start Services"
            android:layout_gravity="center_horizontal"
            android:textAllCaps="false"
            android:textSize="18dp"/>
    <Button
            android:id="@+id/btn_stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Stop Services"
            android:layout_marginTop="20dp"
            android:layout_gravity="center_horizontal"
            android:textAllCaps="false"
            android:textSize="18dp"/>
</LinearLayout>
ServicesActivity.java
package com.bhaumik.programmingstudy;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class ServicesActivity extends AppCompatActivity {

    Button start,stop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_services);

        start = (Button) findViewById(R.id.btn_start);
        stop = (Button) findViewById(R.id.btn_stop);

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ServicesActivity.this,MyServices.class);
                startService(intent);
            }
        });
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ServicesActivity.this,MyServices.class);
                stopService(intent);
            }
        });
    }
}
MyServices.java
package com.bhaumik.programmingstudy;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;


public class MyServices extends Service{
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        ToastMsg.ShowToast(MyServices.this,"Services Create ");
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        ToastMsg.ShowToast(MyServices.this,"Services Start ");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ToastMsg.ShowToast(MyServices.this,"Services Destroy ");
    }
}
Note : MyServices Register to AndroidManifest.xml File. Follow This Ex. Given Below
     
         <service android:name=".MyServices" />





Ads