Well, I have been looking up answers, but none have worked for me. I have a button that when it is HELD DOWN, is suppose to continue to move an image across the screen. But for some reason, it just keeps freezing my kindle when I tap the button. I think the thread is part of the problem, but i'm unsure. Here is the code:
package com.evilsea.darkages;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnTouchListener, Runnable {
ImageView leftImageButton;
boolean leftButtonDown = false;
ImageView knight;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread thread1 = new Thread(this);
thread1.start();
this.run();
}
public void addListenerOnLeftButton() {
leftImageButton = (ImageView) findViewById(R.id.left_button);
leftImageButton.setOnTouchListener(leftButtonlistener);
knight = (ImageView) findViewById(R.id.knight_image);
}
OnTouchListener leftButtonlistener = new OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
leftButtonDown = true;
try {
while(leftButtonDown) {
Thread.sleep(10);
moveLeft(5);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
case MotionEvent.ACTION_UP:
leftButtonDown = false;
break;
}
return false;
}
};
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
#Override
public void run() {
addListenerOnLeftButton();
}
public void moveLeft(int speed) throws InterruptedException {
knight.setLeft(knight.getLeft() -speed);
}
}
BTW, I am just starting out w/ android, so I am sorry if this is an obvious question. Thanks! Much appreciated.
Firstly, you are making a UI thread sleep that's why your UI is getting freeze.Here, i have created a async task where i am sleeping background thread and running a UI update on main thread as you can see below in doInBackground method.Here is your solution -:
public class MainActivity extends Activity {
private ImageView leftImageButton;
private boolean leftButtonDown = false;
private ImageView knight;
private AsyncTask asyncTask = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
leftImageButton = (ImageView) findViewById(R.id.imageView2);
knight = (ImageView) findViewById(R.id.imageView1);
leftImageButton.setOnTouchListener(new LeftImageListener());
}
public void startTask() {
asyncTask = new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
if (leftButtonDown) {
while (leftButtonDown) {
try {
Thread.sleep(10);
runOnUiThread(new Runnable() {
#Override
public void run() {
try {
moveLeft(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
return null;
}
}.execute();
}
public void moveLeft(int speed) throws InterruptedException {
knight.setLeft(knight.getLeft() - speed);
}
public class LeftImageListener implements OnTouchListener {
public LeftImageListener() {
// TODO Auto-generated constructor stub
}
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
leftButtonDown = true;
startTask();
break;
case MotionEvent.ACTION_UP:
leftButtonDown = false;
break;
default:
break;
}
return true;
}
}
}
create a second thread was right but you should fill it with some action cause the thread you`ve created is an empty one :)
and you dont need to call run after starting your thread.
Thx Abhishek Birdawade for doing the code work for me :)
Related
I want to display a TextView during 10 seconds, then make it disappear, every 5 minutes, but I am not able to do it, I have already read this Android CountDownTimer - adding time results in multiple timers running, and many others, but still cant do it, here is what I have tried
private void placeFingerPrint() {
authViewModel.getSession().compose(bindToLifecycle()).subscribe(session -> {
this.session = session;
TextView textView = view.findViewById(R.id.player_finger_print);
MediaItem mediaItem = getPlayingMediaItem();
new CountDownTimer(20000, 10000) {
#Override
public void onTick(long millisUntilFinished) {
if (fingerprint.getChannel() != null && fingerprint.getChannel().contains(mediaItem.getExternalId())) {
textView.setVisibility(View.VISIBLE);
textView.setText(session.getHouseHoldId());
} else {
textView.setVisibility(View.GONE);
}
}
#Override
public void onFinish() {
textView.setVisibility(View.GONE);
start();
}
}.start();
});
We can achieve this using Thread in android. CountDownTimer usually uses if you have a definite end time.
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class ThreadSample extends AppCompatActivity {
Handler uiHandler = new Handler();
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_thread_sample);
textView = findViewById(R.id.textView);
NameCounter runnable = new NameCounter();
new Thread(runnable).start();
}
class NameCounter implements Runnable {
int count = 0;
boolean show = true;
#Override
public void run() {
while (true) { // add your customized condition here to exit from the loop.
uiHandler.post(new Runnable() {
#Override
public void run() {
if (show)
textView.setVisibility(View.VISIBLE);
else
textView.setVisibility(View.GONE);
}
});
try {
if (show) {
Thread.sleep(10000);
show = false;
} else {
Thread.sleep(5000);
show = true;
}
} catch (Exception e) {
Log.e("ERROR", e.getMessage());
}
}
}
}
}
i'm trying to play stream radio using Mediaplayer with MP1 as variable of Mediaplayer i want to play it in all Fragments app,expect one activity (ActivityOne) which is contains another Mediaplayer MP2 to play,so i want to stop MP1 when i'm in (ActivityOne) activity, and play MP2 , and when i return from (ActivityOne) i want to resume MP1, my big problem is the (ActivityOne) called when i click button which is exist in fragment
my code below works only in one direction :
when i return from (ActivityOne) activity, the music stops.
structure of the app : MainAcitivty > Fragment > ActivityOne
MainActivity.java
MediaPlayer MP1;
boolean prepared = false;
boolean started = false;
PlayerTask playerTask;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = new MediaPlayer();
playerTask = new PlayerTask();
playerTask.execute(stream);
/**/
MusicButton = findViewById(R.id.toggleButton);
MusicButton.setVisibility(View.INVISIBLE);
MusicButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (started && MusicButton.isChecked()) {
started = false;
MP1.pause();
MusicButton.setChecked(true);
} else {
started = true;
MP1.start();
MusicButton.setChecked(false);
}
}
});
}
#SuppressLint("StaticFieldLeak")
public class PlayerTask extends AsyncTask<String, Void, Boolean> {
ProgressBar loadingRL = findViewById(R.id.progressBar);
#Override
protected void onPreExecute() {
super.onPreExecute();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
AudioAttributes attribs = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).build();
MP1.setAudioAttributes(attribs);
} else {
MP1.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
loadingRL.setVisibility(View.VISIBLE);
}
#Override
protected Boolean doInBackground(String... strings) {
try {
MP1.setDataSource(strings[0]);
MP1.prepare();
prepared = true;
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
MP1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer MP1) {
MP1.start();
}
});
return prepared;
}
#Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
MusicButton.setVisibility(View.VISIBLE);
MusicButton.setChecked(true);
loadingRL.setVisibility(View.VISIBLE);
}
ActivityOne.java
MediaPlayer MP2;
boolean prepared = false;
boolean started = false;
ToggleButton music;
PlayerTask playerTask = null;
CoordinatorLayout coordinatorLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pop_for_ringtone);
coordinatorLayout = findViewById(R.id.coord);
MP2 = new MediaPlayer();
playerTask = new PlayerTask();
playerTask.execute(url);
}
#SuppressLint("StaticFieldLeak")
public class PlayerTask extends AsyncTask<String, Void, Boolean> {
ProgressBar pb = findViewById(R.id.progress);
#Override
protected void onPreExecute() {
super.onPreExecute();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
AudioAttributes attribs = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).build();
MP2.setAudioAttributes(attribs);
} else {
MP2.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
}
#Override
protected Boolean doInBackground(String... strings) {
if (!isCancelled()) {
try {
MP2.setDataSource(strings[0]);
MP2.prepare();
prepared = true;
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
MP2.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer MP2) {
MP2.start();
}
});
}
return prepared;
}
#Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
music.setEnabled(true);
music.setVisibility(View.VISIBLE);
music.setChecked(true);
all.setVisibility(View.VISIBLE);
}
#Override
protected void onCancelled(Boolean aBoolean) {
if (isCancelled() && MP2.isPlaying()) {
MP2.stop();
}
}
}
#Override
public void onBackPressed() {
if (playerTask != null && playerTask.getStatus() == AsyncTask.Status.FINISHED) {
if (MP2.isPlaying()) {
MP2.stop();
}
} else if (playerTask != null && playerTask.getStatus() != AsyncTask.Status.FINISHED) {
playerTask.cancel(true);
}
super.onBackPressed();
}
i spent 2 days to resolve this problem without any result ,please someone help me i will be thankful to him
You could solve this by using Otto library. First create a new Java class but choose enum instead and inside enum you can add: PLAY and PAUSE for example:
public enum PlaybackEvent {
PLAY, PAUSE
}
Then if you are not using custom Application class create one and extend Application and override inside onCreate method. Inside your app gradle add compile 'com.squareup:otto:1.3.8' then create an instance of Bus inside Application class and register. For example this would look like this:
public class MApplication extends Application {
public static Bus sBus = new Bus(ThreadEnforcer.MAIN);
#Override
public void onCreate() {
super.onCreate();
sBus.register(this);
}
Don't forget to replace in manifest default application class with your new one
<application
android:name="com.packagename.MApplication"
After that in your MainActivity class override and register/unregister your event bus in onResume and in onPause.
#Override
protected void onResume() {
super.onResume();
try {
MApplication.sBus.register(this);
}
catch(Exception e){
e.printStackTrace();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
try {
MApplication.sBus.unregister(this);
}
catch(Exception e){
e.printStackTrace();
}
}
After that in MainActivity create a public void method passing as parameter PlayBackEvent and Subscribe so you can listen a message which will be send from your fragment class. For example:
#Subscribe
public void handlePlaybackEvent(PlaybackEvent event) {
switch (event) {
case PLAY:
if(MP1.isPlaying())
MP1.pause();
break;
case PAUSE:
if(!MP1.isPlaying())
MP1.play();
break;
}
}
And last thing you have to do is to send the message from your fragment when starting second activity and that will go:
MApplication.sBus.post(PlaybackEvent.PAUSE);
and of course you can also send a message to play again MP1 from second activity overriding onBackPressed putting inside line of code:
MApplication.sBus.post(PlaybackEvent.PLAY);
Hope this will help you to resolve the problem.
Have you tried using startActivityForResult()?
I made a simple game, where whenever my object reaches end of the screen current activity stops and another activity stars.
I have created a simple boolean method called collisionDetection() which checks whether an object is hitting the end of the screen or not. However, I don't know where I should use that method collisionDetection() .
Should I check for collision at onResume? And when collision is detected, how to start another activity and stop the main one?
My class for first activity
public class ZmijicaCrtanje extends AppCompatActivity implements View.OnTouchListener{
ZmijicaSV zmija;
private static int score=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
zmija=new ZmijicaSV(this);
zmija.setOnTouchListener(this);
setContentView(zmija);
}
#Override
protected void onPause() {
super.onPause();
zmija.pause();
}
#Override
protected void onResume() {
super.onResume();
zmija.resume();
}
#Override
public boolean onTouch(View v, MotionEvent event) {
boolean up=zmija.sp.isUp();
boolean right=zmija.sp.isRight();
boolean down=zmija.sp.isDown();
boolean left=zmija.sp.isLeft();
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
if(up) {
zmija.sp.setUp(false);
zmija.sp.setRight(true);
update();
break;
}
if(right) {
zmija.sp.setRight(false);
zmija.sp.setDown(true);
update();
break;
}
if(down) {
zmija.sp.setDown(false);
zmija.sp.setLeft(true);
update();
break;
}
if(left) {
zmija.sp.setLeft(false);
zmija.sp.setUp(true);
update();
break;
}
}
return true;
}
public void update()
{
score++;
}
public int getScore()
{
return score;
}
}
public class ZmijicaSV extends SurfaceView implements Runnable {
private boolean Collision;
Thread t=null;
Pravougaonik pr;
SurfaceHolder holder;
boolean isRunning=false;
SnakeParts sp;
ZmijicaCrtanje zc;
public ZmijicaSV(Context context) {
super(context);
holder=getHolder();
pr=new Pravougaonik(context);
sp=new SnakeParts();
zc=new ZmijicaCrtanje();
Collision=false;
}
public void run()
{
Paint p=new Paint();
p.setColor(Color.BLUE);
p.setStyle(Paint.Style.FILL);
while(isRunning)
{
if(!holder.getSurface().isValid()) {
continue;
}
Canvas c=holder.lockCanvas();
c.drawColor(Color.BLACK);
sp.Draw(c);
pr.onDraw(c);
sp.update();
drawText(c);
holder.unlockCanvasAndPost(c);
try {
t.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void resume()
{
isRunning=true;
t=new Thread(this);
t.start();
}
public void pause()
{
isRunning=false;
while(true)
{
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
t=null;
}
public void drawText(Canvas canvas)
{
Paint paint=new Paint();
paint.setColor(Color.WHITE);
paint.setTextSize(50);
paint.setTypeface(Typeface.create(Typeface.DEFAULT,Typeface.BOLD));
canvas.drawText("Score:"+zc.getScore(),getWidth()-230,getHeight()-25,paint);
}
public boolean checkCollision()
{
if((sp.getHeadX()<-1)||(sp.getHeadX()>getWidth())||(sp.getHeadY()>getHeight())||(sp.getHeadY()<-1))
Collision=true;
return Collision;
}
}
Put the collisionDetection() method in your public void update() method like
public void update()
{
score++;
collisionDetection();
}
And when collition is detected in collisionDetection() start next activity by Intent like
Intent next=new Intent(this.class,your_class_for_secondactivity.class);
startActivity(next);
I have this code in my Android application:
private void showMyViews() {
mAnimation.cancel();
mAnimation.reset();
mAnimateionView.clearAnimation();
mAnimationView.setVisibility(View.GONE);
mOtherViewToHide.setVisibility(View.GONE);
mFirstViewToShow.setVisibility(View.VISIBLE);
mSecondViewToShow.setVisibility(View.VISIBLE);
}
But sometimes mSecondViewToShow appears a little bit before mFirstViewToShow. How could I easily force these Views to appear at the same time?
Some of the code is:
public class mFragment extends Fragment implements OnClickListener, com.squareup.picasso.Callback {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.mFragment, null);
mFirstViewToShow = (RelativeLayout) view
.findViewById(R.id.mFirstViewToShow);
mSecondViewToShow = (RelativeLayout) view
.findViewById(R.id.mSecondViewToShow);
...
...
...
if (isConnected()) {
animateMagnifier();
updateUserLocation();
} else {
}
}
private void animateMagnifier() {
Thread mThread = new Thread(new Runnable() {
#Override
public void run() {
AppLog.Log(TAG, "ver si la lupa sirve");
if (getActivity() != null) {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
mAnimationView.setVisibility(View.VISIBLE);
animeMagnifier = AnimationUtils
.loadAnimation(getActivity(),
R.anim.translate_center_amim);
mAnimationView.startAnimation(animeMagnifier);
// Code use to repeat the animation
// See
// http://stackoverflow.com/questions/4480652/android-animation-does-not-repeat
animeMagnifier
.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(
Animation animation) {
}
#Override
public void onAnimationRepeat(
Animation animation) {
}
#Override
public void onAnimationEnd(
Animation animation) {
animeMagnifier = AnimationUtils
.loadAnimation(
getActivity(),
R.anim.translate_center_amim);
animeMagnifier
.setAnimationListener(this);
mAnimationView
.startAnimation(animeMagnifier);
}
});
}
});
}
}
});
mThread.start();
try {
mThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void updateUserLocation() {
...
...
...
sendDataToServer();
}
private void sendDataToServer() {
...
...
...
findPerson();
}
private void findPerson() {
new BackGroundTaskForFindPerson().execute();
}
private class BackGroundTaskForFindMatch extends
AsyncTask<String, Void, Void> {
#Override
protected Void doInBackground(String... params) {
try {
findPeopleResponse = mServices.makeHttpRequest(
Constant.find_people, Constant.methodeName,
findPeopleValuePairList);
Gson gson = new Gson();
...
...
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
try {
if (success) {
if (mFindPeople.getErrNum() == 2) {
// no one found
mFirstViewToShow.setVisibility(View.GONE);
mSecondViewToShow.setVisibility(View.GONE);
messageTextView.setText(R.string.no_one_near);
} else if (mFindPeople.getErrNum == 3) {
...
} else {
....
}
} else {
messageTextView.setText(R.string.no_one_new);
}
} catch (Exception e) {
....
}
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
}
Set them to INVISIBLE instead of GONE that way they will be attached to your layout but hidden.
At the moment they must be added to your view hierarchy before they are revealed, that's probably what's causing your delay.
I am trying to make a Helicopter-like game (as you can probably tell, I'm using "Rocket Man" instead of a helicopter). I'm new to Android programming and to Stack Overflow, so don't pick apart my code too much (please). Anyway, right now I'm just trying to get the rocketman to move up while touch is held down, and move down while touch is released.
It isn't working out very well.
I'm using a Moto XT886 for testing, (in case that's relevent). When I run the application, it starts up and loads the MainActivity perfectly well. When I touch the screen the first time (boolean start == true), the text views are hidden, as they are supposed to. However, on the second, third, fourth and all the rest of the times I touch the screen (Start == false), nothing happens. There are no errors in Log Cat, the code doesn't seem to recognize the touch.
I'm pretty sure that it has something to do with those while loops, but I'm using this application mainly to learn Android Java and I don't know enough (yet) to recognize that I'm doing something wrong. Are you able to put a while loop with the conditions as the MotionEvent of an onTouchListner? Or is it something else?
Either way, I appreciate the help, this is an awesome website (this is the first question I couldn't find already asked on Stack Overflow, and for all I know, mine could've already been answered)!
Here's the code to my MainActivity, if you need the .xml code as well, (though I'm pretty sure that's unnecessary) just say the word.
public class MainActivity extends ActionBarActivity {
boolean Start;
RelativeLayout rLayout;
TextView gameTitle;
TextView clickStart;
TextView highScore;
Handler handler;
ImageView rocketMan;
float Y;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
rLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (Start == true) {
gameTitle.setVisibility(View.GONE);
clickStart.setVisibility(View.GONE);
highScore.setVisibility(View.GONE);
Start = false;
}
if (Start = false) {
while (event.getAction() == MotionEvent.ACTION_DOWN) {
Y = -7;
new Thread(new Runnable() {
#Override
public void run() {
while (true) {
try {
Thread.sleep(100);
handler.post(new Runnable() {
#Override
public void run() {
HeliMove();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
while (event.getAction() != MotionEvent.ACTION_DOWN) {
Y = 7;
new Thread(new Runnable() {
#Override
public void run() {
while (true) {
try {
Thread.sleep(100);
handler.post(new Runnable() {
#Override
public void run() {
HeliMove();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
return true;
}
});
}
private void initialize() {
rLayout = (RelativeLayout) findViewById(R.id.mainlayout);
Start = true;
gameTitle = (TextView) findViewById(R.id.rocketmanTitle);
clickStart = (TextView) findViewById(R.id.tapToStart);
highScore = (TextView) findViewById(R.id.highScore);
handler = new Handler();
rocketMan = (ImageView) findViewById(R.id.rocketMan);
}
private void HeliMove() {
rocketMan.setY(rocketMan.getY() + Y);
}
}
Thanks a million!