2

My current activity class "TimerAct.java" makes use of a timer of 30secs. Upon the completion of the timer a new activity "SMS.java" must be initiated. For this, in onFinish() i call a method of my current activity that starts the new activity. But i get a msg to "Force Close". Can anyone help me out?

Here's the code:

//TimerAct.java
public class TimerAct extends Activity
{
    static TextView timeDisplay;
    Timer t;
    int length = 30000;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.time);

        timeDisplay = (TextView) findViewById(R.id.timer);
        timeDisplay.setText("Time left: " + length / 1000);
        t = new Timer(length, 1000);
        t.start();
    }   

    public void mess()
    {
        Intent i = new Intent(this, SMS.class);
        startActivity(i);
    }
}

//Timer.java
public class Timer extends CountDownTimer
{
    public Timer(long millisInFuture, long countDownInterval)
    {
        super(millisInFuture, countDownInterval);
    }

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

    public void onFinish()
    {
        TimerAct.timeDisplay.setText("Time over!!!");
        TimerAct ta = new TimerAct();
        ta.mess();
    }
}

//SMS.java
public class SMS extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        Intent sendIntent = new Intent(Intent.ACTION_VIEW);
        sendIntent.setType("vnd.android-dir/mms-sms");
        startActivity(sendIntent);

        String phoneNo = "9791192196";
        String message = "Hello";

        sendSMS(phoneNo, message);
    }

    //---sends an SMS message to another device---
    private void sendSMS(String phoneNumber, String message)
    {        
        PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, SMS.class), 0);
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, pi, null);        
    }
}
Nikunj
  • 113
  • 1
  • 2
  • 9

1 Answers1

1

In

public void onFinish()

Use something like:

        Intent fIntent = new Intent();
        fIntent.setClassName("com.pkgname", "com.pkgname.SMSActivity");
        startActivity(fIntent);    

Edit/Update:

You don't need to extend a new CountDowntimer in a new class, you can directly override its methods which are required here. You can have something like this as a member of class..

private CountDownTimer myTimer = new CountDownTimer(30000, 1000) {
//This will give you 30 sec timer with each tick at 1 second

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

    public void onFinish() {
        timeDisplay.setText("Time over!!!");
        Intent fIntent = new Intent();
        fIntent.setClassName("com.pkgname", "com.pkgname.SMSActivity");
        startActivityForResult(fIntent,0);    
    }
 };

with this in onCreate() :

    TextView timeDisplay = (TextView) findViewById(R.id.timer);
    myTimer.start();
Rahul garg
  • 9,202
  • 5
  • 34
  • 67
  • The Timer class cannot make use of Intent, so only i'm calling a method of current activity that handles the Intent part. – Nikunj Feb 05 '12 at 12:10
  • I have updated answer with a diff code implementation for this, or otherwise to use the Timer class of yours, you would have to use handlers, and send message to handler(in same activity class) to start new intent for new activity... – Rahul garg Feb 05 '12 at 15:06
  • still getting the same problem. after timer's 30secs completes, i get a message "force close" and the new activity is not initiated. – Nikunj Feb 05 '12 at 16:36