[英]How to display an activity after certain time period
我有2个活动(FirstActivity和SecondAcitivity)FirstActivity内容EditText和Button我想在用户单击按钮时销毁firstActivity,并在EditText中输入时间后启动SecondActivity
Use a CountDownTimer: 使用CountDownTimer:
String text = editText.getText().toString();
int time = Integer.parseInt(text);//in seconds
time = time * 1000;
new CountDownTimer(time, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
}
}.start();
put this in the onClick, so implement View.OnClickListener and add this code: 将其放在onClick中,因此实现View.OnClickListener并添加以下代码:
public void onClick(View view) {
if (view.getId() == R.id.button1) {
//countdown code
}
}
in onCreate 在onCreate中
Button myBtn = (Button)findViewById(R.id.YourBtnId);
final EditText myEditText = (EditText)findViewById(R.id.YourEditTextId);
myBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int time = Integer.parseInt(myEditText.getText().toString());
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent startActivity = new Intent(FirstActivity.this, SecondAcitivity.class);
startActivity(startActivity);
finish();
}
}, time * 1000);
}
});
Im guessing you also got a mainactivity or homeactivity? 我猜您还参加了一项主要活动或家庭活动? if so you could do the following to get the wanted result:
如果是这样,您可以执行以下操作以获得所需的结果:
in your firstactivity onCreate() method: 在您的firstactivity onCreate()方法中:
Button mButton = (Button)findViewById(R.id.TestButton);
final EditText mEditText = (EditText)findViewById(R.id.TestTextBox);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int time = Integer.parseInt(mEditText.getText().toString());
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("Time", time);
startActivity(intent);
}
});
than in your mainactivity's onCreate() method place the following: 比在您的mainactivity的onCreate()方法中放置以下内容:
if (getIntent().getIntExtra("Time", 0) > 0) {
int time = getIntent().getIntExtra("Time" , 0);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
finish();
}
}, time * 1000);
}
EDIT: (if you don't have an MainActivity) 编辑:(如果您没有MainActivity)
first: register receiver inside XML 第一:在XML中注册接收者
<receiver android:name=".AlarmReceiver"/>
Create AlarmReceiver class 创建AlarmReceiver类
package (your package)
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm time reached", Toast.LENGTH_SHORT).show();
Intent i = new Intent();
i.setClassName("(your package name)", "(your package name.SecondActivity)");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
In your FirstActivity's onCreate method: 在您的FirstActivity的onCreate方法中:
Button mButton = (Button)findViewById(R.id.TestButton);
final EditText mEditText = (EditText)findViewById(R.id.TestTextBox);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int time = Integer.parseInt(mEditText.getText().toString());
if(time > 0) {
Intent myIntent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, time);
alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(getApplicationContext(), "Starting Activity in: " + time + " seconds", Toast.LENGTH_SHORT).show();
finish();
}
}
});
Hope this helps you out ;) 希望这可以帮助你 ;)
PS: about passing the image between the activities you could try the following: First declare your bitmap at the top of your activity: PS:关于在活动之间传递图像,您可以尝试以下操作:首先在活动顶部声明位图:
private Intent myIntent;
add following code to the onCreate method in your FirstActivity 将以下代码添加到FirstActivity中的onCreate方法中
myIntent = new Intent(FirstActivity.this, AlarmReceiver.class);
and add below bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), bitmapOptions);
并在下面添加
bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), bitmapOptions);
the following: 下列:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
myIntent.putExtra("SendImage", byteArray);
and add below Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
并在下面添加
Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
ByteArrayOutputStream stream = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
myIntent.putExtra("SendImage", byteArray);
and then in your AlarmReceiver 然后在您的AlarmReceiver中
byte[] mByteArray = intent.getByteArrayExtra("SendImage") //above Intent i = new Intent();
i.putExtra("Image", mByteArray); //above context.startActivity(i);
and in your SecondActivity 并在您的SecondActivity中
byte[] mByteArray= getIntent().getByteArrayExtra("Image");
if(mByteArray != null){
Bitmap mBitmap = BitmapFactory.decodeByteArray(mByteArray, 0, mByteArray.length);
mTestImage.setImageBitmap(mBitmap); //Your imageview
}
Don't think this is the best solution, but I have tested this, and it worked ;) 不要以为这是最好的解决方案,但是我已经对此进行了测试,并且效果很好;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.