我需要将字符串发送到另一个活动。
long timeSpent = System.currentTimeMillis() - initialTime;
timeSpent = (long) (timeSpent / 1000.0);
String string = "Game Over! Time spent " + String.valueOf(timeSpent) + "s";
我使用此代码,但这是错误的。
Intent intent = new Intent();
intent.putExtra("timespeent", timeSpent);
答案 0 :(得分:0)
您需要像这样更改代码
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.IOException;
public class MediaActivity extends AppCompatActivity implements SurfaceHolder.Callback {
static SurfaceView videoSurface;
static SurfaceHolder videoHolder;
static MediaPlayer mediaPlayer;
private ImageView imgPlay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_media);
videoSurface = (SurfaceView) findViewById(R.id.surfView);
videoHolder = videoSurface.getHolder();
videoHolder.addCallback(this);
imgPlay = (ImageView) findViewById(R.id.imgPLay);
final String link = "Your Link here";
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(link);
} catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
new Thread(new Runnable() {
@Override
public void run() {
imgPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
try {
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Log.i("Completion Listener", "Song Complete");
mp.stop();
mp.reset();
try {
mp.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
}
});
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}).start();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}