Download 6.Arrays

Document related concepts
no text concepts found
Transcript
Mobile Computing
Arrays
Assg1 ExtraCredit1
Assg2 ExtraCredit2
1
Copyright 2014 by Janson Industries
Topics
Copyright 2014 by Janson Industries
▀
Arrays
▀
Looping/Iteration
▀
More GUI
▀
Timers
▀
Random numbers
▀
Touch listeners
▀
Media players
2
Tic Tac Toe
▀
▀
Last piece of our app requires
the extensive use of arrays
An array is a programming data
structure that holds multiple
values
 Think
of it as a single row of
storage spaces
3
Copyright 2014 by Janson Industries
Array
▀
An array’s values
 Must
all be of the same type
 Are
referred to by an index (aka a
subscript)
► The
 Can
index begins at 0
be constant
► Fixed
values for program use
• Discount rates, zip codes, state abbrs

Can be changed
► Multiple
user or file input values
• Shopping cart items
4
Copyright 2014 by Janson Industries
Array
▀
Arrays are fixed in length
 i.e.
Arrays have a size - the max
number of values they can hold
▀
An array is assigned to a
variable
5
Copyright 2014 by Janson Industries
Array
▀
When creating an array
 Specify
the type of values it will
hold
 Specify
the maximum number of
values it will hold
 Create
and assign the array to a
variable
▀
Syntax to create an array
 new
arrayType[size]
► new
String[3];
► new int[6];
Copyright 2014 by Janson Industries
6
Using Arrays In Java
▀
To use or access the array, must
assign to a variable
 arrayType[
] varName = new
arrayType[size]
► String[]
► int[]
zipCodes = new String[3];
qtys = new int[6];
7
Copyright 2014 by Janson Industries
Array
▀
String[] stateAbbrs = new
String[4]; results in:
Index
▀
▀
0
1
2
To refer to a particular value,
use the array variable name
and index number in brackets
stateAbbrs[2] = "FL"; results in
Index
0
1
2
FL
Copyright 2014 by Janson Industries
3
3
8
Array
▀
Let's be clear:
stateAbbrs[2]
= "FL“; results in
data being put into the third
position of the array
Index
0
1
2
3
FL
stateAbbrs[4]
= "GA" results in?
An out of bounds error!
Copyright 2014 by Janson Industries
9
Looping
▀
▀
WHILE and DO WHILE allow
looping based on a condition
FOR allows looping for a set
number of iterations based on a
 Starting
value
 Increment
 Ending
value
value
10
Copyright 2014 by Janson Industries
Looping
▀
WHILE allows looping based on a
condition
while (eof == 0) {
statements to be repeated;
}
▀
FOR allows a certain number of
iterations
for (int j = 0; j < 10; j++) {
statements to be repeated;
}
11
Copyright 2014 by Janson Industries
Looping
▀
DO WHILE checks the condition
after the statements are executed
do {
statements to be repeated;
} while (eof == 0)
 Therefore,
the loop will be executed
at least once
12
Copyright 2014 by Janson Industries
Looping
▀
BREAK and CONTINUE
provide exits from a loop
for (int j = 0; j < 5; j++) {
if (j == 2) {
break;
}
System.out.println("j = " + j);
}
▀
Results in:
j = 0
j = 1
13
Copyright 2014 by Janson Industries
Looping
for (int j = 0; j < 5; j++) {
if (j == 2) {
continue;}
System.out.println(“j = “ + j);
}
▀
Results in:
j
j
j
j
=
=
=
=
0
1
3
4
14
Copyright 2014 by Janson Industries
Loops and Arrays
▀
▀
Loops very good way to process
arrays
For example we could easily
initialize an array with a for loop
String[] arrayOfStrings = new String[5];
for (int i=0; i<arrayOfStrings.length; i++) {
arrayOfStrings[i] =
new String("This string has the value " + i);
}
15
Copyright 2014 by Janson Industries
Loops and Arrays
▀
Could display the array’s
contents with this
System.out.println(arrayOfStrings[0]);
System.out.println(arrayOfStrings[1]);
System.out.println(arrayOfStrings[2]);
System.out.println(arrayOfStrings[3]);
System.out.println(arrayOfStrings[4]);
▀
Ewwwwww…
16
Copyright 2014 by Janson Industries
Loops and Arrays
▀
Much more elegantly
for (int i = 0; i < arrayOfStrings.length; i++) {
System.out.println(arrayOfStrings[i]);
}
▀
Results in:
This
This
This
This
This
string
string
string
string
string
has
has
has
has
has
the
the
the
the
the
value
value
value
value
value
0
1
2
3
4
17
Copyright 2014 by Janson Industries
Nested Loops
▀
▀
Sometimes loops within loops
needed
What do you think would be the
output of this
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i);
}
}
122333444455555
Copyright 2014 by Janson Industries
18
Nested Loops
▀
How about this
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i);
}
System.out.println("");
}
1
22
333
4444
55555
Copyright 2014 by Janson Industries
19
Tic Tac Toe
▀
Need a playing board
New
▀
XML layout: TableLayout
Each square will have a button
When
clicked, button text changed
to X or O and button disabled
Need
▀
a click handler method
Will have a text view at bottom to
give players info
20
Copyright 2014 by Janson Industries
Will take the numbers off later
21
Copyright 2014 by Janson Industries
tttboard XML
▀
New layout (TableLayout) and a
couple new attributes:
 Gravity
