android - 如何从 CountDownTimer 调用 Activity ?

标签 android android-intent

我当前的 Activity 类“TimerAct.java”使用了一个 30 秒的计时器。计时器结束后,必须启动新 Activity “SMS.java”。为此,在 onFinish() 中,我调用了当前 Activity 的一个方法来启动新 Activity 。但是我收到一条“强制关闭”的消息。谁能帮帮我?

代码如下:

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

最佳答案

public void onFinish()

使用类似的东西:

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

编辑/更新:

您不需要在新类中扩展新的 CountDowntimer,您可以直接覆盖这里需要的它的方法。 作为类(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);    
    }
 };

在 onCreate() 中使用这个:

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

关于android - 如何从 CountDownTimer 调用 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9147609/

相关文章:

java - Android 按钮 onClickListener

android - Webview 上的 Telegram URL(在 Telegram 应用程序链接上共享)

android - 在 Android Keychain 上检索已安装的证书

android - hdpi、ldpi 还是 mdpi?

java - libGDX - 对不同的屏幕使用不同的 InputProcessors?

java - 有没有办法在Azure MobileServiceSyncTable中进行计数查询?

android - java.lang.RuntimeException : Parcel android. os.Parcel:解码未知类型代码

android - 从 fragment 退出到包含 Viewpager 的 Activity 时,共享元素转换不起作用

Android:打开选择音频的对话框

javascript - Android Chrome 和 Intent 检测