【问题标题】:How to call an activity from a CountDownTimer?如何从 CountDownTimer 调用活动?
【发布时间】:2012-02-05 07:15:03
【问题描述】:

我当前的活动类“TimerAct.java”使用 30 秒的计时器。计时器完成后,必须启动新活动“SMS.java”。为此,在 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);        
    }
}

【问题讨论】:

    标签: android android-intent


    【解决方案1】:

    public void onFinish()
    

    使用类似的东西:

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

    编辑/更新:

    你不需要在一个新的类中扩展一个新的 CountDowntimer,你可以直接覆盖它这里需要的方法。 作为班级成员,您可以拥有这样的东西..

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

    【讨论】:

    • Timer 类不能使用 Intent,所以只有我在调用当前活动的方法来处理 Intent 部分。
    • 我已经用差异代码实现更新了答案,或者使用你的 Timer 类,你必须使用处理程序,并将消息发送到处理程序(在同一个活动类中)开始新活动的新意图...
    • 仍然遇到同样的问题。计时器的 30 秒完成后,我收到一条消息“强制关闭”并且新活动未启动。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    相关资源
    最近更新 更多