Problem with SMS scheduling(Android) - java

I have designed for schedule SMS in android.i set timer for SMS to send by using CountDownTimer Constructor class. values for timer assigned,but i cannot start the countdown time even i have passed the timer value and interval values. If i start the constructor manually by using object, the sms is sent immediately(not waiting for the time set). I need to send the sms when the time set to send is reached .Here goes my code. Any Help is Appreciated.
new Mycounter(dif, 1000);
// My Counter timer code goes here
public class Mycounter extends CountDownTimer
{
String phoneNo = txtPhoneNo.getText().toString();
String message = txtMessage.getText().toString();
public Mycounter(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
}
#Override
public void onFinish() {
sendSMS(phoneNo, message);
// TODO Auto-generated method stub
}
#Override
public void onTick(long millisUntilFinished)
{
// TODO Auto-generated method stub
}
}

Are you calling start()?
new Mycounter(dif, 1000).start()
Otherwise your timer code looks good.
Add few logs.

Related

Automatic logout of Android App

I know there are several answers to this question, but it isn't working for me, currently my code is:
public class LogoutService extends Service {
public static CountDownTimer timer;
#Override
public void onCreate(){
// TODO Auto-generated method stub
super.onCreate();
timer = new CountDownTimer(1 * 60 * 1000, 1000) {
public void onTick(long millisUntilFinished) {
//Some code
Log.v(TAG, "Service Started");
}
public void onFinish() {
Log.v(TAG, "Call Logout by Service");
// Code for Logout
stopSelf();
}
};
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
and in every activity I have:
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
LogoutService.timer.start();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
LogoutService.timer.cancel();
}
But I get a "Unable to resume activity java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.CountDownTimer android.os.CountDownTimer.start()' on a null object reference" when I try to start any of my activities that contain this code. My application isn't too complicated, it's completely offline, so no web service calls or anything, I just need to go back to my login activity after x minutes. Anyone have any ideas?
Here's the one I used: Auto logout after X minutes, Android and http://androidjug.blogspot.com/2015/10/auto-logout-after-15-minutes-due-to.html
You aren't starting the service, so you aren't allocating the timer. Really you should not be using a static for this.

How can I send latitude longitude of android device every 3 minutes in back end to php web service

based on app id send latitude and longitude and time send to server and use of alram manager
Request Location updates using LocationManager.
NetworkListener listener = new NetworkListener()
locationManager.requestLocationUpdates (LocationManager.NETWORK_PROVIDER, 30000, 0, listener);
Inside Listener onLocationChanged u can write code to send lat, lng to php webservice.
class NetworkListener implements LocationListener{
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
double latitude = location.getLatitude();
double longitude = location.getLongitude();
// write code to send lat, lng to php webservice.
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
You can use timer task that will trigger after each 3 minutes. Please refer below code for that
final Handler handler = new Handler();
timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
/// send lat and long here to your server
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 300000)
User AlarmManager
First Please check the Scheduling Repeating Alarms on android developer site.
and check the following answer here it explains how to run every 5 seconds.
finally check the following tutorial, it explains how to run a service(in your case to call the backend).
Hope that Helps.

handler or chronometer or timertask or countdowntimer which I should use for showing countdowntimer in UI and doing some operation after time is up?

I am doing a quiz application in android, which should exit after the time allotted for that quiz is over and calculate the score when time is up or user pressed submit.For the time up score calculation scenario which one I should use timertask or countdowntimer or handler or chronometer.I would need to show a countdown timer as well until time up. Now after the answers I am further confused.Please suggest.
You should use a Handler, here's an example:
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 100ms
}
}, 100);
If you'd like to terminate the handler at any time, you can call:
handler.removeCallbacks(myRunnable);
From the docs:
public final void removeCallbacksAndMessages (Object token)
Added in API level 1
Remove any pending posts of callbacks and sent messages whose obj is token. If token is null, all callbacks and messages will be removed.
Use this function on your onCreate and do stuff on onFinish of countdown
public void startTicking() {
countDownTimer = new CountDownTimer(timerinterval, 1000) {
#Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
// do stuff here
}
};
}

call a function every 5 seconds

I'm trying to build a simple Android app and I need a function to be called every 5 seconds. I can't flipping figure it out.
The function I need to call every 5 seconds is otherFunction()
Handler mHideHandler = new Handler();
Runnable mHideRunnable = new Runnable() {
#Override
public void run() {
otherFunction();
mSystemUiHider.hide();
}
};
You could refer to the following example:
Handler locationPrompt = new Handler(){
#Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
if(msg.what==SUBJECT){
onLocationChanged(location);
sendEmptyMessageDelayed(SUBJECT, REFRESH);
}
}
};
when calling this method:
locationPrompt.sendEmptyMessage(SUBJECT);
where
final static long REFRESH = 10 * 1000;
final static int SUBJECT = 0;
So this method gets called every 10 seconds here.\
Hope this helps.
Have a look at the Timer class, specifically the schedule methods.

Can't create handler inside thread that has not called Looper.prepare() Android. Loop in a timer

I'm trying to receive message from my php server by creating a thread in a service but it doesnt work. can't go in the while loop and the error message as title shows. Please Help.
//Set the schedule function and rate
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
}
});
}
private void runOnUiThread(Runnable runnable) {
// TODO Auto-generated method stub
String flag = receivermessage();
while (flag.equals("N")){
notifyUser();
}
Log.d(TAG, "Download Successful");
}
}, 0, 10000);
better to use asynctask and receive msg inside asynctask doinbackground() mtd.As webservice call may take more time that u can't block main thread for more than 5sec in android.

Categories

Resources