Terminating timer in android

2019-09-10 08:51发布

问题:

I am a begineer in android app development, i have stoptimertask function in my mainactivity, and a button stop in another activity. What i want to do is when i press this stop button from my 2nd activity(which is maps.class), i want the stoptimertask to stop i.e. stop the tasks. However the app crashes.

Here is my code of mainactivity.java

public class MainActivity extends FragmentActivity{
    protected static final int CONTACT_PICKER_RESULT = 0;
    int count=0;
    Timer timer;
    TimerTask timerTask;
    final Handler handler = new Handler();
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
sendBtn.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {
             startTimer();
            sendSMSMessage();
            Intent toAnotherActivity = new Intent(MainActivity.this, maps.class);
            startActivityForResult(toAnotherActivity, 0);
     }
      });
   }
 public void startTimer() {

        timer = new Timer();
         initializeTimerTask();
        if(radioBtnten.isChecked()==true)
        timer.schedule(timerTask, 5000, 10000);
        // if(radioBtn2.isSelected()==true)
         else if(radioBtnone.isChecked()==true)
        timer.schedule(timerTask, 5000, 1000);
    }
   public void initializeTimerTask() {

        timerTask = new TimerTask() {
            public void run() {
                handler.post(new Runnable() {
                    public void run() {
                        Toast.makeText(getApplicationContext(), "your message has been sent, the message(s) sent are:-"+count++,Toast.LENGTH_LONG).show();
                        sendSMSMessage();    
                    }
                });
            }
        };
    }
   public void stoptimertask(View v) 
   {
        //stop the timer, if it's not already null

       Toast.makeText(getApplicationContext(), "Stop button pressed",Toast.LENGTH_LONG).show();

        if (timer != null) 
        {
            timer.cancel();
            timer = null;
            count = 0;
        }
        MainActivity.this.finish();
    }  
}

Here is the maps.java(2nd activity)

public class maps extends FragmentActivity implements LocationListener {

MainActivity call=new MainActivity();
    GoogleMap googleMap;
    Button stop;
    Timer timer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //show error dialog if GoolglePlayServices not available
        if (!isGooglePlayServicesAvailable()) {
            finish();
        }
        setContentView(R.layout.maps);
        stop = (Button)findViewById(R.id.stop);


        stop.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View aView)
                        {
                               Intent toAnotherActivity = new Intent(aView.getContext(), MainActivity.class);
                               startActivityForResult(toAnotherActivity, 0);
                               Toast.makeText(getApplicationContext(), "Stop button pressed",Toast.LENGTH_LONG).show();
                               maps.this.finish();
                               call.stoptimertask(aView);
                         }
        }); 

here is the logcat

 FATAL EXCEPTION: main
Process: com.example.textmessage, PID: 19869
 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:105)
    at com.example.textmessage.MainActivity.stoptimertask(MainActivity.java:167)
    at com.example.textmessage.maps$1.onClick(maps.java:49)
    at android.view.View.performClick(View.java:4756)
    at android.view.View$PerformClick.run(View.java:19749)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5221)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

回答1:

Best use for this kind of scenario is Singleton pattern.

MainActivity.java

public class MainActivity extends FragmentActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initializeTools();

        // Find reference of "sendBtn" with "findViewById" or other stuff
        sendBtn.setOnClickListener(
                new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        startTimer();
                    }
                });
                // Rest of your code
    }

    private void initializeTools()
    {
        // Give context to Timers instance
        Timers.getInstance().giveContext(this);
    }

    private void startTimer()
    {
        // Starts the timer when you click on "sendBtn"
        Timers.getInstance().startTimers();
    }
}

Timers.java

public class Timers
{
    private final ScheduledExecutorService scheduledExecutorService;
    private final Runnable myTask;
    private ScheduledFuture<?> futureTask;

    private int count = 0;

    private Context _context;

    private static volatile Timers _timers;

    private Timers()
    {
        super();

        // Your "futureTask manager"
        scheduledExecutorService = Executors.newScheduledThreadPool(5);

        // Good use is to instanciate task since it won't change on runtime
        myTask = new Runnable()
        {
            @Override
            public void run()
            {
                // Your code to run after the delay has expired
                Toast.makeText(_context, "your message has been sent, the message(s) sent are:-" + count++, Toast.LENGTH_LONG).show();

                // Same as the whole example, you should use the Singleton pattern to handle communications thanks to the Singleton class "Communicator"
                Communicator.getInstance().sendSMSMessage();
            }
        };
    }

    // Allow only one instance of the class running. Anyone can get reference of the class with the static function Timers.getInstance();
    public static Timers getInstance()
    {
        if (Timers._timers == null)
        {
            synchronized (Timers.class)
            {
                if (Timers._timers == null)
                {
                    Timers._timers = new Timers();
                }
            }
        }

        return Timers._timers;
    }

    // For Toasts and other useful stuff
    public void giveContext(Context context)
    {
        this._context = context;
    }

    // Stop the timer
    public void stopTimer()
    {
        if (futureTask != null)
        {
            futureTask.cancel(true);
        }
    }

    // Starts the task to happen in 10 seconds
    public void startTimers()
    {
        futureTask = scheduledExecutorService.schedule(myTask, 10, TimeUnit.SECONDS);
    }
}

And inside any class of your application, use Timers.getInstance().stopTimer(); to stop the timer and Timers.getInstance().startTimer(); to start it again.



回答2:

Did you try?

mTimer = new Runnable() {
   @Override
   public void run() {
      **return;**