You are here
Home > HelloWorld >

Hello World in android studio using activity,service & broadcastReceiver

Problem Statement:

Printing Hello World text using activity,service and broadcast receiver.

This program demonstrates how to call any activity from service and service from broadcast receiver. The application starts from broadcast receiver then control goes to service and service call activity to print “Hello World”.

Several ways To create an application:

  • LocalBroadcastManager:This can be used ,If you want to send broadcasts to your application.
  • BroadcastReceiver:This can be used,If you want to send broadcasts across applications.

About Code:

I have created 3 java classes namely broadcast,MyService,MyActivity.

  • broadcast.java :This class inherits BroadcastReceiver class and contains method onReceive(Context context, Intent intent) to create an intent and to call startService() method.
  • MyService.java:This class inherits Service class and contains method onBind(Intent intent) to create intent and to call startActivity().
  • MainActivity.java:This class inherits Activity class and contains method onCreate() to create an application.

Output:

Output

Manifest File:AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.all.ActivityServiceBroadcast">
<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"
tools:ignore="GoogleAppIndexingWarning">
<receiver android:name=".broadcast">
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
</intent-filter>
</receiver>
<service
android:name=".MyService"
android:enabled="false"
android:exported="true"></service>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Layout file:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

BroadcastReceiver file:broadcast.java

package com.all.ActivityServiceBroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class broadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context, MyService.class);
context.startService(serviceIntent);
String action = intent.getAction();
}
}

Service file:MyService.java

package com.all.ActivityServiceBroadcast;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service {
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
Intent ActivityIntent = new Intent(this,MainActivity.class);
startActivity(ActivityIntent);
throw new UnsupportedOperationException("Not yet implemented");
}
}

Activity file:MainActivity.java

package com.all.ActivityServiceBroadcast;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle SavedInstanceState) {
super.onCreate(null);
setContentView(R.layout.activity_main);
}
}


Leave a Reply

Top
%d bloggers like this: