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);
                }
            }
        });
    }

    
}

5 comments:

  1. I’m amazed, I must say. Rarely do I encounter a blog that’s both educative and interesting, and let me tell you, you have hit the nail on the head. The problem is something too few folks are speaking intelligently about. I am very happy I came across this in my search for something concerning this.
    Instagram Followers Best Price

    ReplyDelete
  2. Hi there! I could have sworn I’ve visited this site before but after browsing through some of the posts I realized it’s new to me. Anyways, I’m certainly pleased I discovered it and I’ll be book-marking it and checking back often!
    Social Media Followers

    ReplyDelete
  3. Great Post. Very informative. Keep Sharing!!

    Apply Now for Android Training in Noida

    For more details about the course fee, duration, classes, certification, and placement call our expert at 70-70-90-50-90

    ReplyDelete