Android: service not running - java

I have a service:
import android.app.IntentService;
import android.content.Intent;
import android.os.CountDownTimer;
import android.widget.Button;
public class CustTimer extends IntentService {
int length;
long timeLeft;
Button button;
long endTime;
public CustTimer() {
super("CustTimer");
}
public CustTimer(int length, Button button) {
super("CustTimer");
this.length = length;
this.button = button;
this.endTime = System.currentTimeMillis() + length;
}
#Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
new CountDownTimer(length, 1000) {
public void onTick(long millisUntilFinished) {
String min = String.valueOf(millisUntilFinished/60000 );
long re = millisUntilFinished%60000;
String secs = String.valueOf(re/1000);
button.setText(min + ":" + secs);
timeLeft = millisUntilFinished;
}
public void onFinish() {
//button.setText("done");
}
}.start();
}
}
and an activity which calls it, the onCreate method is:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent intent = new Intent(this, CustTimer.class);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout l2 = new LinearLayout(this);
l2.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout l3 = new LinearLayout(this);
l3.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout l4 = new LinearLayout(this);
l4.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout l5 = new LinearLayout(this);
l5.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams hor = new LayoutParams(0, LayoutParams.MATCH_PARENT, 1f);
LayoutParams ver = new LayoutParams(LayoutParams.MATCH_PARENT, 0, 1f);
for (int i = 0; i < 16; i++){
buts[i] = new Button(this);
buts[i].setText(String.valueOf(i+1));
timerRun[i] = false;
}
for (int i = 0; i < 4; i++) {
l2.addView(buts[i], hor);
}
for (int i = 4; i < 8; i++) {
l3.addView(buts[i], hor);
}
for (int i = 8; i < 12; i++) {
l4.addView(buts[i], hor);
}
for (int i = 12; i < 16; i++) {
l5.addView(buts[i], hor);
}
for (int i = 0; i < 16; i++) {
final int j = i;
buts[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CustTimer t1 = new CustTimer(times[j], buts[j]);
timers[j] = t1;
startService(intent);
}
});
}
ll.addView(l2, ver);
ll.addView(l3, ver);
ll.addView(l4, ver);
ll.addView(l5, ver);
this.setContentView(ll);
}
My problem is that the startService() call doesn't return any errors however it wont run. just looking for some basic input on if i've missed anything important to do with services
edit:
manifest section is:
<service android:name="CustTimer"> </service>

There's an easier way to implement a simple timer:
private final Handler mHandler = new Handler();
private final Runnable mRunnable = new Runnable() {
#Override
public void run() {
// TODO: update button text, manipulate views, etc.
mHandler.postDelayed(mRunnable, 1000);
}
};
private void start() {
mHandler.postDelayed(mRunnable, 1000);
}
private void stop() {
mHandler.removeCallbacks(mRunnable);
}

Related

Increase animation speed when using Clipdrawable in Android studio?

