我是Android应用程序开发的新手,对不起,如果我犯了任何错误。每当我按照我的代码访问homeActivity时,如何开始计时器倒计时。我感谢所有试图帮助我的人。谢谢。
public class homeActivity extends AppCompatActivity {
private TextView userEmail; // Declaring email textview
private TextView timerDisplay; // Declaring Timer Display textview
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
userEmail = (TextView) findViewById(R.id.userEmailID);
timerDisplay = (TextView) findViewById(R.id.timer);
// Retriving the the email id of the user which was passed from the sigin Activity
userEmail.setText(getIntent().getExtras().getString("Email"));
//use of a timer to display the notification 40 seconds as default for testing purposes
new CountDownTimer(40000, 1000) {
public void onTick(long millisUntilFinished) {
// Displaying the following timer on the textview
timerDisplay.setText("seconds remaining: " + millisUntilFinished / 1000);
}
//once the timer stops the following notification message is being Displayed
public void onFinish() {
final String title1="Break!";
String subject1="It is time to take a break";
String body1="20 minutes have passed";
// Break taken is displayed on the textview
timerDisplay.setText("Break Taken");
// notification is diplayed
NotificationManager notif=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify=new Notification.Builder
(getApplicationContext()).setContentTitle(title1).setContentText(body1).
setContentTitle(subject1).setSmallIcon(R.mipmap.iclauncher).build();
notify.flags |= Notification.FLAG_AUTO_CANCEL;
notif.notify(0, notify);
}
}.start();
}
// Directed to rate us Activity
public void rateusOperation (View v){
Intent intent = new Intent(homeActivity.this,rateUsActivity.class);
startActivity(intent);
}
// Directed to daily offers Activity
public void dailyOffersOperation (View v){
Intent intent = new Intent(homeActivity.this,dailyOffersActivity.class);
startActivity(intent);
}
// directed to statistics Activity
public void statisticsOperation (View v){
Intent intent = new Intent(homeActivity.this,statisticsActivity.class);
startActivity(intent);
}
// directed to privacy policy Activity
public void privacyPolicyOperation (View v){
Intent intent = new Intent(homeActivity.this,privacyPolicyActivity.class);
startActivity(intent);
}
// Directed back to sign in Activity
public void logoutOperation (View v){
AlertDialog.Builder altDial = new AlertDialog.Builder (homeActivity.this);
altDial.setMessage("Are You Sure ? ").setCancelable(true)
.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(homeActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = altDial.create();
alert.setTitle("You want to Logout ");
alert.show();
}
}
答案 0 :(得分:1)
public class homeActivity extends AppCompatActivity
{
private TextView userEmail; // Declaring email textview
private TextView timerDisplay; // Declaring Timer Display textview
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
userEmail = (TextView) findViewById(R.id.userEmailID);
timerDisplay = (TextView) findViewById(R.id.timer);
// Retriving the the email id of the user which was passed from the sigin Activity
userEmail.setText(getIntent().getExtras().getString("Email"));
final CounterClass timer2 = new CounterClass(4000, 1000);
timer2.start();
}
// Directed to rate us Activity
public void rateusOperation (View v){
Intent intent = new Intent(homeActivity.this,rateUsActivity.class);
startActivity(intent);
}
// Directed to daily offers Activity
public void dailyOffersOperation (View v){
Intent intent = new Intent(homeActivity.this,dailyOffersActivity.class);
startActivity(intent);
}
// directed to statistics Activity
public void statisticsOperation (View v){
Intent intent = new Intent(homeActivity.this,statisticsActivity.class);
startActivity(intent);
}
// directed to privacy policy Activity
public void privacyPolicyOperation (View v){
Intent intent = new Intent(homeActivity.this,privacyPolicyActivity.class);
startActivity(intent);
}
// Directed back to sign in Activity
public void logoutOperation (View v){
AlertDialog.Builder altDial = new AlertDialog.Builder (homeActivity.this);
altDial.setMessage("Are You Sure ? ").setCancelable(true)
.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(homeActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = altDial.create();
alert.setTitle("You want to Logout ");
alert.show();
}
public class CounterClass extends CountDownTimer
{
TextView conter;
public CounterClass(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish()
{
final String title1="Break!";
String subject1="It is time to take a break";
String body1="20 minutes have passed";
// Break taken is displayed on the textview
timerDisplay.setText("Break Taken");
// notification is diplayed
NotificationManager notif=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify=new Notification.Builder
(getApplicationContext()).setContentTitle(title1).setContentText(body1).
setContentTitle(subject1).setSmallIcon(R.mipmap.iclauncher).build();
notify.flags |= Notification.FLAG_AUTO_CANCEL;
notif.notify(0, notify);
}
@SuppressLint("NewApi")
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
public void onTick(long millisUntilFinished)
{
long millis = millisUntilFinished;
String hms = String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
timerDisplay.setText(hms);
}
}
}