Android Tutorial – Introduction to Services and Creating Started Service




Android Tutorial
Android Tutorial

Android administration is a segment that is utilized to perform operations on the foundation, for example, playing music, handle system exchanges, cooperating content suppliers and so on. It doesn’t has any (UI).

The administration keeps running out of sight inconclusively regardless of the fact that application is devastated.
In addition, administration can be limited by a segment to perform intuitiveness and bury process correspondence (IPC).
The android.app.Service is subclass of ContextWrapper class.
service_lifecycle_rdc

Android Service Example
activity_main.xml

<RelativeLayout xmlns:androclass="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:paddingBottom="@dimen/activity_vertical_margin"  
    android:paddingLeft="@dimen/activity_horizontal_margin"  
    android:paddingRight="@dimen/activity_horizontal_margin"  
    android:paddingTop="@dimen/activity_vertical_margin"  
    tools:context=".MainActivity" >  
  
    <Button  
        android:id="@+id/buttonStart"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentTop="true"  
        android:layout_centerHorizontal="true"  
        android:layout_marginTop="19dp"  
        android:text="Start Service" />  
  
    <Button  
        android:id="@+id/buttonStop"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_above="@+id/buttonNext"  
        android:layout_alignRight="@+id/buttonStart"  
        android:layout_marginBottom="35dp"  
        android:text="Stop Service" />  
  
    <Button  
        android:id="@+id/buttonNext"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignLeft="@+id/buttonStop"  
        android:layout_centerVertical="true"  
        android:text="Next Page" />  
  
</RelativeLayout>

File: activity_next.xml
It contains only one textview displaying the message Next Page

<RelativeLayout xmlns:androclass="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:paddingBottom="@dimen/activity_vertical_margin"  
    android:paddingLeft="@dimen/activity_horizontal_margin"  
    android:paddingRight="@dimen/activity_horizontal_margin"  
    android:paddingTop="@dimen/activity_vertical_margin"  
    tools:context=".MainActivity" >  
  
    <TextView  
        android:id="@+id/textView1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentLeft="true"  
        android:layout_alignParentTop="true"  
        android:layout_marginLeft="96dp"  
        android:layout_marginTop="112dp"  
        android:text="Next Page" />  
  
</RelativeLayout>

File: MyService.java

package com.example.serviceexampleaudio;  
  
import android.app.Service;  
import android.content.Intent;  
import android.media.MediaPlayer;  
import android.os.IBinder;  
import android.widget.Toast;  
public class MyService extends Service {  
 MediaPlayer myPlayer;  
 @Override  
 public IBinder onBind(Intent intent) {  
  return null;  
 }  
 @Override  
 public void onCreate() {  
  Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();  
   
  myPlayer = MediaPlayer.create(this, R.raw.sun);  
  myPlayer.setLooping(false); // Set looping  
 }  
 @Override  
 public void onStart(Intent intent, int startid) {  
  Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();  
  myPlayer.start();  
 }  
 @Override  
 public void onDestroy() {  
  Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();  
  myPlayer.stop();  
 }  
}

File: MainActivity.java

package com.example.serviceexampleaudio;  
import android.app.Activity;  
import android.content.Intent;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
public class MainActivity extends Activity implements OnClickListener {  
     Button buttonStart, buttonStop,buttonNext;  
     @Override  
     public void onCreate(Bundle savedInstanceState) {  
      super.onCreate(savedInstanceState);  
      setContentView(R.layout.activity_main);  
  
      buttonStart = (Button) findViewById(R.id.buttonStart);  
      buttonStop = (Button) findViewById(R.id.buttonStop);  
      buttonNext = (Button) findViewById(R.id.buttonNext);  
  
      buttonStart.setOnClickListener(this);  
      buttonStop.setOnClickListener(this);  
      buttonNext.setOnClickListener(this);  
     }  
     public void onClick(View src) {  
      switch (src.getId()) {  
      case R.id.buttonStart:  
       startService(new Intent(this, MyService.class));  
       break;  
      case R.id.buttonStop:  
       stopService(new Intent(this, MyService.class));  
       break;  
      case R.id.buttonNext:  
       Intent intent=new Intent(this,NextPage.class);  
       startActivity(intent);  
       break;  
      }  
     }  
}

Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com





Be the first to comment

Leave a Reply

Your email address will not be published.


*