From this thread: How to Customize a Progress Bar In Android
I made my own progress bar using ClipDrawAble animation and it works perfecty, and the code is here:
public class MainActivity extends AppCompatActivity {
private EditText etPercent;
private ClipDrawable mImageDrawable;
private Button fly_to_50;
// a field in your class
private int mLevel = 0;
private int fromLevel = 0;
private int toLevel = 0;
//public static final int MAX_LEVEL = 10000;
public static final int MAX_LEVEL = 10000;
public static final int LEVEL_DIFF = 100;
public static final int DELAY = 0;
public static final int START_AT_50 = 50;
private Handler mUpHandler = new Handler();
private Runnable animateUpImage = new Runnable() {
#Override
public void run() {
doTheUpAnimation(fromLevel, toLevel);
}
};
private Handler mDownHandler = new Handler();
private Runnable animateDownImage = new Runnable() {
#Override
public void run() {
doTheDownAnimation(fromLevel, toLevel);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etPercent = (EditText) findViewById(R.id.etPercent);
ImageView img = (ImageView) findViewById(R.id.imageView1);
mImageDrawable = (ClipDrawable) img.getDrawable();
mImageDrawable.setLevel(0);
}
private void doTheUpAnimation(int fromLevel, int toLevel) {
mLevel += LEVEL_DIFF;
mImageDrawable.setLevel(mLevel);
if (mLevel <= toLevel) {
mUpHandler.postDelayed(animateUpImage, DELAY);
} else {
mUpHandler.removeCallbacks(animateUpImage);
MainActivity.this.fromLevel = toLevel;
}
}
private void doTheDownAnimation(int fromLevel, int toLevel) {
mLevel -= LEVEL_DIFF;
mImageDrawable.setLevel(mLevel);
if (mLevel >= toLevel) {
mDownHandler.postDelayed(animateDownImage, DELAY);
} else {
mDownHandler.removeCallbacks(animateDownImage);
MainActivity.this.fromLevel = toLevel;
}
}
public void onClick50(View v){
mImageDrawable.setLevel(START_AT_50);
}
public void onClickOk(View v) {
//int temp_level = ((Integer.parseInt(etPercent.getText().toString())) * MAX_LEVEL) / 100;
int temp_level = ((Integer.parseInt(etPercent.getText().toString())) * MAX_LEVEL) / 100;
if (toLevel == temp_level || temp_level > MAX_LEVEL) {
return;
}
toLevel = (temp_level <= MAX_LEVEL) ? temp_level : toLevel;
if (toLevel > fromLevel) {
// cancel previous process first
mDownHandler.removeCallbacks(animateDownImage);
MainActivity.this.fromLevel = toLevel;
mUpHandler.post(animateUpImage);
} else {
// cancel previous process first
mUpHandler.removeCallbacks(animateUpImage);
MainActivity.this.fromLevel = toLevel;
mDownHandler.post(animateDownImage);
}
}
}
I set my "DELAY" variable to be 0, which increased the speed a little bit, however, I am not completely satisfied with the speed and would love to increase the speed more. Is this somehow possible? And if not, is there a chance that I can create a normal progress(would work here for sure), but using my own custom edited images?
Appreciate all answers!
Thank you.
SOLVED:
I changed this line:
mDownHandler.postDelayed(animateDownImage, DELAY);
with this:
mDownHandler.postAtTime(animateDownImage,DELAY);

Can not trigger on Click

Can not trigger on Click listener
I tried
setClickable(true);
but still not working
minimum sdk is 15
the code doesn't have xml file, i need to fix it programmatically
the complete code:
public class MainActivity extends AppCompatActivity {
private static char CHAR = 'a';
private Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
int rowNo = ROWS;
int colNo = (screenWidth().widthPixels * ROWS /screenWidth().heightPixels) ;
for (int i = 0; i < 5; i++) {
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f));
for (int j = 0; j < 5; j++) {
String txt = String.valueOf(CHAR);
Cell cell = new Cell(this, txt, 50, 50);
cell.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
System.out.println("value is " + txt);
}
});
row.addView(cell);
CHAR += 1;
}
layout.addView(row);
}
setContentView(layout);
}
private class Cell extends LinearLayout{
public Cell(Context context, final String value, int width, int height) {
super(context);
LinearLayout.LayoutParams cellParams = new LinearLayout.LayoutParams(width, height);
setLayoutParams(cellParams);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
setBackground(getGradientDrawable());
}else{
setBackgroundDrawable(getGradientDrawable());
}
}
private GradientDrawable getGradientDrawable() {
GradientDrawable shape = new GradientDrawable();
shape.setShape(GradientDrawable.RECTANGLE);
shape.setColor(Color.TRANSPARENT);
shape.setStroke(2, Color.BLACK);
return shape;
}
}
}
so, why and how to fix it.
at clicking the associated text will be printed
Try onClickListener inside your loop
Cell cell = new Cell(this, txt, 50, 50);
cell.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
System.out.println("value is " + value);
}
});

Disabling onClick until int = something

