如何调用从CountDownTimer活动? [英] How to call an activity from a CountDownTimer?
问题描述
我现在的活动类TimerAct.java利用30secs的计时器。在定时器完成一个新的活动SMS.java必须启动。为此,在onFinish()我叫启动新的活动我的当前活动的方法。但我得到一个味精强制关闭。谁能帮我?
这里的code:
// TimerAct.java
公共类TimerAct扩展活动
{
静态的TextView timeDisplay;
定时器T;
INT长度= 30000; / **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.time); timeDisplay =(的TextView)findViewById(R.id.timer);
timeDisplay.setText(剩余时间的长度+ / 1000);
T =新的Timer(长,1000);
t.start();
} 公共无效乱()
{
意图I =新意图(这一点,SMS.class);
startActivity(ⅰ);
}
}//Timer.java
公共类定时器扩展CountDownTimer
{
公共定时器(长millisInFuture,长countDownInterval)
{
超(millisInFuture,countDownInterval);
} 公共无效onTick(长millisUntilFinished)
{
TimerAct.timeDisplay.setText(剩余时间+ millisUntilFinished / 1000);
} 公共无效onFinish()
{
TimerAct.timeDisplay.setText(时间超过!!!);
TimerAct TA =新TimerAct();
ta.mess();
}
}//SMS.java
公共类短信扩展活动
{
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState); 意图sendIntent =新意图(Intent.ACTION_VIEW);
sendIntent.setType(vnd.android-DIR / MMS短信);
startActivity(sendIntent); 字符串PHONENO =9791192196;
字符串消息=你好; sendSMS(PHONENO,消息);
} // ---发送SMS消息到另一装置---
私人无效sendSMS(字符串phoneNumber的,字符串消息)
{
的PendingIntent圆周率= PendingIntent.getActivity(在此,0,新意图(此,SMS.class),0);
SmsManager短信= SmsManager.getDefault();
sms.sendTextMessage(phoneNumber的,空,消息,PI,NULL);
}
}
在
公共无效onFinish()
使用类似:
意图fIntent =新意图();
fIntent.setClassName(com.pkgname,com.pkgname.SMSActivity);
startActivity(fIntent);
修改/更新:
您不需要在一个新的类扩展新的CountDowntimer,可以直接忽略它这是在这里所需的方法。
你可以有这样的事情作为类的成员。
私人CountDownTimer myTimer =新CountDownTimer(30000,1000){
//这会给你30秒计时器滴答各以1秒 公共无效onTick(长millisUntilFinished){
timeDisplay.setText(剩余时间的长度+ / 1000);
} 公共无效onFinish(){
timeDisplay.setText(时间超过!!!);
意图fIntent =新的Intent();
fIntent.setClassName(com.pkgname,com.pkgname.SMSActivity);
startActivityForResult(fIntent,0);
}
};
考虑到这的onCreate():
TextView的timeDisplay =(的TextView)findViewById(R.id.timer);
myTimer.start();
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);
}
}
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();
这篇关于如何调用从CountDownTimer活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!