Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Want it to generate random character instead of random numbers. Here is the code so far Output required: MainActivity import android.content.ComponentName; import android.content.Context; import android.content.Intent;

Want it to generate random character instead of random numbers. Here is the code so far

Output required:

image text in transcribed

image text in transcribed

MainActivity

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.TextView; public class MainActivity extends AppCompatActivity { private Button btnStart, btnStop, btnBind, btnUnbind, btnGetNumber; private TextView myTV; private Intent serviceIntent; private RandomNumberService myService; private boolean isServiceBound; private ServiceConnection myServiceConnection; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnStart = (Button) findViewById(R.id.button); btnStop = (Button) findViewById(R.id.button2); btnBind = (Button) findViewById(R.id.button3); btnUnbind = (Button) findViewById(R.id.button4); btnGetNumber = (Button)findViewById(R.id.button5); myTV = (TextView)findViewById(R.id.textview); } public void onClick(View view) { switch (view.getId()){ case R.id.button: serviceIntent = new Intent(getApplicationContext(), RandomNumberService.class); startService(serviceIntent); break; case R.id.button2: //serviceIntent = new Intent(getApplicationContext(), RandomNumberService.class); stopService(serviceIntent); break; case R.id.button3: bindMyService(); break; case R.id.button4: unbindMyService(); break; case R.id.button5: setRandomNumber(); break; } } private void bindMyService() { if(myServiceConnection == null){ myServiceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { RandomNumberService.RandomNumberServiceBinder myServiceBinder = (RandomNumberService.RandomNumberServiceBinder)iBinder; myService = myServiceBinder.getService(); isServiceBound = true; } @Override public void onServiceDisconnected(ComponentName componentName) { isServiceBound = false; } }; } bindService(serviceIntent,myServiceConnection, Context.BIND_AUTO_CREATE); } private void unbindMyService() { if(isServiceBound) { unbindService(myServiceConnection); isServiceBound = false; } } private void setRandomNumber() { if(isServiceBound) myTV.setText("Random Number: " + myService.getRandomNumber()); else myTV.setText("Service is not bound."); } } 

RandomNumberService.java

 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 RandomNumberService extends Service { private int myRandomNumber; private boolean isRandomGeneratorOn; private final int MIN = 0; private final int MAX = 100; private final String TAG = "RandomNumber Service: "; class RandomNumberServiceBinder extends Binder{ public RandomNumberService getService() { return RandomNumberService.this; } } private IBinder myBinder = new RandomNumberServiceBinder(); @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){ try { Thread.sleep(1000); if(isRandomGeneratorOn){ myRandomNumber = new Random().nextInt(MAX)+MIN; Log.i(TAG, "Thread ID is "+Thread.currentThread().getId() + ", Random number is "+myRandomNumber); } }catch(InterruptedException e) { Log.i(TAG, "Thread Interrupted."); } } } private void stopRandomGenerator() { isRandomGeneratorOn = false; } public int getRandomNumber() { return myRandomNumber; } @Override public void onDestroy() { super.onDestroy(); stopRandomGenerator(); Log.i(TAG, "Service Destroyed."); } @Nullable @Override public IBinder onBind(Intent intent) { Log.i(TAG, "In onBind ..."); return myBinder; } } 
Text Merged Manifest Android Monitor Emulator Nexus 6 API 25 Android 7.11. AP12 com.example.jhel.randomnumberserviceapplication (223) logcat Monitors Build Variants 09-15 15:31:40.649 2623-2823/com.example.The.randomnumberserviceapplication I/Randomlumber Services In OnStart Command Thread ID is 1 09-15 15:31:49.852 2823-10206/com.example.jhe 4. randomnumberserviceapplication 1/Randomblumber Service Thread TD 1a 119, Random number 13 45 09-15 15:31:50.853 2023-10206/com.example.The randomnumber serviceapplication 1/Randomlumber Services Thread ID i. 119, Random number is 62 09-15 15:31:51.655 2823-10206/com.example.jhet. randomberserviceapplication 1/Randomblumber Service:: Thread ID 1 119, Random number is 49 09-15 15:31:52.855 2223-10206/com.example.jhe 4. randomberserviceapplication I/Randomblumber Service Thread ID 1. 119, Random sumber is? 09-15 15:31:53.857 2823-10206/com.example.jhe 4. randomberserviceapplication 1/Randoblumber Service:: Thread ID 1= 119. Random number 13 84 09-15 15:31:54.95 2223-10206/com.example. The 4. randomberserviceapplication I/Randomblumber se lication I/Randalumber Services Thread ID : 119, Randon number is 70 09-15 15:31:55.858 2023-10206/com.example. The 4. randomnumber serviceapplication 1/Randoalumber Services: Thread ID 18 119, Random number 1885 09-15 15:31:56.860 2023-10206/com.example.jhe. randomnumber serviceapplication 1/Randallumber Service Thread ID 18 119, Random number is 92 09-15 15:31:57.861 2823-10206/com.example.The.candomber serviceapplication 1/Randallumber Services Thread ID 1a 119, Random number 1a 57 wortes Run 2 TODO Android Monitor Terminal Messages Instant Run applied code changes and restarted the app. // (Don't show again) (minutes ago) Tot Merged Manifest Android Monitor Emulator Nexus 6 APL 25 Android 711 APL com.example.jhe randomnumberserviceapplication 223) loga Motors 09-15 15:33:08.932 2023-10206/com.example.hel.randomnumberservice application I/Random e r Service! ThreadID IN 119, Random number is 31 09-15 15:33:09.933 2023-10206/com.example.ghet. Tandenb ervice application t/Random er Services! Thread TD 18 119, Random number 1945 09-15 15:33:10.9342423-10206/com.example.hel. o berservice application I/Randomber Service!! Thread ID 18 119, Random number is 09-15 15:33:11.935 2625-10206/com.example.The.randomberservice application 1/Random er Service Thread ID 13 119, Random number 1976 09-15 15:33:12.937 2023-10206/com.example.jhe n berserviceapplication I/Randomber Service Thread ID 119, Random number is 76 09-15 15:33:13.937 2823-10206/com.example.The. Tandem berserviceapplication I/randomlumber Service!! Thread TD 19 119. Random number 13 26 09-15 15:33:14.930 2625-10206/com.example.hel.randomnumber service application I/Randomlumber Services Thread ID : 119, Random number is 30 09-15 15:33:15.939 2623-10206/com.example.The.randomnumberserviceapplication I/Randomdumber Services Thread ID 18 119, Random number 1991 09-15 15:33:16.941 2623-10206/com.example.hel. Tandem number serviceapplication 1/Randomlumber Services Thread ID 1 119, Random number is 09-15 15:33:17.414 2823-2523/com.example.The random service application 1/Randomber Services Service Destroyed. Run TODO Android Monitor Terminal Messages Instant Run applied code changes and restarted the app. I Don't show again) 0 minutes ago) W DTP

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

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

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Practical Oracle8I Building Efficient Databases

Authors: Jonathan Lewis

1st Edition

0201715848, 978-0201715842

More Books

Students also viewed these Databases questions

Question

statistical significance (Chapter 5)

Answered: 1 week ago

Question

=+ Is the information source free from bias on the subject?

Answered: 1 week ago

Question

=+ Is the information source knowledgeable about the subject?

Answered: 1 week ago

Question

=+2. How will it be used?

Answered: 1 week ago