Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

This lab covers broadcasts in Android Programming. The goal is to broadcast the random characters generated by your custom service, so that the main activity

This lab covers broadcasts in Android Programming. The goal is to broadcast the random characters generated by your custom service, so that the main activity registered to intercept the broadcast can get the random numbers and display them on an EditText. the Layout consists of two buttons: Start Service and Stop Service, and one EditText. image text in transcribed

The start button will trigger the random character generator service. The EditText will display the random numbers generated the service in real time (without any button press). The stop button will stop the service. The EditText wont display any numbers. I managed to get the app to compile, but it is not performing as it should, and I do not understand how to do broadcasts. Help on this would be appreciated! activity.xml is below:

 
 MainActivity.java:
package com.example.cs7455lab8; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private Button btnStart, btnStop; private TextView myTV; private Intent serviceIntent; private RandomCharacterService myService; private ServiceConnection myServiceConnection; private boolean isServiceOn; //checks if the service is on @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnStart = (Button) findViewById(R.id.StartButton); btnStop = (Button) findViewById(R.id.StopButton); myTV = (EditText)findViewById(R.id.RandomCharText); String alphabet = myTV.getText().toString(); } public void onClick(View view) { isServiceOn = true; switch (view.getId()){ case R.id.StartButton: serviceIntent = new Intent(getApplicationContext(), RandomCharacterService.class); startService(serviceIntent); break; case R.id.StopButton: isServiceOn = false; stopService(serviceIntent); break; } } private void setRandomNumber() { myTV.setText("Random Character: " + (char)myService.getRandomCharacter()); } } RandomCharacterService.java: 
package com.example.cs7455lab8; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.support.annotation.IntDef; import android.support.annotation.Nullable; import android.util.Log; import java.util.Random; public class RandomCharacterService extends Service { private int myRandomCharacter; char MyRandomCharacter = (char)myRandomCharacter; private boolean isRandomGeneratorOn; private final int MIN = 65; char m = (char)MIN; private final int MAX = 26; char x = (char)MAX; private final String TAG = "Random Char Service: "; private final String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; class RandomCharacterServiceBinder extends Binder { public RandomCharacterService getService() { return RandomCharacterService.this; } } private IBinder myBinder = new RandomCharacterServiceBinder(); @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i(TAG, "In OnStartCommand Thread ID is "+Thread.currentThread().getId()); isRandomGeneratorOn = true; new Thread(new Runnable() { @Override public void run() { startRandomGenerator(); } } ).start(); return START_STICKY; } private void startRandomGenerator() { while(isRandomGeneratorOn) { char alphabet = 'A'; for (int i = 65; i  
 
Android Emulator-Nexus 6 AP MyServiceApplication Hello World START STOP Android Emulator-Nexus 6 AP MyServiceApplication Hello World START STOP

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions