I'm having a problem with my program. The process is when the client send a message,the server sends back a message, if the message is "OK", the client will move to the next activity but it didn't. This is my code:
OnClickListener SendOnClickListener = new OnClickListener() {
public void onClick(View v) {
thread.SendSocket(Message.getText().toString());
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// Actions to do after 3 seconds
if(Response.getText().toString().equals("OK")){
Intent i = new Intent(MainActivity.this,LOGIN.class);
startActivity(i);
}
Response.setText(thread.Socketdata );
}
}, 3000);
}
};
Change your if condition as
if(thread.Socketdata.toString().equals("OK")){
Intent i = new Intent(MainActivity.this,LOGIN.class);
startActivity(i);
}
and check.
Related
I want to add some delay after calling an intent from a service. Ex: as shown below
How can i do it?
Any Help would be appreciated.
if (yAccel > 500 || yAccel < -500)
{
gMeasure = true;
lockStatus = 1; //lock done;
Intent intent = new Intent(this, ScreenLock.class);
startActivity(intent);
//delay for min 10 min
}
Have you tried with an Handler?
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(this, ScreenLock.class);
startActivity(intent);
}
}, 1000);
I am using a toast for Count Down Timer, so the toast should change it's text in every second. I use this to display the toast for exactly 1 second but i want the toast to repeat itself. Hope i make you understand.
toast = Toast.makeText(getApplicationContext(), text.getText().toString(), Toast.LENGTH_SHORT); toast.show();
Handler handler = new Handler();
handler.postDelayed
(new Runnable() {
#Override
public void run() {
toast.cancel();
}
}, 1000);
This will show a new toast every second for exactly one second.
int count = 100; //Declare as inatance variable
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
final Toast toast = Toast.makeText(
getApplicationContext(), --count + "",
Toast.LENGTH_SHORT);
toast.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
toast.cancel();
}
}, 1000);
}
});
}
}, 0, 1000);
run() is called after every second. so show toast there.
Handler handler = new Handler();
handler.postDelayed
(new Runnable() {
#Override
public void run() {
toast.cancel();
toast = Toast.makeText(getApplicationContext(), text.getText().toString(),Toast.LENGTH_SHORT);
toast.show();
}
}, 1000);
This page describes a way to keep the toast be shown indefinitely. So when you have the text view of the toast on hand, you may change the text as you like.
you have to learn more about android srvices
create java class extends from IntentService
override this function
#Override
protected void onHandleIntent(Intent intent) {
try {
Toast.makeText(context,"Click on Location button to find your bus !",Toast.LENGTH_LONG).show();
Thread.sleep(5000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
go to manifest an type
go to your launcher java class and
Intent intent = new Intent(this, Service_toast.class);
startService(intent);
====>> for more information about services vist android devloper :
https://developer.android.com/guide/components/services.html
I have made 4 activities in android they all are same. They just have a relative layout and that layout has 4 different images as backgroud,i have set animation on that for 2000miliseconds for example 1st screen should come from right.second from left...etc..I have implemented as below but its not working pls help me..!
screen1.java
Thread splashThread = new Thread() {
public void run() {
try {
sleep(2000);
} catch (Exception e) {
}
startAnimatedActivity(new Intent(SplashActivity1.this,
SplashActivity2.class),
CustAnimatedActivity.SLIDE_FROM_RIGHT);
finish();
}
};
splashThread.start();
same code for 3 activity also..!
i have used "handler" in place of "thread" .I have tried following code..and its working like butter..!
new Handler().postDelayed(new Runnable()
{
#Override
public void run()
{
handler.sendEmptyMessage(1);
}
}, 2000);
}
private Handler handler = new Handler()
{
#SuppressWarnings("deprecation")
#Override
public void handleMessage(android.os.Message msg)
{
try
{
Intent intent = null;
intent = new Intent(SplashActivity1.this,
SplashActivity2.class);
startActivity(intent);
overridePendingTransition(R.anim.animated_activity_slide_left_in, R.anim.animated_activity_slide_right_out);
finish();
} catch (Exception e) {
}
}
};
I have a Service and i try to send message to my primary Activity just like this:
public void callAsynchronousTask() {
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
Message m = new Message();
m.arg1 = 10;
m.arg2 = 15;
handler.sendMessage(m);
Log.i("Sent", "!");
} catch (Exception e) {}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 3000);
}
How can i fetch this message data in my Activity ?
In your activity, you should make a handler like this, and pass the reference to the handler to the service before you start it...
handler = new Handler() {
#Override
public void handleMessage(Message msg) {
//msg.arg1
}
};
but right now you are creating a handler inside the service but thats not what you wanted to do!
Another way would be to bind your Activity to your Service so that they can communicate.
Reference
http://developer.android.com/reference/android/content/Context.html#bindService(android.content.Intent, android.content.ServiceConnection, int)
put your data in message.obj and take it from this field in your activity. Your data can be a class that you can define how you want it.
There is simple example project (which is created by CommonsWare) which shows how to send messages from Service to Activity via Handler. Check it out
//use this code to send data to activity
Bundle data = new Bundle();
data.putString("data", result);
Message msg = Message.obtain();
msg.setData(data);
replyTo.sendMessage(msg); //replyTo is handler declared in your main_Activity
//Pass this **replyTo** handler when you call your service from mainActivity..
Handler replyTo= new Handler() {
public void handleMessage(Message msg) {
//get data here and do what you want..
};
};
Hope it helps..!
I am new in android. Please tell me whether it is possible to send Intent after 5 minute, 10 minutes in android?
How would I do that?
Thanks in advance.
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// Launch new Intent here
}
}, ((1000 * 60) * 5)); // 5 minutes delay before execute run()
see below code it may help you. use this timer for 5 minute.
final Timer myt = new Timer();
myt.schedule(new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
Intent intent= new Intent(currentActivity.this, new_activity.class);
startActivity(intent);
} catch (Exception e) {
// TODO: handle exception
}
myt.cancel();
}
}, 300000);
in above code after call intent timer terminated automatically.
Hm this can go bad if the OS decides to kill your app at some point. If you will really need that intent to still be passed after 5 minutes, you should use alarms. Take a look at this answer
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent= new Intent(youractivity.this, activitytostart5minuteslater.class);
startActivity(intent);
}
}, ((1000 * 60) * 5));
did you ever read the comments???