2012-02-05 11 views
1

私の現在のアクティビティクラス "TimerAct.java"は、30秒のタイマーを使用します。タイマーの完了時に、新しい活動 "SMS.java"が開始されなければならない。このために、onFinish()で私は新しいアクティビティを開始する現在のアクティビティのメソッドを呼び出します。しかし、私はmsgを "強制終了"にします。誰か助けてくれますか?CountDownTimerからアクティビティを呼び出す方法は?

ここでは、コードです:

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

編集/更新:

あなたドン

public void onFinish() 

使用何かのように

//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);   
    } 
} 

答えて

1

は、新しいクラスで新しいCountDowntimerを拡張する必要がある場合は、ここで必要なメソッドを直接オーバーライドできます。 あなたは)(のonCreateで..クラスの一員としてこれに

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);  
    } 
}; 

をこのような何かを持つことができます。

TextView timeDisplay = (TextView) findViewById(R.id.timer); 
    myTimer.start(); 
+0

Timerクラスは、意図の利用をすることはできませんので、唯一私が呼んでいますインテント部分を処理する現在のアクティビティのメソッド。 – Nikunj

+0

私はこのための差分コードの実装で答えを更新しました。そうでない場合は、あなたのTimerクラスを使用し、ハンドラを使用してハンドラ(同じアクティビティクラス内の)にメッセージを送信して新しいアクティビティを開始します。 –

+0

はまだ同じ問題を抱えています。タイマーの30秒後に "強制終了"というメッセージが表示され、新しいアクティビティは開始されません。 – Nikunj

関連する問題