Write a program to demonstrate use of service.

XML File:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    >

    <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="74dp"

        android:text="Start Service" />

    <Button

        android:id="@+id/buttonStop"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:layout_centerVertical="true"

        android:text="Stop Service" />

    <Button

        android:id="@+id/buttonNext"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentBottom="true"

        android:layout_centerHorizontal="true"

        android:layout_marginBottom="63dp"

        android:text="Next Page" />

</RelativeLayout> 



MainActivity.java

package com.example.myapplication;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    Button buttonStart, buttonStop,buttonNext;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        buttonStart = findViewById(R.id.buttonStart);

        buttonStop = findViewById(R.id.buttonStop);

        buttonNext =  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;

        } }

Activity_next.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:layout_width="match_parent"

    android:layout_height="match_parent"

   >

    <TextView

        android:id="@+id/textView"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginEnd="8dp"

        android:layout_marginStart="8dp"

        android:layout_marginTop="200dp"

        android:text="ZAINAB's Next Page"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent"

        android:textSize="50dp"

        android:fontFamily="serif-monospace"/>

</LinearLayout>


NextPage.java

package com.example.myapplication;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.R;

public class NextPage extends AppCompatActivity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_next);

    }

}  

MyService.java

package com.example.myapplication;

import android.app.Service;

import android.content.Intent;

import android.media.MediaPlayer;

import android.os.IBinder;

import android.widget.Toast;

import androidx.annotation.Nullable;

public class MyService extends Service {

    MediaPlayer myPlayer;

    @Nullable

    @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();

    }

}  






Manifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.myapplication">

    <application

        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"

        android:label="@string/app_name"

        android:roundIcon="@mipmap/ic_launcher_round"

        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>

        <activity android:name=".NextPage"></activity>

        <service

            android:name=".MyService"

            android:enabled="true" />

    </application>

</manifest>


          

         



Comments

Popular posts from this blog

Write a program to create a first display screen on any search engine using auto complete text View

Write a Program to create a toggle button to display the ON/OFF Bluetooth on the display screen.

Write a program to create a login form for social networking website.