Thursday, 16 February 2017

Service Example with Getting Running Services from the device

MyService.java:
 package com.ramsandroid.demo;

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

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

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(getApplicationContext(),
        "service created",Toast.LENGTH_SHORT).show();
    }

    @Override
    public int onStartCommand(Intent intent,  int flags, 
                                           int startId) {
        Toast.makeText(getApplicationContext(),
             "service started",Toast.LENGTH_SHORT).show();

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(getApplicationContext(),
              "service stopped",Toast.LENGTH_SHORT).show();

    }
}



activity_main.java:
 <?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"
    tools:context="com.ramsandroid.demo.MainActivity">


    <Switch
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="27dp"
        android:text="Switch"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    
</RelativeLayout>


MainActivity.java:
 package com.ramsandroid.demo;

import android.app.ActivityManager;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;

import java.util.List;

public class MainActivity extends AppCompatActivity {

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

        ActivityManager am = (ActivityManager)
                  this.getSystemService(ACTIVITY_SERVICE);

        List<ActivityManager.RunningServiceInfo> rs = 
                  am.getRunningServices(Integer.MAX_VALUE);

        Switch aSwitch=(Switch)findViewById(R.id.switch1);

        for(ActivityManager.RunningServiceInfo runningServiceInfo:rs){

            String sname = runningServiceInfo.service.getClassName();
            Log.d("MainActivity",sname);

            if(sname.equals("com.ramsandroid.demo.MyService")){
                aSwitch.setChecked(true);
            }
        }


        aSwitch.setOnCheckedChangeListener
               (new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged
                  (CompoundButton compoundButton, boolean b) {
                Intent intent=
                   new Intent(getApplicationContext(),MyService.class);

                if(b){
                    startService(intent);
                }else{
                    stopService(intent);
                }
            }
        });
    }

    
}