2013-11-21 6 views
-1

두 번째 활동을 매 초마다 실행하고 첫 번째 활동으로 업데이트를 보내려고합니다. 내 화면이 계속 깜박 거리지 않게하려면 다른 활동도 시작할 수 있습니다. 앱의 백그라운드 활동과 같은 두 번째 활동을 만드는 것과 같습니다. 나는이 프로그래밍을 처음 사용하기 때문에 그 방법을 사용하는 방법을 알려줄 수 있기를 바랍니다. 감사.매 초마다 활동 실행

첫 번째 활동

package com.example.suntracking; 

import java.util.Calendar; 
import java.util.Timer; 
import java.util.TimerTask; 

import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.SharedPreferences.Editor; 
import android.os.Bundle; 
import android.os.Handler; 
import android.preference.PreferenceManager; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainActivity extends Activity implements View.OnClickListener{ 

    TextView aziangle,elevation,numberof,rightasc,decli,hourangle,solartime,showinlat,showinlong, 
    showintimezone,showindlst,showinofftime,showinoffazi,showinoffele,showinoffphi,showinofflamda,showinoffxi, 
    showdate,showtime,showday; 
    Button editting; 
    Float latval,longval,offtimeval,offazival,offeleval,offphival,offlamdaval,offxival; 
    int timeval,taketimesec,taketimeminute,taketimehour,takedateday,takedatemonth,takedateyear; 
    String show1,show2,show3,show4,sshowday,takeampmtext; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     initialize(); 
     loadSavedpreferences(); 
    } 


    private void initialize() { 
     // TODO Auto-generated method stub 
     aziangle=(TextView)findViewById(R.id.tvAziAngle); 
     elevation=(TextView)findViewById(R.id.tvElevation); 
     numberof=(TextView)findViewById(R.id.tvNumberof); 
     rightasc=(TextView)findViewById(R.id.tvRightAsc); 
     decli=(TextView)findViewById(R.id.tvDecli); 
     hourangle=(TextView)findViewById(R.id.tvHourAngle); 
     solartime=(TextView)findViewById(R.id.tvSolarTime); 
     showday=(TextView)findViewById(R.id.tvDay); 
     showdate=(TextView)findViewById(R.id.tvDate); 
     showtime=(TextView)findViewById(R.id.tvTime); 
     showinlat=(TextView)findViewById(R.id.tvShowInLat); 
     showinlong=(TextView)findViewById(R.id.tvShowInLong); 
     showintimezone=(TextView)findViewById(R.id.tvShowInTimeZone); 
     showindlst=(TextView)findViewById(R.id.tvShowInDLST); 
     showinofftime=(TextView)findViewById(R.id.tvShowInOffTime); 
     showinoffazi=(TextView)findViewById(R.id.tvShowInOffAzimuth); 
     showinoffele=(TextView)findViewById(R.id.tvShowInOffElevation); 
     showinoffphi=(TextView)findViewById(R.id.tvShowInOffphi); 
     showinofflamda=(TextView)findViewById(R.id.tvShowInOfflamda); 
     showinoffxi=(TextView)findViewById(R.id.tvShowInOffxi); 
     editting=(Button)findViewById(R.id.bedit); 
     editting.setOnClickListener(this); 
     } 


    private void loadSavedpreferences() { 
     // TODO Auto-generated method stub 
     SharedPreferences savedata= getSharedPreferences("savealldata",0); 

     sshowday=savedata.getString("daytext",null); 
     showday.setText(sshowday); 

     takedateday=savedata.getInt("dateday",00); 
     takedatemonth=savedata.getInt("datemonth", 00); 
     takedateyear=savedata.getInt("dateyear", 00); 
     showdate.setText(takedateday + "/" + takedatemonth + "/" + takedateyear); 

     taketimesec=savedata.getInt("timesec", 00); 
     taketimeminute=savedata.getInt("timeminute", 00); 
     taketimehour=savedata.getInt("timehour", 00); 
     takeampmtext=savedata.getString("ampmtext", "AM"); 
     showtime.setText(taketimehour + ":" + taketimeminute + ":" + taketimesec + takeampmtext); 

     show1=savedata.getString("show1",null); 
     latval=savedata.getFloat("latval", 0); 
     showinlat.setText("Latitude : " + latval + ", " + show1); 

     show2=savedata.getString("show2",null); 
     longval=savedata.getFloat("longval",0); 
     showinlong.setText("Longitude : " + longval + ", " + show2); 

     show3=savedata.getString("show3",null); 
     timeval=savedata.getInt("timezoneval",0); 
     showintimezone.setText("Time Zone : " + timeval + show3); 

     show4=savedata.getString("show4","No"); 
     showindlst.setText("Daylight Saving Time : "+ show4); 

     offtimeval=savedata.getFloat("show5",0); 
     showinofftime.setText("Offset Time : " + offtimeval); 

     offazival=savedata.getFloat("show6",0); 
     showinoffazi.setText("Offset Azimuth : "+ offazival); 

     offeleval=savedata.getFloat("show7",0); 
     showinoffele.setText("Offset Elevation : " + offeleval); 

     offphival=savedata.getFloat("show8", 0); 
     showinoffphi.setText("Offset Φ :" + offphival); 

     offlamdaval=savedata.getFloat("show9", 0); 
     showinofflamda.setText("Offset λ : "+offlamdaval); 

     offxival=savedata.getFloat("show10", 0); 
     showinoffxi.setText("Offset ξ : "+ offxival); 

    } 





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

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     Intent e=new Intent(MainActivity.this, Parameterlist.class); 
     startActivity(e); 
    } 



    /*private void savePreferences(String string, String value) { 
     // TODO Auto-generated method stub 
      SharedPreferences savedata = getApplicationContext().getSharedPreferences(
        "savealldata", MODE_PRIVATE); 
     Editor editor=savedata.edit(); 
     editor.putString("show1", show1); 
     editor.commit(); 
    }*/ 




    /*@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // TODO Auto-generated method stub 
     super.onActivityResult(requestCode, resultCode, data); 
     if(resultCode==RESULT_OK){ 
     loadSavedpreferences(); 
     //Bundle getanswer=data.getExtras(); 
     //show1=getanswer.getString("showloc1"); 
     //latval=getanswer.getFloat("anslatitudevalue"); 
     //showinlat.setText("Latitude is : " + latval + ", " + show1); 
     //show2=getanswer.getString("Floatiloc"); 
     //Floatval=getanswer.getFloat("ansFloatitudevalue"); 
     //showinFloat.setText("Latitude is : " + Floatval + ", " + show2); 
     } 
    }*/ 



} 

두 번째 활동

package com.example.suntracking; 

import java.util.Calendar; 

import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.SharedPreferences.Editor; 
import android.os.Bundle; 
import android.os.Handler; 

public class TimeClock extends Activity{ 

    int timesec,timeminute,timehour,dateday,datemonth,dateyear,day,ampm; 
    String daytext,ampmtext; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     //do anything 
     Calendar c = Calendar.getInstance(); 
     timesec = c.get(Calendar.SECOND); 
     timeminute = c.get(Calendar.MINUTE); 
     timehour = c.get(Calendar.HOUR); 
     ampm = c.get(Calendar.AM_PM); 
     if(ampm==0){ 
      ampmtext="AM"; 
     }else { 
      ampmtext="PM"; 
     } 
     dateday = c.get(Calendar.DAY_OF_MONTH); 
     datemonth = c.get(Calendar.MONTH); 
     datemonth += 1; 
     dateyear = c.get(Calendar.YEAR); 
     day = c.get(Calendar.DAY_OF_WEEK); 
     switch(day){ 
     case 1: 
      daytext="SUNDAY"; 
      break; 
     case 2: 
      daytext="MONDAY"; 
      break; 
     case 3: 
      daytext="TUESDAY"; 
      break; 
     case 4: 
      daytext="WEDNESDAY"; 
      break; 
     case 5: 
      daytext="THURSDAY"; 
      break; 
     case 6: 
      daytext="FRIDAY"; 
      break; 
     case 7: 
      daytext="SATURDAY"; 
      break; 
     } 
     savePreferences(); 
     Intent i= new Intent(TimeClock.this,MainActivity.class); 
     startActivity(i); 
     finish(); 

    } 

    private void savePreferences() { 
     // TODO Auto-generated method stub 
     SharedPreferences savedata = getSharedPreferences("savealldata",0); 
     Editor editor=savedata.edit(); 
     editor.putInt("timesec",timesec); 
     editor.putInt("timeminute", timeminute); 
     editor.putInt("timehour", timehour); 
     editor.putInt("dateday", dateday); 
     editor.putInt("datemonth", datemonth); 
     editor.putInt("dateyear", dateyear); 
     editor.putInt("ampm", ampm); 
     editor.putString("daytext", daytext); 
     editor.putString("ampmtext", ampmtext); 
     editor.commit(); 
    } 

} 

편집 : 최신

package com.example.suntracking; 

import java.util.Calendar; 

import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.SharedPreferences.Editor; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.KeyEvent; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainActivity extends Activity implements View.OnClickListener{ 
    //showing input and output 
    TextView aziangle,elevation,numberof,rightasc,decli,hourangle,solartime,showinlat,showinlong, 
    showintimezone,showindlst,showinofftime,showinoffazi,showinoffele,showinoffphi,showinofflamda,showinoffxi, 
    showdate,showtime,showday; 

    Button editting; 
    Float latval,longval,offtimeval,offazival,offeleval,offphival,offlamdaval,offxival; 
    int timeval,taketimesec,taketimeminute,taketimehour,takedateday,takedatemonth,takedateyear; 
    String show1,show2,show3,show4,sshowday,takeampmtext; 
    //for clock 
    int timesec,timeminute,timehour,dateday,datemonth,dateyear,day,ampm; 
    String daytext,ampmtext,showazi,showele,shownoday,showra,showdecli,showh_angle,shows_time; 
    Handler mHandler = new Handler(); 
    Runnable r; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     initialize(); 
     loadSavedpreferences(); 
     r = new Thread(new Runnable() { 
      @Override 
      public void run() { 
       // TODO Auto-generated method stub 
       while (true) { 
        try { 
         Thread.sleep(1000); 
         mHandler.post(new Runnable() { 

          @Override 
          public void run() { 
           // TODO Auto-generated method stub 
           // Write your code here to update the UI. 
           Save_fetchPref(); 
           run_cal(); 

          } 

          private void run_cal() { 
           // TODO Auto-generated method stub 
           Intent j=new Intent(MainActivity.this,Calculation.class); 
           startActivity(j); 
          } 

          private void Save_fetchPref() { 
           // TODO Auto-generated method stub 
            Calendar c = Calendar.getInstance(); 
              timesec = c.get(Calendar.SECOND); 
              timeminute = c.get(Calendar.MINUTE); 
              timehour = c.get(Calendar.HOUR_OF_DAY); 
              ampm = c.get(Calendar.AM_PM); 
              if(ampm==0){ 
               ampmtext="AM"; 
              }else { 
               ampmtext="PM"; 
              } 
              dateday = c.get(Calendar.DAY_OF_MONTH); 
              datemonth = c.get(Calendar.MONTH); 
              datemonth += 1; 
              dateyear = c.get(Calendar.YEAR); 
              day = c.get(Calendar.DAY_OF_WEEK); 
              switch(day){ 
              case 1: 
               daytext="SUNDAY"; 
               break; 
              case 2: 
               daytext="MONDAY"; 
               break; 
              case 3: 
               daytext="TUESDAY"; 
               break; 
              case 4: 
               daytext="WEDNESDAY"; 
               break; 
              case 5: 
               daytext="THURSDAY"; 
               break; 
              case 6: 
               daytext="FRIDAY"; 
               break; 
              case 7: 
               daytext="SATURDAY"; 
               break; 
              } 
              savePreferences(); 
              loadSavedpreferences(); 
          } 
         }); 
        } catch (Exception e) { 
         // TODO: handle exception 
        } 
       } 
      } 
     }).start(); 


     } 


    private void savePreferences() { 
     // TODO Auto-generated method stub 
     SharedPreferences savedata = getSharedPreferences("savealldata",0); 
     Editor editor=savedata.edit(); 
     editor.putInt("timesec",timesec); 
     editor.putInt("timeminute", timeminute); 
     editor.putInt("timehour", timehour); 
     editor.putInt("dateday", dateday); 
     editor.putInt("datemonth", datemonth); 
     editor.putInt("dateyear", dateyear); 
     editor.putInt("ampm", ampm); 
     editor.putString("daytext", daytext); 
     editor.putString("ampmtext", ampmtext); 
     editor.commit(); 
    } 


    private void initialize() { 
     // TODO Auto-generated method stub 
     aziangle=(TextView)findViewById(R.id.tvAziAngle); 
     elevation=(TextView)findViewById(R.id.tvElevation); 
     numberof=(TextView)findViewById(R.id.tvNumberof); 
     rightasc=(TextView)findViewById(R.id.tvRightAsc); 
     decli=(TextView)findViewById(R.id.tvDecli); 
     hourangle=(TextView)findViewById(R.id.tvHourAngle); 
     solartime=(TextView)findViewById(R.id.tvSolarTime); 
     showday=(TextView)findViewById(R.id.tvDay); 
     showdate=(TextView)findViewById(R.id.tvDate); 
     showtime=(TextView)findViewById(R.id.tvTime); 
     showinlat=(TextView)findViewById(R.id.tvShowInLat); 
     showinlong=(TextView)findViewById(R.id.tvShowInLong); 
     showintimezone=(TextView)findViewById(R.id.tvShowInTimeZone); 
     showindlst=(TextView)findViewById(R.id.tvShowInDLST); 
     showinofftime=(TextView)findViewById(R.id.tvShowInOffTime); 
     showinoffazi=(TextView)findViewById(R.id.tvShowInOffAzimuth); 
     showinoffele=(TextView)findViewById(R.id.tvShowInOffElevation); 
     showinoffphi=(TextView)findViewById(R.id.tvShowInOffphi); 
     showinofflamda=(TextView)findViewById(R.id.tvShowInOfflamda); 
     showinoffxi=(TextView)findViewById(R.id.tvShowInOffxi); 
     editting=(Button)findViewById(R.id.bedit); 
     editting.setOnClickListener(this); 
     } 


    private void loadSavedpreferences() { 
     // TODO Auto-generated method stub 
     SharedPreferences savedata= getSharedPreferences("savealldata",0); 
     //input 
     sshowday=savedata.getString("daytext",null); 
     showday.setText(sshowday); 

     takedateday=savedata.getInt("dateday",00); 
     takedatemonth=savedata.getInt("datemonth", 00); 
     takedateyear=savedata.getInt("dateyear", 00); 
     showdate.setText(takedateday + "/" + takedatemonth + "/" + takedateyear); 

     taketimesec=savedata.getInt("timesec", 00); 
     taketimeminute=savedata.getInt("timeminute", 00); 
     taketimehour=savedata.getInt("timehour", 00); 
     takeampmtext=savedata.getString("ampmtext", "AM"); 
     showtime.setText(taketimehour + ":" + taketimeminute + ":" + taketimesec + takeampmtext); 

     show1=savedata.getString("show1",null); 
     latval=savedata.getFloat("latval", 0); 
     showinlat.setText("Latitude : " + latval + ", " + show1); 

     show2=savedata.getString("show2",null); 
     longval=savedata.getFloat("longval",0); 
     showinlong.setText("Longitude : " + longval + ", " + show2); 

     show3=savedata.getString("show3",null); 
     timeval=savedata.getInt("timezoneval",0); 
     showintimezone.setText("Time Zone : " + timeval + show3); 

     show4=savedata.getString("show4","No"); 
     showindlst.setText("Daylight Saving Time : "+ show4); 

     offtimeval=savedata.getFloat("show5",0); 
     showinofftime.setText("Offset Time : " + offtimeval); 

     offazival=savedata.getFloat("show6",0); 
     showinoffazi.setText("Offset Azimuth : "+ offazival); 

     offeleval=savedata.getFloat("show7",0); 
     showinoffele.setText("Offset Elevation : " + offeleval); 

     offphival=savedata.getFloat("show8", 0); 
     showinoffphi.setText("Offset Φ :" + offphival); 

     offlamdaval=savedata.getFloat("show9", 0); 
     showinofflamda.setText("Offset λ : "+offlamdaval); 

     offxival=savedata.getFloat("show10", 0); 
     showinoffxi.setText("Offset ξ : "+ offxival); 
     //output 
     showazi=savedata.getString("azimuth", null); 
     aziangle.setText("Azimuth Angle : " + showazi); 

     showele=savedata.getString("elevation", null); 
     elevation.setText("Elevation Angle" + showele); 

     shownoday=savedata.getString("jd", null); 
     numberof.setText("Number of Days : " + shownoday); 

     showra=savedata.getString("RA", null); 
     rightasc.setText("Right Ascension : " + showra); 

     showdecli=savedata.getString("declination", null); 
     decli.setText("Declination Angle : " + showdecli); 

     showh_angle=savedata.getString("h_angle", null); 
     hourangle.setText("Hour Angle : " + showh_angle); 

     shows_time=savedata.getString("s_time", null); 
     solartime.setText("Solar Time : " + shows_time); 
    } 

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

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     mHandler.removeCallbacks(run); 
     Intent e=new Intent(MainActivity.this, Parameterlist.class); 
     startActivity(e); 
    } 
    @Override 
    protected void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
     mHandler.removeCallbacks(run); 
    } 


} 
+0

왜 모든 코드를 하나의 활동에 포함시키지 않습니까? –

+1

UI 스레드에서 스레드, 서비스 및 업데이트 UI를 사용할 수 있습니다. –

+0

나는 그것이 매우 지저분 할 것이라고 생각했기 때문에 그것이 왜 그렇게하지 못했습니다. 그렇게하는 것이 더 낫습니까? – user3008754

답변

0
당신은 1 초에서 기본 설정을 업데이트하기 위해 ManActivity에서 핸들러를 사용할 수 있습니다

(1000 NS 코딩) :

Handler mHandler = new Handler(); 

(3210)는 이제 하나 개의 스레드를 생성하고 주기적으로 스레드의 수면 방법을 사용하여 작업을 수행하는 루프 동안 사용합니다.

Runnable run = new Thread(new Runnable() { 
      @Override 
      public void run() { 
       // TODO Auto-generated method stub 
       while (true) { 
        try { 
         Thread.sleep(1000); 
         mHandler.post(new Runnable() { 

          @Override 
          public void run() { 
           // TODO Auto-generated method stub 
           // Write your code here to update the UI. 
           Save_fetchPref(); 
          } 
         }); 
        } catch (Exception e) { 
         // TODO: handle exception 
        } 
       } 
      } 
     }).start(); 
    public void Save_fetchPref(){ 
    Calendar c = Calendar.getInstance(); 
      timesec = c.get(Calendar.SECOND); 
      timeminute = c.get(Calendar.MINUTE); 
      timehour = c.get(Calendar.HOUR); 
      ampm = c.get(Calendar.AM_PM); 
      if(ampm==0){ 
       ampmtext="AM"; 
      }else { 
       ampmtext="PM"; 
      } 
      dateday = c.get(Calendar.DAY_OF_MONTH); 
      datemonth = c.get(Calendar.MONTH); 
      datemonth += 1; 
      dateyear = c.get(Calendar.YEAR); 
      day = c.get(Calendar.DAY_OF_WEEK); 
      switch(day){ 
      case 1: 
       daytext="SUNDAY"; 
       break; 
      case 2: 
       daytext="MONDAY"; 
       break; 
      case 3: 
       daytext="TUESDAY"; 
       break; 
      case 4: 
       daytext="WEDNESDAY"; 
       break; 
      case 5: 
       daytext="THURSDAY"; 
       break; 
      case 6: 
       daytext="FRIDAY"; 
       break; 
      case 7: 
       daytext="SATURDAY"; 
       break; 
      } 
      savePreferences(); 
      loadSavedpreferences(); 
    } 

핸들러를 중지하려면 다음

onDestry() 
{ 
mHandler.removeCallbacks(run); 
} 

또는

당신은 시작에 참으로 실행 한 부울 수 있습니다. & 핸들러 같이 넣어 :

if(running) 
{ 
Save_fetchPref(); 
} 

가 onDestry 거짓 =가 실행하게().

+0

안녕하세요, 예를 들어 주셔서 감사합니다! 그것은 작동하지만, 지금은 계속 실행하고 나는 이미 응용 프로그램을 종료해도 깜박입니다. 신청서를 남겨두면 어떻게 종료되는지 확인하는 방법은 무엇입니까? – user3008754

+0

수정 사항을 확인하십시오. –

+0

내가 첫 번째를 사용하고 난들의 OnDestroy에 넣어하지만 실행이 변수에 해결 될 수없는 "과"실행 "에 대한 오류와 함께 나온다. – user3008754