public class MainActivity extends AppCompatActivity {
int foulCounterA = 0;
int scoreOnePointTeamA = 0;
int periodCount = 0;
private TextView tv1;
private TextView period;
private Button startbtn, cancelbtn;
private ToggleButton togbtn;
private boolean isPaused = false;
private boolean isCanceled = false;
private long remainingTime = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.tv1);
period = (TextView) findViewById(R.id.period_number);
startbtn = (Button) findViewById(R.id.startBtn);
cancelbtn = (Button) findViewById(R.id.cancelBtn);
togbtn = (ToggleButton) findViewById(R.id.togBtn);
cancelbtn.setEnabled(false);
togbtn.setEnabled(false);
startbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
foulCounterA = 0;
foulCounterB = 0;
displayForTeamAFoul(foulCounterA);
displayForTeamBFoul(foulCounterB);
if (periodCount < 3)
periodCount = periodCount + 1;
else periodCount = 4;
period.setText("Period " + periodCount);
startbtn.setEnabled(false);
cancelbtn.setEnabled(true);
togbtn.setEnabled(true);
isPaused = false;
isCanceled = false;
long millisInFuture = 20000; /////20sec
long countDownInterval = 1000; /////1sec
new CountDownTimer(millisInFuture, countDownInterval) {
#Override
public void onTick(long millisUntilFinished) {
if (isPaused || isCanceled) {
cancel();
} else {
tv1.setText("" + millisUntilFinished / 1000);
remainingTime = millisUntilFinished;
}
}
#Override
public void onFinish() {
startbtn.setEnabled(true);
togbtn.setEnabled(false);
if (periodCount < 4)
tv1.setText("Times up!");
else tv1.setText("Game Over!");
}
}.start();
}
});
togbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (togbtn.isChecked()) {
isPaused = true;
} else {
isPaused = false;
long millisInFuture = remainingTime;
long countDownInterval = 1000; /////1sec
new CountDownTimer(millisInFuture, countDownInterval) {
#Override
public void onTick(long millisUntilFinished) {
if (isPaused || isCanceled) {
cancel();
} else {
tv1.setText("" + millisUntilFinished / 1000);
remainingTime = millisUntilFinished;
}
}
#Override
public void onFinish() {
startbtn.setEnabled(true);
togbtn.setEnabled(false);
if (periodCount < 4)
tv1.setText("Times up!");
else tv1.setText("Game Over!");
}
}.start();
}
}
});
cancelbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
isCanceled = true;
period.setText("Period");
tv1.setText("Timer");
startbtn.setEnabled(true);
togbtn.setEnabled(false);
cancelbtn.setEnabled(false);
foulCounterA = 0;
foulCounterB = 0;
periodCount = 0;
displayForTeamAFoul(foulCounterA);
displayForTeamBFoul(foulCounterB);
}
});
}
public void onePointForTeamA(View v) {
scoreTeamA = scoreTeamA + 1;
scoreOnePointTeamA = scoreOnePointTeamA + 1;
displayForTeamA(scoreTeamA);
displayForTeamAOnePoint(scoreOnePointTeamA);
}
public void foulCountForTeamA(View v) {
if (foulCounterA < 5)
foulCounterA = foulCounterA + 1;
else
foulCounterA = 5;
displayForTeamAFoul(foulCounterA);
}
public void displayForTeamAOnePoint(int score) {
TextView scoreView = (TextView) findViewById(R.id.team_a_score_1_point);
scoreView.setText(String.valueOf(score));
}
public void displayForTeamAFoul(int score) {
TextView scoreView = (TextView) findViewById(R.id.team_a_foul);
scoreView.setText(String.valueOf(score));
}
}
I wanted to make the java code as simple as I can so I've just added the lines for my question. What I'm trying to do is
android:onClick="onePointForTeamA"make this button only clickable when foulCounterA = 5 Failed to add if (foulCounterA = 5); inside public void foulCountForTeamA(View v) {
It gave me an error that way. Says required: boolean, found: int.
What should I do with the code? Any help will be appreeciated
Regarding your concrete question, the syntax of if (foulCounterA = 5); is wrong, because the equation check is have to made by == operator.
So the correct syntax would be if (foulCounterA == 5);
As #OH GOD SPIDERS wrote in the comment, you should check the basics of java operators.
Also I recommend You to search for the answer before asking a new question.

Android Studio App Unfortunately has stopped-Java code

My app in Android Studio is OK but when I run it, it tells me your app has closed. The application is a card game.
My code:
## Heading ##
public class MainActivity extends Activity {
*Button btn_retry;*
*int lastImageView = 0;
int beforLastImageView = 0;
int lastCard=0;
int beforLastCard=0;
int imageNumber=0;
int beforImageNumber=0;
int numberOfCards = 6;
int numberOfImages = 30;*
final ImageView[] Imageview = new ImageView[numberOfCards];
int[] Imageview_Id = {R.id.img1, R.id.img2, R.id.img3,
R.id.img4, R.id.img5, R.id.img6};
final int[] images = {R.drawable.i1, R.drawable.i2, R.drawable.i3, R.drawable.i4, R.drawable.i5,
R.drawable.i6, R.drawable.i7, R.drawable.i8, R.drawable.i9, R.drawable.i10,
R.drawable.i11, R.drawable.i12, R.drawable.i13, R.drawable.i14, R.drawable.i15,
R.drawable.i16, R.drawable.i17, R.drawable.i18, R.drawable.i19, R.drawable.i20,
R.drawable.i21, R.drawable.i22, R.drawable.i23, R.drawable.i24, R.drawable.i25,
R.drawable.i26, R.drawable.i27, R.drawable.i28, R.drawable.i29, R.drawable.i30,
R.drawable.i31, R.drawable.i32, R.drawable.i33, R.drawable.i34, R.drawable.i35,
R.drawable.i36, R.drawable.i37, R.drawable.i38, R.drawable.i39, R.drawable.i40};
final List<Integer> cardlist = new ArrayList<Integer>();
#Override
**protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
clickOnButton1();**
final List<Integer> imagelist = new ArrayList<Integer>();
for (int i = 0; i < numberOfCards; i++) {
imagelist.add(i);
}
Collections.shuffle(imagelist);
for (int i = 0; i < numberOfCards; i++) {
Imageview[i] = (ImageView) findViewById(Imageview_Id[i]);
}
final List<Integer> cardlist = new ArrayList<Integer>();
for (int i = 0; i< numberOfCards / 2; i++) {
cardlist.add(imagelist.get(i));
}
for (int i=numberOfCards/2;i<numberOfCards;i++){
cardlist.add(imagelist.get(i-numberOfCards/2));
}
Collections.shuffle(cardlist);
for (int i=0 ; i<numberOfCards;i++){
Imageview[i].setOnClickListener(onClickImage);
}
};
protected View.OnClickListener onClickImage= new
View.OnClickListener() {
**#Override
public void onClick(final View v) {**
switch (v.getId()){
case(R.id.img1):
imageNumber = 1;
case(R.id.img2):
imageNumber = 2;
case(R.id.img3):
imageNumber = 3;
case(R.id.img4):
imageNumber = 4;
case(R.id.img5):
imageNumber = 5;
case(R.id.img6):
imageNumber = 6;
}
android.os.Handler handler =new android.os.Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
String imageName="i"+cardlist.get(imageNumber);
int res = getResources().getIdentifier(imageName, "drawable", "com.example.arash.card");
Imageview[imageNumber].setImageResource(res);
lastImageView=(v.getId());
lastCard=cardlist.get(imageNumber);
}
},1000);
if (lastImageView != beforLastImageView) {
if (lastCard == beforLastCard) {
Imageview[imageNumber].setVisibility(View.INVISIBLE);
Imageview[imageNumber].setEnabled(false);
Imageview[beforImageNumber].setVisibility(View.INVISIBLE);
Imageview[beforImageNumber].setEnabled(false);
beforLastCard = 0;
beforLastImageView = 0;
lastCard = 0;
lastImageView = 0;
} else {
beforLastImageView = lastImageView;
beforLastCard = lastCard;
}
}
beforImageNumber=imageNumber;
}
};
**public void clickOnButton1() {**
final Context context = this;
btn_retry = (Button) findViewById(R.id.button);
btn_retry.setOnClickListener(new View.OnClickListener() {
***#Override***
public void onClick(View arg0) {
Intent intent = new Intent(context, MainActivity.class);
finish();
*startActivity(intent);*
}
});
}

