Services in Android

Introduction to Services and Creating Started Service

CLASS FILE

package com.example.malik.test;

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

public class MainActivity extends AppCompatActivity {

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

public void startService(View view){
Intent intent = new Intent(this, TheService.class);
startService(intent);
}

public void stopService(View view){
Intent intent = new Intent(this, TheService.class);
stopService(intent);
}

}

XML FILE

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout 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: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=”com.example.malik.test.MainActivity”>
<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Start Service”
android:id=”@+id/button”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”67dp”
android:onClick=”startService”
/>

<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Stop Service”
android:id=”@+id/button2″
android:layout_below=”@+id/button”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”53dp”
android:onClick=”stopService”
/>
</RelativeLayout>

 

TheService CLASS

package com.example.malik.test;

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

/**
* Created by malik on 07/06/2016.
*/
public class TheService extends Service{

@Override
public void onCreate() {
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(TheService.this, “Service Started”, Toast.LENGTH_SHORT).show();
return START_STICKY;
}

@Override
public void onDestroy() {
Toast.makeText(TheService.this, “Service Destroy”, Toast.LENGTH_SHORT).show();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}

 

Manifest FILE

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.malik.test”>

<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<activity android:name=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<service android:name=”.TheService” android:exported=”false”></service>

</application>

</manifest>

 

Output :

pic

 

 

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.