As I am implementing an Android Calculator, I've come across another requirement of client.
I need to implement the feature in calculator that does the following:
2x3 =6, 2x2 = 4, 2x5 = 10.
Hit GT and will bring up 20(adds up all totals)
I've taken Buttons to perform this operations
Whatever button is pressed, I am taking its KeyCode to detect its number.
Then operator is taken as int operator which can take one of four values. i.e. 1=Add,2=Subtract, and all that.
here is a sample code to do such mathematical operation:
private void handleEquals(int newOperator) {
if (hasChanged) {
switch (operator) {
case 1:
num = num + Double.parseDouble(txtCalc.getText().toString());
break;
case 2:
num = num - Double.parseDouble(txtCalc.getText().toString());
break;
case 3:
num = num * Double.parseDouble(txtCalc.getText().toString());
String strNum = null;
strNum = Double.toString(num);
if(strNum.contains("E")){
strNum = strNum.substring(0, 6) + strNum.substring(strNum.indexOf("E"));
}
Log.i("MULTIPLICATION","Checking Precision");
System.out.println("New StrNum is: " + strNum);
num = Double.valueOf(strNum);
break;
case 4:
num = num / Double.parseDouble(txtCalc.getText().toString());
break;
}
Any suggestions or coding snippets would be greatly appreciated.
Thanks
Related
I am taking a crack at Android development and for my first app.. pun intended, I decided to go with a calculator. So my problem is that any arithmetic operator is evaluated as a numeric datatype and crashes the app. I've seen this discussed on other forums but most of them where syntactical issues, and I don't see that here.
Anyway here is my code any help is appreciated.
public void btnEqual_Click(View v){
TextView tv = (TextView)findViewById(R.id.txtInput);
TextView tv2 = (TextView)findViewById(R.id.txtOutput);
String s = tv.getText().toString();
Integer first;
Integer second;
Number result;
Integer x;
for(Integer i = 0; i <= s.length() - 1 ; i++){
switch(s.charAt(i))
{
case '/': x = s.indexOf("/");
first = Integer.valueOf(s.substring(0, x));
second = Integer.valueOf(s.substring(x+1, s.length()));
result = first / second;
tv2.setText(result.toString());
break;
case '*': x = s.indexOf("*");
first = Integer.valueOf(s.substring(0, x));
second = Integer.valueOf(s.substring(x+1, s.length()));
result = first * second;
tv2.setText(result.toString());
break;
case '-': x = s.indexOf("-");
first = Integer.valueOf(s.substring(0, x));
second = Integer.valueOf(s.substring(x+1, s.length()));
result = first - second;
tv2.setText(result.toString());
break;
case '+': x = s.indexOf("+");
first = Integer.valueOf(s.substring(0, x));
second = Integer.valueOf(s.substring(x+1, s.length()));
result = first + second;
tv2.setText(result.toString());
break;
default: break;
}
if(!tv2.getText().toString().isEmpty()){break;}
}
Here is the error I'm getting
Caused by: java.lang.NumberFormatException: Invalid int: "+"
You have to use x+1 in your substring, to cut the operator sign from the numerical value:
Integer.valueOf(s.substring(x+1, s.length() - 1));
Why is the code breaking?
Intro: trying to make a "Choose Your Own Adventure" game.
Layout is composed by:
bPage = button saying what page the user is on. Click to manually change page (not implemented yet)
bOne: go through first path
bTwo: go through second path
tvPageTitle: page title, at the top
mainText: main text body (story explained here)
there's also a splash screen, which works perfectly, but when you click on "start", it loads this layout, which hangs (possible infinite loop)
Can someone tell me why? Tried to put as many comments as I could, so it should be self explanatory
Please ask as many questions as you want :)
package com.assignement.cyoa;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Remember extends Activity {
// Declaring variables
TextView mainText;
int page = 1;
MediaPlayer pageflip;
Boolean gameOver = false;
int currentPage = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.remember);
// Enable manual scrolling text
mainText = (TextView) findViewById(R.id.mainText);
mainText.setMovementMethod(new ScrollingMovementMethod());
// Enable sound when choosing path
pageflip = MediaPlayer.create(Remember.this, R.raw.pageflip);
//TODO: customise story to insert player name. Remember to import method
//EditText userName = LoginActivity.userName;
// Link XML Views with Java
final TextView mainText = (TextView) findViewById(R.id.mainText);
final TextView tvPageTitle = (TextView) findViewById(R.id.tvPageTitle);
final Button bOne = (Button) findViewById(R.id.bOne);
final Button bTwo = (Button) findViewById(R.id.bTwo);
final Button bPage = (Button) findViewById(R.id.bPage);
// Array creation
final String[] storyText = new String[22];
final String[] pageTitle = new String[22];
final CharSequence[] pathA = new CharSequence[22];
final CharSequence[] pathB = new CharSequence[22];
///// MAIN TEXT AND POSSIBLE PATHS ARRAYS \\\\\
// Page 0 - Error
storyText[0] = "Path broken";
pageTitle[0] = "Path broken";
pathA[0]= "Path broken";
pathB[0]= "Path broken";
// Page 1
storyText[1] = "\tYou lift open your eyes. All you can see is a bright light. You get up and take a look around you. You are in a very tight room, with no windows and one door. Everything is painted in white. Beside the door is a small walkie talkie which looks brand new.\n\tYou begin to think, where are you? Who are you? You steady yourself and stumble towards the door. You tightly grab the knob and begin to twist it open. It's locked. Damn. You take another spin around the place. You don't know where you are, you don't know who you are, and you are trapped inside this small room. You pace slowly around the room with your mind thinking intensely on what to do.\n\tYou pick up the walkie talkie. You press the button for the microphone and speak.\n\t''Hello? Is anyone there?''\n\tThe room remains silent until a voice springs out of the device.\n\t''Ahh yes, hello!'' The voice is very high pitched, seemingly happy, yet, sinister. ''Do you remember who you are?''\n\tYou think once more, trying to remember who you are, but nothing manages to spring up.\n\t''Hello? Do you remember-''\n\t''No,'' you answer unhappily. ''I don't''\n\tSilence fills the room once more, but the voice comes back.\n\t''Good, this should make things much more interesting.''\n\tYou don't know whether you should be scared or happy. Questions pop up in your mind.";
pageTitle[1] = "Your New Home";
pathA[1]= "You brush it off and go anyway";
pathB[1]= "Decide it's not worth it and leave";
// Page 2
storyText[2] = "Test";
pageTitle[2] = "Not worth it";
pathA[2]= "Go back";
pathB[2]= "";
// Page 3
storyText[3] = "Test";
pageTitle[3] = "You brush it off";
pathA[3]= "Go upstairs and explore the mysterious shadow";
pathB[3]= "Go to the kitchen to start unpacking";
// Page 4
storyText[4] = "Test";
pageTitle[4] = "You go upstairs";
pathA[4]= "Check it out";
pathB[4]= "Go to the kitchen to start unpacking";
// Page 5 - End
storyText[5] = "Test";
pageTitle[5] = "Checking it out";
pathA[5]= "Try Again";
pathB[5]= "";
// Page 6
storyText[6] = "Test";
pageTitle[6] = "The Kitchen";
pathA[6]= "It's probably just a cat, continue unpacking";
pathB[6]= "Check it out";
// Page 7
storyText[7] = "Test";
pageTitle[7] = "Unpacking";
pathA[7]= "Check out the basement";
pathB[7]= "";
// Page 8
storyText[8] = "Test";
pageTitle[8] = "Checking it out";
pathA[8]= "Check out the basement";
pathB[8]= "";
// Page 9
storyText[9] = "Test";
pageTitle[9] = "The Basement";
pathA[9]= "Forget the light! Get out of here!";
pathB[9]= "Turn the light on anyways";
// Page 10 - End
storyText[10] = "Test";
pageTitle[10] = "Leaving";
pathA[10]= "Try Again";
pathB[10]= "";
// Page 11
storyText[11] = "Test";
pageTitle[11] = "Lights on";
pathA[11]= "Chase the mysterious shadow";
pathB[11]= "Scream and run upstairs";
// Page 12
storyText[12] = "Test";
pageTitle[12] = "The Mysterious Shadow";
pathA[12]= "News report";
pathB[12]= "";
// Page 13 - END
storyText[13] = "Test";
pageTitle[13] = "News Report";
pathA[13]= "Try Again";
pathB[13]= "";
// Page 14
storyText[14] = "Test";
pageTitle[14] = "Upstairs";
pathA[14]= "Just stay where you are";
pathB[14]= "Answer the door";
// Page 15 - End
storyText[15] = "Test";
pageTitle[15] = "Stay where you are";
pathA[15]= "Try Again";
pathB[15]= "";
// Page 16
storyText[16] = "Test";
pageTitle[16] = "The Door";
pathA[16]= "Go to the Attic";
pathB[16]= "";
// Page 17
storyText[17] = "Test";
pageTitle[17] = "The Attic";
pathA[17]= "Leave it alone";
pathB[17]= "Play with the Ouija Board";
// Page 18
storyText[18] = "Test";
pageTitle[18] = "Left Alone";
pathA[18]= "Jump out of the window";
pathB[18]= "Accept your fate";
// Page 19
storyText[19] = "Test";
pageTitle[19] = "The Ouija Board";
pathA[19]= "Jump out of the window";
pathB[19]= "Accept your fate";
// Page 20 - End
storyText[20] = "Test";
pageTitle[20] = "Out the Window";
pathA[20]= "Try Again";
pathB[20]= "";
// Page 21 - End
storyText[21] = "Test";
pageTitle[21] = "Fate Accepted";
pathA[21]= "Try Again";
pathB[21]= "";
// Loop until Game Over
do {
// Populate fields and play page flip sound
mainText.setText(storyText[page]);
tvPageTitle.setText(pageTitle[page]);
bOne.setText(pathA[page]);
bTwo.setText(pathB[page]);
bPage.setText("" + page);
pageflip.start();
currentPage = page;
// Hide button 2 if it doesn't have a possible option
if (pathB[page] == "")
bTwo.setVisibility(View.INVISIBLE);
else
bTwo.setVisibility(View.VISIBLE);
/*
TODO: Enable debug. Manually change page when clicking on the page button
bPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
???
}
});
*/
// Operate first path
bOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (currentPage) {
case 1: page = 3;
break;
case 2: page = 1;
break;
case 3: page = 4;
break;
case 4: page = 5;
break;
case 5: page = 1;
break;
case 6: page = 7;
break;
case 7: page = 9;
break;
case 8: page = 9;
break;
case 9: page = 10;
break;
case 10: page = 1;
break;
case 11: page = 12;
break;
case 12: page = 13;
break;
case 13: page = 1;
break;
case 14: page = 15;
break;
case 15: page = 1;
break;
case 16: page = 17;
break;
case 17: page = 18;
break;
case 18: page = 20;
break;
case 19: page = 20;
break;
case 20: page = 1;
break;
case 21: page = 1;
break;
default: page = 0;
}
}
});
// Operate second path
bTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (currentPage) {
case 1: page = 2;
break;
case 2: page = 0;
break;
case 3: page = 6;
break;
case 4: page = 6;
break;
case 5: page = 0;
break;
case 6: page = 8;
break;
case 7: page = 0;
break;
case 8: page = 0;
break;
case 9: page = 11;
break;
case 10: page = 0;
break;
case 11: page = 14;
break;
case 12: page = 0;
break;
case 13: page = 0;
break;
case 14: page = 16;
break;
case 15: page = 0;
break;
case 16: page = 0;
break;
case 17: page = 19;
break;
case 18: page = 21;
break;
case 19: page = 21;
break;
case 20: page = 0;
break;
case 21: page = 0;
break;
default: page = 0;
}
}
});
} while (gameOver == false);
//TODO: Game Over screen
}
}
EDIT2: adopted new suggestion, made according changes. now layout loads, but clicking a button makes the app crash
package com.assignement.cyoa;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Remember extends Activity {
// Declaring variables
int page;
int currentPage;
MediaPlayer pageflip;
TextView mainText;
TextView tvPageTitle;
Button bOne;
Button bTwo;
Button bPage;
// Array creation
String[] storyText = new String[22];
String[] pageTitle = new String[22];
CharSequence[] pathA = new CharSequence[22];
CharSequence[] pathB = new CharSequence[22];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.remember);
// Link XML Views with Java
TextView mainText = (TextView) findViewById(R.id.mainText);
TextView tvPageTitle = (TextView) findViewById(R.id.tvPageTitle);
Button bOne = (Button) findViewById(R.id.bOne);
Button bTwo = (Button) findViewById(R.id.bTwo);
Button bPage = (Button) findViewById(R.id.bPage);
// Enable manual scrolling text
mainText = (TextView) findViewById(R.id.mainText);
mainText.setMovementMethod(new ScrollingMovementMethod());
// Enable sound when choosing path
pageflip = MediaPlayer.create(Remember.this, R.raw.pageflip);
///// MAIN TEXT AND POSSIBLE PATHS ARRAYS \\\\\
// Page 0 - Error
storyText[0] = "Path broken";
pageTitle[0] = "Path broken";
pathA[0]= "Path broken";
pathB[0]= "Path broken";
// Page 1
storyText[1] = "\tYou lift open your eyes. All you can see is a bright light. You get up and take a look around you. You are in a very tight room, with no windows and one door. Everything is painted in white. Beside the door is a small walkie talkie which looks brand new.\n\tYou begin to think, where are you? Who are you? You steady yourself and stumble towards the door. You tightly grab the knob and begin to twist it open. It's locked. Damn. You take another spin around the place. You don't know where you are, you don't know who you are, and you are trapped inside this small room. You pace slowly around the room with your mind thinking intensely on what to do.\n\tYou pick up the walkie talkie. You press the button for the microphone and speak.\n\t''Hello? Is anyone there?''\n\tThe room remains silent until a voice springs out of the device.\n\t''Ahh yes, hello!'' The voice is very high pitched, seemingly happy, yet, sinister. ''Do you remember who you are?''\n\tYou think once more, trying to remember who you are, but nothing manages to spring up.\n\t''Hello? Do you remember-''\n\t''No,'' you answer unhappily. ''I don't''\n\tSilence fills the room once more, but the voice comes back.\n\t''Good, this should make things much more interesting.''\n\tYou don't know whether you should be scared or happy. Questions pop up in your mind.";
pageTitle[1] = "Your New Home";
pathA[1]= "You brush it off and go anyway";
pathB[1]= "Decide it's not worth it and leave";
// Page 2
storyText[2] = "Test";
pageTitle[2] = "Not worth it";
pathA[2]= "Go back";
pathB[2]= "";
// Page 3
storyText[3] = "Test";
pageTitle[3] = "You brush it off";
pathA[3]= "Go upstairs and explore the mysterious shadow";
pathB[3]= "Go to the kitchen to start unpacking";
// Page 4
storyText[4] = "Test";
pageTitle[4] = "You go upstairs";
pathA[4]= "Check it out";
pathB[4]= "Go to the kitchen to start unpacking";
// Page 5 - End
storyText[5] = "Test";
pageTitle[5] = "Checking it out";
pathA[5]= "Try Again";
pathB[5]= "";
// Page 6
storyText[6] = "Test";
pageTitle[6] = "The Kitchen";
pathA[6]= "It's probably just a cat, continue unpacking";
pathB[6]= "Check it out";
// Page 7
storyText[7] = "Test";
pageTitle[7] = "Unpacking";
pathA[7]= "Check out the basement";
pathB[7]= "";
// Page 8
storyText[8] = "Test";
pageTitle[8] = "Checking it out";
pathA[8]= "Check out the basement";
pathB[8]= "";
// Page 9
storyText[9] = "Test";
pageTitle[9] = "The Basement";
pathA[9]= "Forget the light! Get out of here!";
pathB[9]= "Turn the light on anyways";
// Page 10 - End
storyText[10] = "Test";
pageTitle[10] = "Leaving";
pathA[10]= "Try Again";
pathB[10]= "";
// Page 11
storyText[11] = "Test";
pageTitle[11] = "Lights on";
pathA[11]= "Chase the mysterious shadow";
pathB[11]= "Scream and run upstairs";
// Page 12
storyText[12] = "Test";
pageTitle[12] = "The Mysterious Shadow";
pathA[12]= "News report";
pathB[12]= "";
// Page 13 - END
storyText[13] = "Test";
pageTitle[13] = "News Report";
pathA[13]= "Try Again";
pathB[13]= "";
// Page 14
storyText[14] = "Test";
pageTitle[14] = "Upstairs";
pathA[14]= "Just stay where you are";
pathB[14]= "Answer the door";
// Page 15 - End
storyText[15] = "Test";
pageTitle[15] = "Stay where you are";
pathA[15]= "Try Again";
pathB[15]= "";
// Page 16
storyText[16] = "Test";
pageTitle[16] = "The Door";
pathA[16]= "Go to the Attic";
pathB[16]= "";
// Page 17
storyText[17] = "Test";
pageTitle[17] = "The Attic";
pathA[17]= "Leave it alone";
pathB[17]= "Play with the Ouija Board";
// Page 18
storyText[18] = "Test";
pageTitle[18] = "Left Alone";
pathA[18]= "Jump out of the window";
pathB[18]= "Accept your fate";
// Page 19
storyText[19] = "Test";
pageTitle[19] = "The Ouija Board";
pathA[19]= "Jump out of the window";
pathB[19]= "Accept your fate";
// Page 20 - End
storyText[20] = "Test";
pageTitle[20] = "Out the Window";
pathA[20]= "Try Again";
pathB[20]= "";
// Page 21 - End
storyText[21] = "Test";
pageTitle[21] = "Fate Accepted";
pathA[21]= "Try Again";
pathB[21]= "";
/*
TODO: Enable debug. Manually change page when clicking on the page button
bPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
???
}
});
*/
// Populate fields according to first page
page = 1;
currentPage = page;
mainText.setText(storyText[page]);
tvPageTitle.setText(pageTitle[page]);
bOne.setText(pathA[page]);
bTwo.setText(pathB[page]);
bPage.setText("" + page);
pageflip.start();
// Hide button 2 if it doesn't have a possible option
if (pathB[page] == "")
bTwo.setVisibility(View.INVISIBLE);
else
bTwo.setVisibility(View.VISIBLE);
// Operate first path
bOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (currentPage) {
case 1: page = 3;
break;
case 2: page = 1;
break;
case 3: page = 4;
break;
case 4: page = 5;
break;
case 5: page = 1;
break;
case 6: page = 7;
break;
case 7: page = 9;
break;
case 8: page = 9;
break;
case 9: page = 10;
break;
case 10: page = 1;
break;
case 11: page = 12;
break;
case 12: page = 13;
break;
case 13: page = 1;
break;
case 14: page = 15;
break;
case 15: page = 1;
break;
case 16: page = 17;
break;
case 17: page = 18;
break;
case 18: page = 20;
break;
case 19: page = 20;
break;
case 20: page = 1;
break;
case 21: page = 1;
break;
default: page = 0;
}
setPage(page);
}
});
// Operate second path
bTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (currentPage) {
case 1: page = 2;
break;
case 2: page = 0;
break;
case 3: page = 6;
break;
case 4: page = 6;
break;
case 5: page = 0;
break;
case 6: page = 8;
break;
case 7: page = 0;
break;
case 8: page = 0;
break;
case 9: page = 11;
break;
case 10: page = 0;
break;
case 11: page = 14;
break;
case 12: page = 0;
break;
case 13: page = 0;
break;
case 14: page = 16;
break;
case 15: page = 0;
break;
case 16: page = 0;
break;
case 17: page = 19;
break;
case 18: page = 21;
break;
case 19: page = 21;
break;
case 20: page = 0;
break;
case 21: page = 0;
break;
default: page = 0;
}
}
});
}
private void setPage(int curPage)
{
// Populate fields and play page flip sound
page = curPage;
mainText.setText(storyText[page]);
tvPageTitle.setText(pageTitle[page]);
bOne.setText(pathA[page]);
bTwo.setText(pathB[page]);
bPage.setText("" + page);
pageflip.start();
currentPage = page;
// Hide button 2 if it doesn't have a possible option
if (pathB[page] == "")
bTwo.setVisibility(View.INVISIBLE);
else
bTwo.setVisibility(View.VISIBLE);
}
}
Part of your problem is definitely an infinite loop. You have
} while (gameOver == false);
but you never make gameOver == true. But I would reconsider your design. Instead of having this in a loop, it looks like you could have a function that sets the text of your TextViews and do that work and call that from your onClicks passing the function the int that it needs to select the right pages, etc... There is no reason to continually set your OnClickListeners in a loop. It can be done once in onCreate().
Edit
private void setPage(int curPage)
{
page = curPage;
mainText.setText(storyText[page]);
tvPageTitle.setText(pageTitle[page]);
bOne.setText(pathA[page]);
bTwo.setText(pathB[page]);
bPage.setText("" + page);
pageflip.start();
currentPage = page;
// Hide button 2 if it doesn't have a possible option
if (pathB[page] == "")
bTwo.setVisibility(View.INVISIBLE);
else
bTwo.setVisibility(View.VISIBLE);
}
then in your onClick() call the function
bOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (currentPage) {
case 1: page = 3;
break;
case 2: page = 1;
break;
case 3: page = 4;
break;
...
}
setPage(page);
Declare your variables as member variables before (onCreate()) but initialize them in onCreate()
public class Remember extends Activity {
TextView mainText;
TextView tvPageTitle;
Button bOne;
Button bTwo;
Button bPage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.remember);
mainText = (TextView) findViewById(R.id.mainText);
tvPageTitle = (TextView) findViewById(R.id.tvPageTitle);
bOne = (Button) findViewById(R.id.bOne);
bTwo = (Button) findViewById(R.id.bTwo);
bPage = (Button) findViewById(R.id.bPage);
You can't initialize your Views before you have inflated the layout with setContentView()
You have
TextView mainText = (TextView) findViewById(R.id.mainText);
inside onCreate(). Change that to
mainText = (TextView) findViewById(R.id.mainText);
remove the TextView otherwise you are creating a local variable and your member variables don't have a value so you will get NPE. Do the same with all of your Views.
If i followed your code correctly, it seems you start on page 1, and then just loop.
Your seem to be relying on break; to exit the do-while, but you are just setting the onclicklistener. Are you actually clicking anything? bOne or bTwo i mean. If you dont click them, infinite loop.
Alright, I'm trying to succinct some code in my app. It works fine, I'm just a bit OCD and want to keep improving performance.
Here's what the code in question looks like now:
switch(ressound){
case R.id.button40:
ressound = R.raw.sound40;
soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote40));
break;
}
switch(ressound){
case R.id.button900:
ressound = R.raw.sound900;
soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote900));
break;
}
switch(ressound){
case R.id.button901:
ressound = R.raw.sound901;
soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote901));
break;
}
It's a soundboard app, and this is regarding the save as feature in it. Is there any way to succinct these multiple statements (some screens have 40+ sounds)? Using a loop looks like an obvious choice, however after looking around, the case statement apparently has to be static and not a variable.
EDIT: Forgot to include the actual function header:
public boolean function1(int ressound){
String soundname = "";
int quoteval=0;
switch(ressound){
case R.id.button40:
ressound = R.raw.sound40;
quoteval =R.string.quote40;
break;
case R.id.button900:
ressound = R.raw.sound900;
quoteval =R.string.quote900;
break;
case R.id.button901:
ressound = R.raw.sound901;
quoteval =R.string.quote901;
break;
}
soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(quoteval));
There is a way you can use a loop and eliminate the switch-case statement. Use the Resources#getIdentifier method. Assuming you name your resources with sequential numbers ("sound_filename_1", "sound_filename_2", etc) you can write some code like this:
private static ArrayList<Integer> findSoundResourceIds(Resources res) {
ArrayList<Integer> resIds = new ArrayList<Integer>();
int i = 1;
do {
int resId = res.getIdentifier("sound_filename_"+i, "raw", getPackage().getName());
if (resId == 0) {
break;
}
resIds.add(resId);
i++;
} while (true);
return resIds;
}
in the davik vm/mterp/out/InterpC-portable.cpp code, when interpret invokeMethod, I find it can only handle the case when count is less or equal than 5:
switch (count) {
case 5:
outs[4] = GET_REGISTER(vsrc1 & 0x0f);
case 4:
outs[3] = GET_REGISTER(vdst >> 12);
case 3:
outs[2] = GET_REGISTER((vdst & 0x0f00) >> 8);
case 2:
outs[1] = GET_REGISTER((vdst & 0x00f0) >> 4);
case 1:
outs[0] = GET_REGISTER(vdst & 0x0f);
default:
;
}
then what about the case when the argument size is more than 5?
Sorry I miss something, that the senario I mentioned is the case of non-range call, there is a range call that I missed:
*/
if (methodCallRange) {
// could use memcpy or a "Duff's device"; most functions have
// so few args it won't matter much
assert(vsrc1 <= curMethod->outsSize);
assert(vsrc1 == methodToCall->insSize);
outs = OUTS_FROM_FP(fp, vsrc1);
for (i = 0; i < vsrc1; i++)
outs[i] = GET_REGISTER(vdst+i);
....
so it's handled here!
TelephonyManager.getNetworkType() returns one of the constant values.
It appears that the constant values have an integer order, by possible bearer link speed.
I know using constant values used in the following manner is generally bad,
however could one use this to determine a basic cutoff for application functionality and have it work between API levels? (in API-v1 there was nothing above 0x03)
if( telephonyManager.getNetworkType() > TelephonyManager.NETWORK_TYPE_EDGE )
{
return "3G! party on!";
}
else if( telephonyManager.getNetworkType() > TelephonyManager.NETWORK_TYPE_UNKNOWN )
{
return "2G, OK. just don't go nuts!";
}
else
{
return "No data sorry"
}
You cannot assume they are in order because they are not. For example, LTE is 13 while HSPAP (HSPA+) is 15. Those are not in order. I wrote a "speed ranking" piece of code, which assignes each network type its own speed rank
public static int getNetTypeSpeedRank(int t) {
switch (t) {
case -1:
t = -1;
case ContextManager.MDM_NETWORK_TYPE_UNKNOWN:
t = 0;
break;
case ContextManager.MDM_NETWORK_TYPE_IDEN:
t = 1;
break;
case ContextManager.MDM_NETWORK_TYPE_GPRS:
t = 2;
break;
case ContextManager.MDM_NETWORK_TYPE_EDGE:
t = 3;
break;
case ContextManager.MDM_NETWORK_TYPE_UMTS:
t = 4;
break;
case ContextManager.MDM_NETWORK_TYPE_CDMA:
t = 5;
break;
case ContextManager.MDM_NETWORK_TYPE_1xRTT:
t = 6;
break;
case ContextManager.MDM_NETWORK_TYPE_EVDO_0:
t = 7;
break;
case ContextManager.MDM_NETWORK_TYPE_EVDO_A:
t = 8;
break;
case ContextManager.MDM_NETWORK_TYPE_EVDO_B:
t = 9;
break;
case ContextManager.MDM_NETWORK_TYPE_HSDPA:
t = 10;
break;
case ContextManager.MDM_NETWORK_TYPE_HSUPA:
t = 11;
break;
case ContextManager.MDM_NETWORK_TYPE_HSPA:
t = 12;
break;
case ContextManager.MDM_NETWORK_TYPE_HSPAP:
t = 13;
break;
case ContextManager.MDM_NETWORK_TYPE_EHRPD:
t = 14;
break;
case ContextManager.MDM_NETWORK_TYPE_LTE:
t = 15;
break;
default:
t = 16;
}
return t;
}
I really wouldn't count on that behavior.