controls both vertical and
horizontal alignment
 dp
stands for density independent
pixels
► Buttons
will size correctly for different
screen resolutions
 layout_marginTop
controls space
between title bar and layout
22
Copyright 2014 by Janson Industries
tttboard XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center_horizontal"
android:background="#000000">
<TableLayout android:id="@+id/play_grid"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginTop="5dp">
<TableRow android:gravity="center_horizontal">
<Button android:id="@+id/one" android:layout_width="100dp"
android:layout_height="100dp" android:text="1"
android:textSize="70dp" android:onClick="clickHandler"/>
<Button android:id="@+id/two" android:layout_width="100dp"
android:layout_height="100dp" android:text="2"
android:textSize="70dp" android:onClick="clickHandler" />
<Button android:id="@+id/three" android:layout_width="100dp"
android:layout_height="100dp" android:text="3"
android:textSize="70dp" android:onClick="clickHandler"/>
</TableRow>
Copyright 2014 by Janson Industries
23
<TableRow android:gravity="center_horizontal">
<Button android:id="@+id/four" android:layout_width="100dp"
android:layout_height="100dp" android:text="4"
android:textSize="70dp" android:onClick="clickHandler"/>
<Button android:id="@+id/five" android:layout_width="100dp"
android:layout_height="100dp" android:text="5"
android:textSize="70dp" android:onClick="clickHandler"/>
<Button android:id="@+id/six" android:layout_width="100dp"
android:layout_height="100dp" android:text="6"
android:textSize="70dp" android:onClick="clickHandler"/>
</TableRow>
<TableRow android:gravity="center_horizontal">
<Button android:id="@+id/seven" android:layout_width="100dp"
android:layout_height="100dp" android:text="7"
android:textSize="70dp" android:onClick="clickHandler"/>
<Button android:id="@+id/eight" android:layout_width="100dp"
android:layout_height="100dp" android:text="8"
android:textSize="70dp" android:onClick="clickHandler"/>
<Button android:id="@+id/nine" android:layout_width="100dp"
android:layout_height="100dp" android:text="9"
android:textSize="70dp" android:onClick="clickHandler"/>
</TableRow>
</TableLayout>
Copyright 2014 by Janson Industries
24
tttboard XML
<TextView android:id="@+id/information" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="info"
android:gravity="center_horizontal" android:layout_marginTop="20dp"
android:textSize="20dp" android:textColor="#ffffff" />
</LinearLayout>
▀
▀
Need a new activity called
TTTActivity
TTTActivity needs variables for
An
array of Buttons
TextView
25
Copyright 2014 by Janson Industries
package my.first.pkg;
import
import
import
import
android.app.Activity;
android.os.Bundle;
android.widget.Button;
android.widget.TextView;
Get the buttons
And text view
public class TTTActivity extends Activity {
private Button tttButtons[];
private TextView infoTV;
private int boardSize = 9;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tttboard);
tttButtons = new Button[boardSize];
tttButtons[0] = (Button) findViewById(R.id.one);
tttButtons[1] = (Button) findViewById(R.id.two);
tttButtons[2] = (Button) findViewById(R.id.three);
tttButtons[3] = (Button) findViewById(R.id.four);
tttButtons[4] = (Button) findViewById(R.id.five);
tttButtons[5] = (Button) findViewById(R.id.six);
tttButtons[6] = (Button) findViewById(R.id.seven);
tttButtons[7] = (Button) findViewById(R.id.eight);
tttButtons[8] = (Button) findViewById(R.id.nine);
infoTV = (TextView) findViewById(R.id.information);
}
}
Copyright 2014 by Janson Industries
26
Tic Tac Toe
▀
Need to initialize the board
 Take
