在android系统终止计时 [英] Terminating timer in android

查看:174
本文介绍了在android系统终止计时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android应用开发begineer,我有在我mainactivity stoptimertask 的功能,以及按钮停止在另一个活动。我想要做的是,当我preSS该停止按钮从我的第二个活动(这是maps.class),我想在 stoptimertask 以停止即停止工作。然而,应用程序崩溃。

下面是mainactivity.java我的code

 公共类MainActivity扩展FragmentActivity {
    受保护的静态最终诠释CONTACT_PICKER_RESULT = 0;
    诠释计数= 0;
    定时器定时器;
    TimerTask的TimerTask的;
    最后的处理程序处理程序=新的处理程序();
   @覆盖
   保护无效的onCreate(捆绑savedInstanceState){
      super.onCreate(savedInstanceState);
      的setContentView(R.layout.activity_main);
sendBtn.setOnClickListener(新View.OnClickListener(){
         公共无效的onClick(查看视图){
             startTimer所();
            sendSMSMessage();
            意图toAnotherActivity =新意图(MainActivity.this,maps.class);
            startActivityForResult(toAnotherActivity,0);
     }
      });
   }
 公共无效startTimer所(){        定时器=新定时器();
         initializeTimerTask();
        如果(radioBtnten.isChecked()==真)
        timer.schedule(TimerTask的,5000,10000);
        //如果(radioBtn2.isSelected()==真)
         否则,如果(radioBtnone.isChecked()==真)
        timer.schedule(TimerTask的,5000,1000);
    }
   公共无效initializeTimerTask(){        TimerTask的=新的TimerTask(){
            公共无效的run(){
                handler.post(新的Runnable(){
                    公共无效的run(){
                        Toast.makeText(getApplicationContext(),您的信息已发送,发送的消息(S)是: - +算++,Toast.LENGTH_LONG).show();
                        sendSMSMessage();
                    }
                });
            }
        };
    }
   公共无效stoptimertask(视图V)
   {
        //停止计时,如果不是已经空       Toast.makeText(getApplicationContext(),停止按钮pressed,Toast.LENGTH_LONG).show();        如果(定时器!= NULL)
        {
            timer.cancel();
            定时器= NULL;
            计数= 0;
        }
        MainActivity.this.finish();
    }
}

下面是maps.java(第二活动)

 公共类的地图扩展FragmentActivity实现LocationListener的{MainActivity通话=新MainActivity();
    GoogleMap的GoogleMap的;
    按钮停止;
    定时器定时器;
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        //显示错误对话框,如果GoolglePlayServices不可用
        如果(!isGooglePlayServicesAvailable()){
            完();
        }
        的setContentView(R.layout.maps);
        停止=(按钮)findViewById(R.id.stop);
        stop.setOnClickListener(新View.OnClickListener()
        {
            公共无效的onClick(查看aView)
                        {
                               意图toAnotherActivity =新意图(aView.getContext(),MainActivity.class);
                               startActivityForResult(toAnotherActivity,0);
                               Toast.makeText(getApplicationContext(),停止按钮pressed,Toast.LENGTH_LONG).show();
                               maps.this.finish();
                               call.stoptimertask(aView);
                         }
        });

这里是logcat的

 致命异常:主要
工艺:com.example.textmessage,PID:19869
 显示java.lang.NullPointerException:试图调用虚拟方法android.content.Context android.content.Context.getApplicationContext()在空对象引用
    在android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:105)
    在com.example.textmessage.MainActivity.stoptimertask(MainActivity.java:167)
    在com.example.textmessage.maps $ 1.onClick(maps.java:49)
    在android.view.View.performClick(View.java:4756)
    在android.view.View $ PerformClick.run(View.java:19749)
    在android.os.Handler.handleCallback(Handler.java:739)
    在android.os.Handler.dispatchMessage(Handler.java:95)
    在android.os.Looper.loop(Looper.java:135)
    在android.app.ActivityThread.main(ActivityThread.java:5221)
    在java.lang.reflect.Method.invoke(本机方法)
    在java.lang.reflect.Method.invoke(Method.java:372)
    在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)
    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)


解决方案

为这种情况的最佳用途是辛格尔顿的格局。

MainActivity.java

 公共类MainActivity扩展FragmentActivity
{    @覆盖
    保护无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        initializeTools();        //查找sendBtn与findViewById或其他东西的参考
        sendBtn.setOnClickListener(
                新View.OnClickListener()
                {
                    @覆盖
                    公共无效的onClick(视图v)
                    {
                        startTimer所();
                    }
                });
                您code的//休息
    }    私人无效initializeTools()
    {
        //给上下文计时器实例
        。Timers.getInstance()giveContext(本);
    }    私人无效startTimer所()
    {
        //启动定时器,当你点击sendBtn
        Timers.getInstance()startTimers()。
    }
}

Timers.java

 公共类定时器
{
    私人最终ScheduledExecutorService的ScheduledExecutorService的;
    私人最终的Runnable myTask;
    私人ScheduledFuture<> FutureTask提供;    私人诠释计数= 0;    私人语境_context;    私有静态挥发定时器_timers;    私人计时器()
    {
        超();        //你的FutureTask提供经理人
        ScheduledExecutorService的= Executors.newScheduledThreadPool(5);        //用好是实例化的任务,因为它不会在运行时改变
        myTask =新的Runnable()
        {
            @覆盖
            公共无效的run()
            {
                //你的code中的延迟后运行已过期
                Toast.makeText(_context,您的信息已发送,发送的消息(S)是: - +算++,Toast.LENGTH_LONG).show();                //同整个例如,你应该使用Singleton模式来处理这要归功于Singleton类通信通信
                。Communicator.getInstance()sendSMSMessage();
            }
        };
    }    //允许类只有一个运行实例。任何人都可以得到类的引用与静态函数Timers.getInstance();
    公共静态的getInstance计时器()
    {
        如果(Timers._timers == NULL)
        {
            同步(Timers.class)
            {
                如果(Timers._timers == NULL)
                {
                    Timers._timers =新定时器();
                }
            }
        }        返回Timers._timers;
    }    //对于祝酒词和其他有用的东西
    公共无效giveContext(上下文的背景下)
    {
        this._context =背景;
    }    //停止计时器
    公共无效stopTimer()
    {
        如果(FutureTask提供!= NULL)
        {
            futureTask.cancel(真);
        }
    }    //启动任务,在10秒内发生
    公共无效startTimers()
    {
        FutureTask提供= scheduledExecutorService.schedule(myTask,10,TimeUnit.SECONDS);
    }
}

和任何一类的应用程序内部,使用 Timers.getInstance()stopTimer(); 停止定时器和 Timers.getInstance( ).startTimer(); 再次启动它。

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)

解决方案

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.

这篇关于在android系统终止计时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