Service

By | September 16th 2019 10:23:10 PM | viewed 70 times

create a main.xml layout and Add a button to service action in the location: layout(android studio)

<?xml version="1.0" encoding="utf-8"? >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="426dp" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:text="Welcome to main activity "
                android:textColor="#ffffff"
                android:textSize="15sp" />

               <Button
                android:id="@+id/btnServiceTest"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/btnListView"
                android:layout_alignTop="@+id/btn_intent_explicit"
                android:text="Service Test"
                android:textSize="8sp" />
          

        </RelativeLayout>

    </LinearLayout>

</LinearLayout>

create a ProjectonOverallClassMainActivity to create button action for service test

package com.uk.modak;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class ProjectonOverallClassMainActivity extends Activity {
   
	
	public Button btnServiceTest2;
		
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
               
          btnServiceTest2 = (Button) findViewById(R.id.btnServiceTest);            
        btnServiceTest2.setOnClickListener(new View.OnClickListener() {
    		
			public void onClick(View v) {				
				Intent n = new Intent(ProjectonOverallClassMainActivity.this,ServiceTestActivity.class);
				startActivity(n);
			}
	    });               
    }
    
    public void onStart(){
    	super.onStart();
    	 	
    }
      
    
}

create a servicetestlayout.xml layout to process service (android studio)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >


        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:text="Welcome to service test activity" />



        <Button
            android:id="@+id/btnStartService"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/textView1"
            android:layout_marginLeft="32dp"
            android:layout_marginTop="170dp"
            android:text="Start" />



        <Button
            android:id="@+id/btnStopService"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/btnStartService"
            android:layout_alignBottom="@+id/btnStartService"
            android:layout_alignRight="@+id/textView1"
            android:layout_marginRight="26dp"
            android:text="Stop" />

    </RelativeLayout>

</LinearLayout>

create a ServiceTestActivity to process service (android studio)

package com.uk.modak;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class ServiceTestActivity extends Activity {
	
	private Button btnStartService;
	private Button btnStopService;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.servicetestlayout);
        
        btnStartService = (Button) findViewById(R.id.btnStartService);
        btnStopService =  (Button) findViewById(R.id.btnStopService);
        
        btnStartService.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				
				Intent o = new Intent(getBaseContext(),TreadService.class);
				startService(o);
			}
		});
        
        btnStopService.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				Intent p = new Intent(getBaseContext(),UKService.class);
				stopService(p);
				//startService(p);
			}
		});
              
    }
}

Write another to service procesor

package com.uk.modak;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class TreadService extends Service {

	private boolean isRunning = false;
    private static final String TAG = "Thread Service";
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate()
	{
		Log.i(TAG,"Service On Create");
		Toast.makeText(this, "Service onCreate Method", Toast.LENGTH_LONG).show();
		isRunning =true;
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId)
	{
		
		Log.i(TAG,"Service Started in onStartCommand");
		Toast.makeText(this, "Service Started in onStartCommand", Toast.LENGTH_LONG).show();
		new Thread(new Runnable(){
			public void run()
			{
				for(int i=0;i<5;i++)
				{
					try{
					Thread.sleep(100);
					}catch(Exception e)
					{
						
					}
				}
				if(isRunning)
				{
					//Toast.makeText(TreadService.this, "Service Running", Toast.LENGTH_SHORT).show();
				    System.out.println("Service is Running");
				    Log.i(TAG,"Service is Running");    
				
				}
				stopSelf();
			}
			
		}
		).start();
		
		return START_STICKY;
		
	}

}

package com.uk.modak;

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

public class UKService extends Service {

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}
	
	
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId)
	{
		Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
		return START_STICKY;
		
	}
	
	@Override
	public void onDestroy()
	{
		
		Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
		
		
	}

}

to to menifeast and setup activity that we use

<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ProjectonOverallClassMainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
		
		 <activity
            android:name=".ServiceTestActivity"
            android:label="@string/app_name" >
        </activity>
       
        <service android:name = "TreadService" />
        <service android:name = "UKService" />
        
        <receiver android:name=".ConnectionReceive">
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
        </receiver>
        
    </application>
bONEandALL