▀
the numbers off
 Enable
the buttons
 Prompt
the players to start
Will create a new method called
start to do this
27
Copyright 2014 by Janson Industries
28
Copyright 2014 by Janson Industries
TTTActivity
▀
Need to code click handler to
 Change
button text to correct
symbol
 Disable
the clicked button
 Prompt
the other player to go
► Must
have a new class level variable
to keep track of who’s turn it is
private String whosTurn;
► start()
must initialize to X
whosTurn = "X";
29
Copyright 2014 by Janson Industries
Click Handler
import android.view.View;
import android.graphics.Color;
:
:
:
:
private String whosTurn;
:
:
:
:
public void clickHandler(View target){
for (int i = 0; i < tttButtons.length; i++) {
if (target == tttButtons[i]){
tttButtons[i].setEnabled(false);
tttButtons[i].setText(whosTurn);
if (whosTurn == "X"){
tttButtons[i].setTextColor(Color.GREEN);
whosTurn = "O";
} else {
tttButtons[i].setTextColor(Color.RED);
whosTurn = "X";}
infoTV.setText("It's " + whosTurn + "'s turn now." );
break;
}
}
}
30
Copyright 2014 by Janson Industries
Tic Tac Toe
▀
Modify HowdyActivity to run it
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.browser) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://fscj.edu"));
startActivity(intent);
} else {
if (item.getItemId() == R.id.order) {
Intent intent = new Intent("my.first.pkg.ShowOrder");
startActivity(intent);
} else {
if (item.getItemId() == R.id.end) {
finish();
} else {
String actionName = "my.first.pkg.TTTActivity";
Intent intent = new Intent(actionName);
startActivity(intent);
}
}
}
return true;
} 2014 by Janson Industries
Copyright
31
Tic Tac Toe
▀
Add Tic Tac Toe activity to the
AndroidManifest.xml
<activity android:name="TTTActivity" android:label="Tic Tac Toe" >
<intent-filter>
<action android:name="my.first.pkg.TTTActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
▀
Run app, display menu, choose
Play Tic Tac Toe
32
Copyright 2014 by Janson Industries
33
Copyright 2014 by Janson Industries
34
Copyright 2014 by Janson Industries
35
Copyright 2014 by Janson Industries
Graded Assg 1
▀
Create a method called
checkForGameOver that
 Checks
► Use
for a winner and ties
loops to check for winners
 Displays