Android Application Button onclicklistener

Hi there i've been constructing this code for a week but i still cant get it to work. It has no errors but when i run it on the AVD it terminates suddenly.
package com.tryout.sample;
import java.util.Random;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.app.Activity;
public class MainActivity extends Activity implements View.OnClickListener{
Random number = new Random();
int Low = 1;
int High = 13;
int RandomNumber = number.nextInt(High-Low) + Low;
int current = 0;
int points=0;
final Integer[] cardid = { R.drawable.card1,
R.drawable.card10,
R.drawable.card11,
R.drawable.card12,
R.drawable.card13,
R.drawable.card2,
R.drawable.card3,
R.drawable.card4,
R.drawable.card5,
R.drawable.card6,
R.drawable.card7,
R.drawable.card8,
R.drawable.card9,
};
ImageView pic2 = (ImageView) findViewById(R.id.imageView1);
final TextView score = (TextView) findViewById(R.id.textView2);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView score = (TextView) findViewById(R.id.textView2);
Button high = (Button) findViewById(R.id.button1);
Button low = (Button) findViewById(R.id.button2);
final ImageView pic = (ImageView) findViewById(R.id.imageView1);
low.setOnClickListener(new view.OnClickListener() {
public void onClick(View v) {
int resource = cardid[RandomNumber];
if(current < RandomNumber){
points = points + 1;
score.setText(points);
pic.setImageResource(resource);
}else{
score.setText("Game Over");
}
}
});
high.setOnClickListener(new View.OnClickListener() {
public void higher(View v) {
int resource = cardid[RandomNumber];
if(current > RandomNumber){
points = points + 1;
score.setText(points);
pic.setImageResource(resource);
}else{
score.setText("Game Over");
}
}
});
int resource = cardid[RandomNumber];
pic.setImageResource(resource);
current = RandomNumber;
}
}
I cant figure out where my problem is, kindly check out my code. THanks for any help
put this:
ImageView pic2 = (ImageView) findViewById(R.id.imageView1);
final TextView score = (TextView) findViewById(R.id.textView2);
in you onCreate method after the call setContentView(R.layout.activity_main);.
How should R.id.imageView1 assigned if the content is not specified like in your case?
ImageView pic2;
TextView score;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pic2 = (ImageView) findViewById(R.id.imageView1);
score = (TextView) findViewById(R.id.textView2);
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class minigame_cardpairing extends Activity implements View.OnClickListener {
private static final int TOTAL_CARD_NUM = 16;
private int[] cardId = {R.id.card01, R.id.card02, R.id.card03, R.id.card04, R.id.card05, R.id.card06, R.id.card07, R.id.card08,
R.id.card09, R.id.card10, R.id.card11, R.id.card12, R.id.card13, R.id.card14, R.id.card15, R.id.card16};
private Card[] cardArray = new Card[TOTAL_CARD_NUM];
private int CLICK_CNT = 0;
private Card first, second;
private int SUCCESS_CNT = 0;
private boolean INPLAY = false;
//----------- Activity widget -----------//
private Button start;
//-----------------------------------//
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.minigame_cardpairing);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
for(int i=0; i<TOTAL_CARD_NUM; i++) {
cardArray[i] = new Card(i/2);
findViewById(cardId[i]).setOnClickListener(this);
cardArray[i].card = (ImageButton) findViewById(cardId[i]); // Card assignment
cardArray[i].onBack();
}
start = (Button) findViewById(R.id.start);
start.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startGame();
//start.setBackgroundDrawable(background);
}
});
findViewById(R.id.exit).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setResult(RESULT_OK);
finish();
}
});
} // end of onCreate
protected void startDialog() {
AlertDialog.Builder alt1 = new AlertDialog.Builder(this);
alt1.setMessage("The match-card game. Please remember to flip the cards two by two card hand is a pair Hit. Hit all pairs are completed.")
.setCancelable(false)
.setPositiveButton("close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alt2 = alt1.create();
alt2.setTitle("Game Description");
alt2.show();
}
protected void clearDialog() {
AlertDialog.Builder alt1 = new AlertDialog.Builder(this);
alt1.setMessage("It fits all the cards in pairs. Congratulations.")
.setCancelable(false)
.setPositiveButton("close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alt2 = alt1.create();
alt2.setTitle("Match-complete");
alt2.show();
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
startDialog();
}
public void onClick(View v) {
if (INPLAY) {
switch (CLICK_CNT) {
case 0:
for (int i=0; i<TOTAL_CARD_NUM; i++) {
if (cardArray[i].card == (ImageButton) v) {
first = cardArray[i];
break;
}
}
if (first.isBack) {
first.onFront();
CLICK_CNT = 1;
}
break;
case 1:
for (int i=0; i<TOTAL_CARD_NUM; i++) {
if (cardArray[i].card == (ImageButton) v) {
second = cardArray[i];
break;
}
}
if (second.isBack) {
second.onFront();
if (first.value == second.value) {
SUCCESS_CNT++;
Log.v("SUCCESS_CNT", "" + SUCCESS_CNT);
if (SUCCESS_CNT == TOTAL_CARD_NUM/2) {
clearDialog();
}
}
else {
Timer t = new Timer(0);
t.start();
}
CLICK_CNT = 0;
}
break;
}
}
}
void startGame() {
int[] random = new int[TOTAL_CARD_NUM];
int x;
for (int i=0; i<TOTAL_CARD_NUM; i++) {
if (!cardArray[i].isBack)
cardArray[i].onBack();
}
boolean dup;
for (int i=0; i<TOTAL_CARD_NUM; i++) {
while(true) {
dup = false;
x = (int) (Math.random() * TOTAL_CARD_NUM);
for (int j=0; j<i; j++) {
if (random[j] == x) {
dup = true;
break;
}
}
if (!dup) break;
}
random[i] = x;
}
start.setClickable(false);
for (int i=0; i<TOTAL_CARD_NUM; i++) {
cardArray[i].card = (ImageButton) findViewById(cardId[random[i]]);
cardArray[i].onFront();
}
Log.v("timer", "start");
Timer t = new Timer(1);
//flag = false;
t.start();
/*
while(true) {
if (flag) break;
//Log.v("flag", "" + flag);
}
Log.v("timer", "end");
*/
SUCCESS_CNT = 0;
CLICK_CNT = 0;
INPLAY = true;
}
class Timer extends Thread {
int kind;
Timer (int kind) {
super();
this.kind = kind;
}
#Override
public void run() {
INPLAY = false;
// TODO Auto-generated method stub
try {
if (kind == 0) {
Thread.sleep(1000);
mHandler.sendEmptyMessage(0);
}
else if (kind == 1) {
Thread.sleep(3000);
mHandler.sendEmptyMessage(1);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
INPLAY = true;
}
}
Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == 0) {
first.onBack();
second.onBack();
first.isBack = true;
second.isBack = true;
}
else if (msg.what == 1) {
//flag = true;
for (int i=0; i<TOTAL_CARD_NUM; i++) {
cardArray[i].onBack();
}
start.setClickable(true);
}
}
};
}
class Card { // start of Card class
private final static int backImageID = R.drawable.cardback;
private final static int[] frontImageID = {R.drawable.card1, R.drawable.card2,
R.drawable.card3, R.drawable.card4,
R.drawable.card5, R.drawable.card6,
R.drawable.card7, R.drawable.card8};
int value;
boolean isBack;
ImageButton card;
Card(int value) {
this.value = value;
}
public void onBack() {
if (!isBack) {
card.setBackgroundResource(backImageID);
isBack = true;
}
}
public void flip() {
if (!isBack) {
card.setBackgroundResource(backImageID);
isBack = true;
}
else {
card.setBackgroundResource(frontImageID[value]);
isBack = false;
}
}
public void onFront() {
if (isBack) {
card.setBackgroundResource(frontImageID[value]);
isBack = false;
}
}
} // end of Card class

Categories