Bound Services (Get Random Number)

Bound Services in Android

CLASS FILE

package com.example.malik.test;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

MyService myService;
boolean isBound = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, MyService.class);
bindService(intent,serviceConnection , Context.BIND_AUTO_CREATE);

}

public void getRandomNumber(View view){
TextView textView = (TextView) findViewById(R.id.textView123);
textView.setText(Integer.toString(myService.getRandom()));

}

private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MyService.LocalBinder binder = (MyService.LocalBinder) service;
myService = binder.getService();
isBound = true;
}

@Override
public void onServiceDisconnected(ComponentName name) {
isBound = false;
}
};
}

 

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=”Get Random Number”
android:id=”@+id/button”
android:layout_marginTop=”78dp”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:onClick=”getRandomNumber”
/>

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textAppearance=”?android:attr/textAppearanceLarge”
android:text=”Large Text”
android:id=”@+id/textView123″
android:layout_centerVertical=”true”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true” />
</RelativeLayout>

 

MyService CLASS

package com.example.malik.test;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

import java.util.Random;

public class MyService extends Service {

private final IBinder iBinder = new LocalBinder();
private final Random mGenerator = new Random();
public class LocalBinder extends Binder {
MyService getService(){
return MyService.this;
}
}

public MyService() {
}

@Override
public IBinder onBind(Intent intent) {
return iBinder;
}

public int getRandom(){
return mGenerator.nextInt(200);
}
}

 

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=”.MyService”
android:enabled=”true”
android:exported=”true”></service>
</application>

</manifest>

 

Output:

pic pic1

 

Share

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.