简体   繁体   中英

How to display an activity after certain time period

我有2个活动(FirstActivity和SecondAcitivity)FirstActivity内容EditText和Button我想在用户单击按钮时销毁firstActivity,并在EditText中输入时间后启动SecondActivity

Use a 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:

public void onClick(View view) {
    if (view.getId() == R.id.button1) {
        //countdown code
    }
}

in 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:

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:

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)

first: register receiver inside XML

<receiver android:name=".AlarmReceiver"/>

Create AlarmReceiver class

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:

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:

private Intent myIntent;

add following code to the onCreate method in your FirstActivity

myIntent = new Intent(FirstActivity.this, AlarmReceiver.class);

and add below 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));

ByteArrayOutputStream stream = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
myIntent.putExtra("SendImage", byteArray);

and then in your AlarmReceiver

byte[] mByteArray = intent.getByteArrayExtra("SendImage")  //above Intent i = new Intent();
i.putExtra("Image", mByteArray); //above context.startActivity(i);

and in your 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 ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM