计算android中的当前时间

时间:2013-12-02 09:05:29

标签: android date notifications

假设我单击“开始”按钮并关闭应用程序,但“通知”正在运行。五分钟后我打开通知然后第二个活动打开并设置文本5分钟。再次关闭申请。然后我再次点击开始按钮。3分钟后检查通知它应该打开3分钟而不是8.但在我的应用程序中它显示8分钟

1:开始通知的主要活动

public class MainActivity extends Activity {
final Context context=this;
private EditText timerValue;
private static final int NOTIFY_ME_ID=1337;
Intent MyIntent;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    timerValue = (EditText) findViewById(R.id.timerValue111);

    startButton = (Button) findViewById(R.id.startButton);
    x=timerValue.getText().toString();
    nman=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    startButton.setOnClickListener(new View.OnClickListener() {


        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        public void onClick(View view) {

            Long s1=(long) 0.0;
            String timeStamp = new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime());
            //Toast.makeText(MainActivity.this,timeStamp,2000).show();

            Date interestingDate = new Date();
            s1= interestingDate.getTime();




            int seconds = (int) (s1/ 1000) % 60 ;
            int minutes = (int) ((s1 /(1000*60)) % 60);
            int hours   = (int) ((s1 /(1000*60*60)) % 24);


            Toast.makeText(MainActivity.this,"Time:"+hours+":"+minutes+":"+seconds,2000).show();

            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


            String MyText = "Reminder";
            Notification mNotification = new Notification(R.drawable.ic_launcher, MyText, System.currentTimeMillis() );

            //The three parameters are: 1. an icon, 2. a title, 3. time when the notification appears



            String MyNotificationTitle = "Medicine!";
            String MyNotificationText  = "Don't forget to take your medicine!";

            MyIntent = new Intent(MainActivity.this,SecondActivity.class);
            MyIntent.putExtra("s1", (long)s1);

            PendingIntent StartIntent = PendingIntent.getActivity(getApplicationContext(),0,MyIntent,0);
            // x=timerValue.getText().toString();



            //A PendingIntent will be fired when the notification is clicked. The FLAG_CANCEL_CURRENT flag cancels the pendingintent


            mNotification.setLatestEventInfo(getApplicationContext(), MyNotificationTitle, MyNotificationText, StartIntent);

            mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
            notificationManager.notify(0,   mNotification);



        }
    });


}



}

2:计算总时间的第二项活动

public class SecondActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    long p=(long) 0.0;
    long s=(long) 0.0;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    TextView text1=(TextView)findViewById(R.id.text1);

    Bundle extras = getIntent().getExtras(); 

    Date interestingDate = new Date();
     s= interestingDate.getTime();


    long x1=extras.getLong("s1");

     p=s-x1;

    int minutes = (int) ((p /(1000*60)) % 60);

    text1.setText(""+minutes);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.second, menu);
    return true;
}

}

1 个答案:

答案 0 :(得分:0)

这可能是因为您没有关闭应用程序,因此应用程序进入onPause状态,而您的第二个活动未调用onDestroy =>再次在第二个Activty中输入时,称为onResume方法,在此方法中不设置TextView文本