the messages
►X
won
► O won
► It’s a tie
36
Copyright 2014 by Janson Industries
Graded Assg 1
▀
▀
Have the program keep track of
how many wins for X and O and
how many ties
Add a textview beneath the
message to display the number
of wins for X and O and ties
37
Copyright 2014 by Janson Industries
Graded Assg 1
▀
▀
Add a menu
and function, so
that the user
can start a new
game
Store menu
definition in a
file called
new_game.xml
38
Copyright 2014 by Janson Industries
39
Copyright 2014 by Janson Industries
Example after 5
games
40
Copyright 2014 by Janson Industries
Whack a Square
▀
▀
▀
▀
Program will randomly display
a square on the screen for a
second
If user touches square, they
get a point
Will need a timer and random
number generator to move the
square around
Game lasts 30 seconds
41
Copyright 2014 by Janson Industries
Will keep score & display a timer to
count down the time left to play
42
Copyright 2014 by Janson Industries
Created new project, package, activity
and layout. Changed layout to above.
43
Copyright 2014 by Janson Industries
WhackaSquare Activity
▀
For displaying, can draw a View
on the screen (aka canvas) from
within the program
▀
▀
▀
Instead of xml layout
Create a class that is a View
Then draw the view on the
screen
44
Copyright 2014 by Janson Industries
WhackaSquare
▀
Create a new View class called
SquareCtrl to display the
square, sense hits, keep score
▀
Make its superclass View
▀
Implement OnTouchListener
▀
Generate inherited abstract
methods
45
Copyright 2014 by Janson Industries
Will have an error
Hover over red squiggly, select first
suggested fix to insert constructor
46
Copyright 2014 by Janson Industries
Should look like this
47
Copyright 2014 by Janson Industries
Whack a Square
▀
SquareCtrl needs class level
variables for
▀
▀
X and Y location coordinates for
the square
Number of clicks, hits, success
percentage
private int xCord, yCord;
private double attempts, score, percentage;
▀
Have Eclipse generate getters
and setters
▀
Copyright 2014 by Janson Industries
Select the code, ALT+SHIFT+S,
Generate Getters and Setters
48
WhackaSquare
▀
SquareCtrl needs a new method
called move which will
▀
▀
Generate random numbers to be
used to calculate the x and y
coordinates
Do this by
▀
▀
Getting the current width and height of
the screen
Multiplying them by the random number
(a decimal value between 0 and 1)
49
Copyright 2014 by Janson Industries
Random Number Generators
▀
▀
Not really random
Complicated algorithm to
imitate randomness
▀
▀
Random method needs a “seed”
number
Traditionally use the current
time’s milliseconds
50
Copyright 2014 by Janson Industries
X and Y Coordinates
▀
▀
When specified for location of
square, indicates the upper left
location of the square
So can’t have coords at bottom
or rightmost of screen because
part of square would be off
screen
▀
Also need to not put square in
score area at bottom
51
Copyright 2014 by Janson Industries
SquareCtrl - move()
import java.util.Random;
:
:
:
protected void move(){
Random randomNumGenerator = new Random();
randomNumGenerator.setSeed(System.currentTimeMillis());
int w = this.getWidth()-20;
int h = this.getHeight()-40;
float randomNum = randomNumGenerator.nextFloat();
this.setxCord((int)(randomNum * w));
randomNum = randomNumGenerator.nextFloat();
this.setyCord((int)(randomNum * h));
}
52
Copyright 2014 by Janson Industries
SquareCtrl - onDraw()
▀
▀
When component is placed on
screen, its onDraw() invoked
Override onDraw() and change to:
▀
Call move()
▀
Set background color to black
▀
▀
Draw a 20x20 pixel red square at the
random location
Display score, percentage, time left
53
Copyright 2014 by Janson Industries
SquareCtrl - onDraw()
▀
▀
Need to import graphics.*
Need class level variables to
calculate time
import android.graphics.*;
:
:
:
int elapsedTime, startTime, timeLeft;
String timeLeftString;
▀
In constructor, attach the listener
to the view and set start time
this.setOnTouchListener(this);
startTime = (int)System.currentTimeMillis()/1000;
54
Copyright 2014 by Janson Industries
SquareCtrl - onDraw()
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
move();
canvas.drawColor(Color.BLACK);
Paint squareColor = new Paint();
squareColor.setColor(Color.RED);
canvas.drawRect(xCord, yCord, xCord+20, yCord+20, squareColor);
Paint scoreColor = new Paint();
scoreColor.setColor(Color.WHITE);
scoreColor.setTextSize(20);
canvas.drawText("Score: " + (int)score + " Attempts: " +
(int)attempts + "
Percentage: " + (int)percentage + "%", 10,
this.getHeight()- 5, scoreColor);
elapsedTime = (int)System.currentTimeMillis()/1000 - startTime ;
timeLeft = 30 - elapsedTime;
if (timeLeft <= 0){timeLeft = 0;}
//so negative time not shown
timeLeftString = String.valueOf(timeLeft);
canvas.drawText(timeLeftString, this.getWidth()-25, this.getHeight()- 5,
scoreColor);
}
55
Copyright 2014 by Janson Industries
SquareCtrl - detectHit(int x, int y)
▀
▀
▀
When screen touched, will
invoke detectHit
Supply location of touch
Must calculate if touch within
square boundary
▀
See if location is within the width
and height of square location
56
Copyright 2014 by Janson Industries
SquareCtrl - detectHit(int x, int y)
protected boolean detectHit(int x, int y) {
if ((x >= xCord && x <= xCord + 20) &&
(y >= yCord && y <= yCord + 20)) {
return true;
} else {
return false;
}
}
57
Copyright 2014 by Janson Industries
SquareCtrl - onTouch()
▀
▀
When screen touched, the
onTouchListener’s onTouch
method is invoked
Must extract location of touch
from event object and invoke
detectHit
58
Copyright 2014 by Janson Industries
SquareCtrl - onTouch()
▀
Change onTouch to this
public boolean onTouch(View v, MotionEvent event) {
attempts = attempts + 1;
// calc number of touches
if(detectHit((int)event.getX(), (int)event.getY())){
score = score + 1;
}
percentage = (score/attempts) * 100;
return false;
}
59
Copyright 2014 by Janson Industries
Timers
▀
▀
Classes that enable you to
schedule “tasks”
Schedule means that a task
can be:
▀
▀
Set to run at a particular day and
time (one shot timer)
To be run repeatedly at some
interval
60
Copyright 2014 by Janson Industries
Timers & Tasks
App
1
Creates
2
Creates
Invokes schedule()
passing task &
interval
task
Task
Invokes
run()
timer
Timer
61
Copyright 2014 by Janson Industries
Timers
▀
To create and use a Timer
▀
▀
▀
▀
Code a TimerTask subclass to perform the task
Create a TimerTask subclass variable and
object (passing what it needs to perform the
task– a SquareCtrl variable/object) and assign
the TimerTask object to the variable
Create a Timer variable, a Timer object and
assign the object to the variable
Using the Timer objects schedule method,
specify the TimerTask subclass and timing
Copyright 2014 by Janson Industries
62
Timers
▀
Our app will need two Timers and
two TimerTasks
▀
▀
One Timer (timer) to run
WaSqTimerTask, which invokes the
SquareCtrl’s move method every
second
One Timer (endOfGameTimer) to run
the EndGame TimerTask, which
cancels the first Timer (timer), after
approx 30 seconds
63
Copyright 2014 by Janson Industries
TimerTasks
▀
Create a subclass of TimerTask
▀
▀
▀
WaSqTimerTask
Create a method called run (that
will be invoked by the timer)
WaSqTimerTask needs a
SquareCtrl object and, in run,
executes its move function
▀
Will also invoke postInvalidate to
force the view to be redrawn
64
Copyright 2014 by Janson Industries
sq
WhackaSquare
Square
Control
1
Creates
WaSq
2
Creates
sq
3
Creates
Invokes schedule()
passing task &
interval
Invokes
move()
task
WaSq
Timer
Task
Invokes
run()
timer
Timer
65
Copyright 2014 by Janson Industries
WaSqTimerTask
package com.mygames.www;
import java.util.TimerTask;
public class WaSqTimerTask extends TimerTask {
SquareCtrl sc;
public WaSqTimerTask(SquareCtrl s){
sc = s;
}
public void run() {
sc.move();
sc.postInvalidate();
}
}
Copyright 2014 by Janson Industries
//execute the move method
//forces a draw
66
EndGame TimerTask
▀
▀
Will need the original Timer
object - timer (created to run
the game and change the
square location )
Will invoke that timers cancel
method to end the game
67
Copyright 2014 by Janson Industries
EndGame TimerTask
package com.mygames.www;
import java.util.Timer;
import java.util.TimerTask;
public class EndGame extends TimerTask {
Timer gameTimer;
public EndGame(Timer t){
gameTimer = t;
}
}
public void run() {
gameTimer.cancel();
}
Copyright 2014 by Janson Industries
68
Pulling It All Together
▀
WhackaSquare activity needs to
create
▀
▀
▀
The initial SquareCtrl View and put
it in on the screen
The timer to run WaSqTimerTask
every second
The timer to run EndGame after
30.5 seconds
69
Copyright 2014 by Janson Industries
WhackaSquare
package com.mygames.www;
import
import
import
import
java.util.Date;
java.util.Timer;
android.app.Activity;
android.os.Bundle;
public class WhackaSquare extends Activity {
private SquareCtrl sq;
private Timer timer, endOfGameTimer;
private WaSqTimerTask task;
private EndGame endGameTask;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Whack a Square");
sq = new SquareCtrl(this.getBaseContext());
setContentView(sq);
//puts SquareCtrl on screen 70
Copyright 2014 by Janson Industries
Timer Schedule Method
▀
▀
Must specify the task to be
performed
For recurring execution: must
specify when to invoke the
task’s run method and how
often thereafter to call the run
method
▀
In milliseconds
71
Copyright 2014 by Janson Industries
Timer Schedule Method
▀
▀
For one time execution must
specify
▀
The task to be performed
▀
The date/time to execute the task
We will retrieve the current
date/time and add 30.5 seconds
to it
▀
Extra half a second to let
SquareCtrl start up
72
Copyright 2014 by Janson Industries
WhackaSquare
task = new WaSqTimerTask(sq); // creates timer task
timer = new Timer();
// creates timer
timer.schedule(task, 0, 1000); // timer set to 1 second
endOfGameTimer = new Timer(); // creates 2nd timer
endGameTask = new EndGame(timer); // sends 1st timer
// end time set to 30.5 seconds from now
Date endTime = new Date(System.currentTimeMillis() +
30500);
// timer set to execute 30.5 seconds from now
endOfGameTimer.schedule(endGameTask, endTime);
}
}
73
Copyright 2014 by Janson Industries
▀
Run to test
74
Copyright 2014 by Janson Industries
App Probs – Extra Credit 1
▀
Have to change source code to
rerun and can’t end game
▀
▀
▀
Add a menu with options to play a
new game or exit the application
If new game need to reset score
and time
Add options to change speed and
size of square
75
Copyright 2014 by Janson Industries
New menu
76
Copyright 2014 by Janson Industries
App Probs – Extra Credit 1
▀
▀
If set to easy, make square 25%
bigger and move every 1.25
seconds
If set to hard, make square 25%
smaller and move every .75
seconds
77
Copyright 2014 by Janson Industries
MusicPlayer
▀
▀
▀
New app will have an initial
splash screen controlled by a
handler
Then display a list of songs,
images and buttons
When song button pressed,
song will play
78
Copyright 2014 by Janson Industries
Initial splash screen displayed for 5
seconds
79
Copyright 2014 by Janson Industries
Then main screen with song images,
text and play/pause buttons
80
Copyright 2014 by Janson Industries
Create a new project
Click Next for next 3 screens
81
Copyright 2014 by Janson Industries
Add a new activity
82
Copyright 2014 by Janson Industries
MusicPlayer
▀
▀
Need to copy/import the
splash image (from class
website) into the drawablehdpi folder in the res folder
Then create a new xml
layout file called splash in
the layout folder in res
83
Copyright 2014 by Janson Industries
Right click layout folder, New, Android
XML File
Specify the file name, make sure
LinearLayout is selected, click Finish
84
Copyright 2014 by Janson Industries
MusicPlayer
▀
Add a TextView that:
▀
▀
▀
Fills the layout
Has the splash image specified for the
background
Text centered horizontally at the bottom
<?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" >
<TextView android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/splashimage"
android:text="My Homemade Music Player"
android:gravity="bottom|center_horizontal"
android:textSize="35sp">
</TextView>
85
</LinearLayout>
Copyright 2014 by Janson Industries
MusicPlayer
▀
Need to change MusicPlayerMain
to display the splash page
package my.musicproj.com;
import
import
import
import
import
android.os.Bundle;
android.app.Activity;
android.view.Menu;
android.view.MenuItem;
android.support.v4.app.NavUtils;
public class MusicPlayerMain extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.music_player_main, menu);
return true;
}
86
}
Copyright 2014 by Janson Industries
Run to test
87
Copyright 2014 by Janson Industries
MusicPlayer
▀
▀
Only want the splash page to be
displayed for 5 seconds
Will use a Handler to delay the
display of the main screen
▀
▀
Copyright 2014 by Janson Industries
Alternative to a timer
A Handlers postDelayed method
lets you specify a time delay and
a Runnable object to be
executed
88
Handlers
▀
The Handler will execute the
Runnable object’s run method
▀
▀
In run, create a new intent to
display the main screen
Have MusicPlayerMain invoke a
Handlers postDelayed method
and create and pass a Runnable
object
89
Copyright 2014 by Janson Industries
Handlers
▀
New concept: an object’s method(s)
can be overridden
▀
▀
Just like an object’s property values can
be set, method(s) can be defined when
an object is created
After the keyword new and class name,
in {} specify the new method
new Handler().postDelayed(new Runnable(){
public void run() {
String actionName = "my.musicproj.com.Player";
Intent intent = new Intent(actionName);
startActivity(intent);
}
90
},by5000);
Copyright 2014
Janson Industries
Handlers
▀
This statement creates Handler and
Runnable objects
▀
▀
Does not assign the objects to variables
When Runnable object created, run
method defined
▀
And passed to the postDelayed method
with time delay in milliseconds
new Handler().postDelayed(new Runnable(){
public void run() {
String actionName = "my.musicproj.com.Player";
Intent intent = new Intent(actionName);
startActivity(intent);
}
5000);
Copyright 2014},
by Janson
Industries
91
MusicPlayerMain
package my.musicproj.com;
:
:
:
:
import android.content.Intent;
import android.os.Handler;
:
:
:
:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new Handler().postDelayed(new Runnable(){
public void run() {
String actionName = "my.musicproj.com.Player";
Intent intent = new Intent(actionName);
startActivity(intent);
}
}, 5000);
}
:
:
:
:
92
Copyright 2014 by Janson Industries
Music Player
▀
Need the new activity defined
in the manifest file
<activity android:name="Player" android:label="Play Music" >
<intent-filter>
<action android:name="my.musicproj.com.Player" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
▀
Have to create Player class to
display main layout
93
Copyright 2014 by Janson Industries
Player
▀
Simply displays the main screen
for the music player
package my.musicproj.com;
import android.app.Activity;
import android.os.Bundle;
public class Player extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
}
}
94
Copyright 2014 by Janson Industries
MusicPlayer
▀
Run to test
▀
▀
▀
Splash should be displayed for 5
seconds
Then Hello world screen
(generated by Eclipse) will be
displayed
Want to change generic main to
list songs that can be played
and look like this…
95
Copyright 2014 by Janson Industries
Main screen
96
Copyright 2014 by Janson Industries
Themes
▀
Predefined colors and
components
▀
Used instead of specifying
individual component properties
▀
▀
▀
layout background color
text color
In the Activity definition (in the
Manifest) specify the theme
<activity android:name="Player" android:label="Play Music"
android:theme="@android:style/Theme.Black">
97
Copyright 2014 by Janson Industries
Black background, white text
98
Copyright 2014 by Janson Industries
MusicPlayer
▀
Need to copy artist
images into the
drawable-hdpi folder in
the res folder
99
Copyright 2014 by Janson Industries
main.xml
▀
For each song, show:
▀
A picture of the artist
▀
Name of the song
▀
A play button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:id="@+id/artist1"
android:src="@drawable/artist1"
android:layout_width="90dip" android:layout_height="90dip" />
100
Copyright 2014 by Janson Industries
<Button android:id="@+id/one" android:layout_width= "80dp"
android:onClick="clickHandler" android:layout_height="45dp"
android:text="Play" android:textSize="20dp"
android:layout_gravity="center_vertical"
android:gravity="center_vertical|center_horizontal" />
</LinearLayout>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Afraid To Fail"
android:layout_gravity="center_horizontal" />
Main Screen
<LinearLayout
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:id="@+id/artist2"
android:layout_width="90dip" android:layout_height="90dip"
android:src="@drawable/artist2" />
<Button android:id="@+id/two" android:layout_width= "80dp"
android:onClick="clickHandler" android:layout_height="45dp"
android:text="Play" android:textSize="20dp"
android:layout_gravity="center_vertical"
android:gravity="center_vertical|center_horizontal" />
</LinearLayout>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Crazy About You"
android:layout_gravity="center_horizontal" />
101
</LinearLayout>
Copyright 2014 by Janson Industries
MusicPlayer
▀
Time to test
▀
Takes a lot longer to load
because of the image files
▀
Splash screen displayed
▀
Then 5 seconds later...
102
Copyright 2014 by Janson Industries
Main screen displayed
103
Copyright 2014 by Janson Industries
Music Files
▀
▀
Traditionally stored in res
folder in a subfolder
called raw
Need to create the raw
folder then copy/import
the mp3 files into it
104
Copyright 2014 by Janson Industries
Player
▀
▀
▀
▀
Have to add function to play
songs after button clicked
Will need variables for the
buttons and two media players
Must add OnClickListeners to the
buttons and create an onClick
method
onClick determines which button
was clicked and plays song
105
Copyright 2014 by Janson Industries
Variables
▀
MediaPlayers are objects that
will allow music to be played
import android.media.MediaPlayer;
import android.widget.Button;
public class Player extends Activity {
Button oneBtn, twoBtn;
MediaPlayer oneMP, twoMP;
:
:
:
106
Copyright 2014 by Janson Industries
package my.musicproj.com;
import android.app.Activity;
import android.os.Bundle;
import android.media.MediaPlayer;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
Buttons
public class Player extends Activity implements OnClickListener {
Button oneBtn, twoBtn;
MediaPlayer oneMP, twoMP;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
oneBtn = (Button)this.findViewById(R.id.one);
twoBtn = (Button)this.findViewById(R.id.two);
oneBtn.setOnClickListener(this);
twoBtn.setOnClickListener(this);
}
}
public void onClick(View v){}
Copyright 2014 by Janson Industries
107
MediaPlayers
▀
▀
Create the MediaPlayer object
with the static MediaPlayer
method create()
And specify the song file to tie to
the media player
:
:
:
protected void onCreate(Bundle savedInstanceState) {
:
:
:
twoBtn.setOnClickListener(this);
oneMP = MediaPlayer.create(this, R.raw.song1);
twoMP = MediaPlayer.create(this, R.raw.song2);
}
Copyright 2014 by Janson Industries
108
MediaPlayers
▀
Three methods to control
the music
▀
start()
▀
pause()
▀
stop()
▀
To use MediaPlayer after it is
stopped, must invoke prepare()
109
Copyright 2014 by Janson Industries
onClick
▀
▀
When first button is pushed,
start the first media player
When second button is pushed
start the second media player
public void onClick(View v){
if (oneBtn == (Button) v) {
oneMP.start();
} else {
twoMP.start();
}
}
110
Copyright 2014 by Janson Industries
App Problems
▀
▀
If you click the first button
and then the second both
songs play
No way to stop song once
started
111
Copyright 2014 by Janson Industries
Graded Assg 2
▀
▀
▀
Copyright 2014 by Janson Industries
When play button clicked
change text on button to pause
When play button clicked,
check to see if other song is
playing (use MP’s isPlaying()),
if it is, pause it and change
that buttons text to play
When pause btn clicked,
pause the song, change text to
play
112
Assg 2
▀
Initial display
▀
First button clicked
▀
▀
First song played
Text changed to Pause
113
Copyright 2014 by Janson Industries
Assg 2
▀
First button clicked again
▀
▀
▀
First song paused
Text changed to Play
First button clicked again
▀
▀
First song resumed
Text changed to Pause
114
Copyright 2014 by Janson Industries
▀
Second button clicked
▀
▀
▀
▀
▀
First button clicked
▀
▀
▀
▀
Copyright 2014 by Janson Industries
First song paused
First button text changed to
Play
Second song played
Second button text changed to
Pause
Second song paused
Second button text changed to
Play
First song played
First button text changed to
115
Pause
Extra Credit Assg 2
▀
In Whack A Square, add
sounds for hits and misses
116
Copyright 2014 by Janson Industries
package my.other.music;
import
import
import
import
java.io.IOException;
android.app.Activity;
android.media.MediaPlayer;
▀
android.os.Bundle;
Other Options
Can run music from a URL
public class Main extends Activity {
MediaPlayer urlMP;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
urlMP = new MediaPlayer();
try {
urlMP.setDataSource("http://web.fscj.edu/Janson/CIS2930/sample.mp3");
urlMP.prepare();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
urlMP.start();
▀
}
}
You’ll notice how much
faster the app loads/installs117
Copyright 2014 by Janson Industries
Must add
<uses-permission android:name="android.permission.INTERNET" />
to Manifest
118
Copyright 2014 by Janson Industries
Other Options
▀
▀
Run music from the phone’s SD card
We defined SD card earlier when we
created the emulator
▀
But if you didn’t, you can change
119
Copyright 2014 by Janson Industries
Other Options
▀
Specify 100 (MB)
120
Copyright 2014 by Janson Industries
Go to DDMS perspective, click File
Explorer, then sdcard
121
Copyright 2014 by Janson Industries
To move a file onto SD Card, click the
Push file onto device button
122
Copyright 2014 by Janson Industries
Select file, click Open
123
Copyright 2014 by Janson Industries
sdcard folder wont look any different
Indictes the folder to expand, in this
case storage
124
Copyright 2014 by Janson Industries
Scroll down and expand storage and
then sdcard folder and you’ll see the
uploaded file
125
Copyright 2014 by Janson Industries
package my.other.music;
import
import
import
import
java.io.IOException;
android.app.Activity;
android.media.MediaPlayer;
android.os.Bundle;
Other Options
▀
Run music
from SD card
public class Main extends Activity {
MediaPlayer urlMP;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
urlMP = new MediaPlayer();
try {
urlMP.setDataSource("/sdcard/sample.mp3");
urlMP.prepare();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
▀
}
urlMP.start();
}
}
Also will load
& install faster
126
Copyright 2014 by Janson Industries
MediaPlayer
▀
When finished with MP,
execute its release method
▀
▀
Frees up memory
But more importantly, failure to
release
▀
▀
May lead to continuous battery
consumption
Playback failure for other
applications
127
Copyright 2014 by Janson Industries
MediaPlayer
▀
For example, Player's onPause()
could have the releases
▀
▀
Then move all the media player
creations to onRestart()
Also, should have a menu or
button to close app that calls
onDestroy()
▀
In onDestroy(), call finish() which
release all resources
128
Copyright 2014 by Janson Industries