开发者

How to call method from another class in Android?

I've been trying to call a method from another class.

I have 2 classes. Timer.class and DecisionMaking.class. The Timer class is suppose to call a method (I just used a toast for now) from DecisionMaking.class once the timer is onFinish(). There is no error in my codes but when the timer finish counting, the method was not called and the emulator ask to Force Close. Both of the classes are in the same package. Is there anything that I'm missing?

Timer.class

public class Timer extends Activity{

  TextView timeDisplay;
  MyCount timer;
  int length = 10000;
  long startTime;
  long timeRemaining;
  DecisionMaking decisionmaking;


  /** Called when the activity is first created. */

  public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

     timeDisplay = (TextView) findViewById(R.id.timer);
     timer = new MyCount(length, 1000);

     timer.start();

 } 

public class MyCount extends CountDownTimer {

public MyCount(long millisInFuture, long countDownInterval) {
           super(millisInFuture, countDownInterval);
    }

    public void onFinish() {

      DecisionMaking decisionmaking = new DecisionMaking(); 
 开发者_如何转开发     decisionmaking.sendSms();

     timer.start();
    }

    public void onTick(long millisUntilFinished) {
       timeDisplay.setText("Time:" + millisUntilFinished / 1000);
    }
  }

}

DecisionMaking.class

public class DecisionMaking extends Timer{  

 public void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState);  

    sendSms();
    }

    public boolean sendSms() {

     Calendar cal = Calendar.getInstance();
         Date d = cal.getTime();
         int hour = d.getHours();

     if (hour > 0 && hour < 6)
        return false;
     else
        {
        //Call SMS class here, remove toast
        Toast.makeText(getBaseContext(), "SMS sent.", Toast.LENGTH_SHORT).show();

        return true;
     }
 }
}


i think no need to call timer.start in the onFinish() method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