不想在每次重新打开时重新启动应用程序

时间:2012-11-08 05:19:32

标签: android gps

我是android新手。我开发了一个应用程序,我计算经度和我当前位置的纬度和更新日期&使用处理程序为特定时间间隔的时间&它通过从此应用程序获取数据来映射Google地图上的位置,此应用程序可以准确地处理更新,但有一个问题是当移动电话被锁定时此应用程序无法正常工作。

  1. 我想在后台运行此应用程序&它继续运行直到&除非移动电话关闭。 当我从该应用程序返回后重新打开应用程序时,此应用程序也开始提供更新。
  2. 我想显示那个日期&上次更新的时间。不重启应用程序&不更新日期&从当前开放时间开始的时间它在背景和工作中不断工作显示在后台运行的活动。
  3. 请帮忙...... 我会非常感激

    我的主要活动代码在这里......

    package com.ETrack;
    
    public class ETrackActivity extends Activity {
    
    public void acquire()
    
        {           final String TAG = null;
                    PowerManager pm = (PowerManager)getApplicationContext().getSystemService(
                    getApplicationContext().POWER_SERVICE);
                    WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
                    wl.acquire();
                    wl.release();
        }
    
    
        private static final List<? extends NameValuePair> nameValuePairs = null;
    
    
        /** Called when the activity is first created. */
         static TelephonyManager tm;
    
    
         TextView GPS_Long_txtbx;
         TextView GPS_Lat_txtbx;
         static TextView last_update_time_txtbx;
         TextView onfig_txtbx;
         static String pswd_auth= "13579",  server_host="helpdesk.cispl.com";
         static String interval = "120";
         String Saveinterval = "interval.txt";
         String FILENAME3 = "savetime";
         String Savetime = "savetime.txt";
    
        Handler h = new Handler();
        Runnable run = new Runnable()
    
        {
    
            @Override
            public void run() {
    
                MyLocation loc = new MyLocation(getBaseContext());
                TextView longitude_view = (TextView) findViewById(R.id.GPS_Long_txta);
                       ((TextView) longitude_view)
                        .setText(" " + String.valueOf(loc.gps_lon));
                TextView latitude_view = (TextView) findViewById(R.id.GPS_Lat_txta);
                ((TextView) latitude_view).setText("  " + String.valueOf(loc.gps_lat));
                long delaytime =(record * 1000);
                last_update_time_txtbx = (TextView) findViewById(R.id.last_update_time_txt);
                last_update_time_txtbx.setText("  " + String.valueOf(java.text.DateFormat.getDateTimeInstance()
                .format(Calendar.getInstance().getTime())));
                tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                String response = CallWebService(tm.getDeviceId(), loc.gps_lon, loc.gps_lat);
                h.postDelayed(this, delaytime);
    
            }
        };
    
    
    
        public void Saveinterval(String Saveinterval) {
            try {
                File dir = getFilesDir();
                File file = new File(dir, Saveinterval);
                if (file.exists())
                    file.delete();
                FileOutputStream fos = openFileOutput(Saveinterval,
                        Context.MODE_PRIVATE);
                fos.write(Saveinterval.getBytes());
                fos.close();
            } catch (Exception ex) {
                ex.getMessage();
            }
        }
    
        public int RestoreRecord0() 
        {
            try
            {
    
                File dir = getFilesDir();
                File file = new File(dir, Saveinterval);
                if (!file.exists()) {
                    return Integer.parseInt(interval);
                }
    
                FileInputStream fileIS = new FileInputStream(file);
                BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
                String readString = new String();
                String data = " ";
    
                // just reading each line and pass it on the debugger
    
                while ((readString = buf.readLine()) != null) 
                {
                    data += readString;
                }
    
                return Integer.parseInt(data);
            }
    
            catch (Exception ex) {
                ex.getMessage();
            }
    
            return Integer.parseInt(interval);
        }
    
       int record = RestoreRecord0();
       private static int activities = 0;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
    
    
    
            this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
            tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    
    
            this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
    
            last_update_time_txtbx = (TextView) findViewById(R.id.last_update_time_txt);
            last_update_time_txtbx.setText(" "+ String.valueOf(java.text.DateFormat.getDateTimeInstance()
                  .format(Calendar.getInstance().getTime())));  
    
    
    
            MyLocation loc = new MyLocation(this.getApplicationContext());
            TextView longitude_view = (TextView)findViewById(R.id.GPS_Long_txta);
            ((TextView) longitude_view).setText(" "+String.valueOf(loc.gps_lon));
            TextView latitude_view = (TextView)findViewById(R.id.GPS_Lat_txta);
            ((TextView) latitude_view).setText(" "+String.valueOf(loc.gps_lat));
    
    
    
            //refresh_location(GPS_Long_txtbx,GPS_lat_txtbx);
    
    
                    final Button btnSkip = (Button) findViewById(R.id.btnRegister);
                    // btnSkip.setTypeface(face);
                    btnSkip.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            // Perform action on click EditText et
    
    
                            Intent i = new Intent(ETrackActivity.this, Configuration.class);                        
                            //i.putExtra("Skip", "true");
                            // i.putExtra("Location", txtEmail.getText());
                            startActivity(i);
                            }
                    });
    
    
        //      last_update_time_txtbx = (TextView)findViewById(R.id.last_update_time_txt);
       //       last_update_time_txtbx.setText(" "+ String.valueOf(java.text.DateFormat.getDateTimeInstance()
    //          .format(Calendar.getInstance().getTime())));
    
    
    
                    // in this response variable it contains  
                    String response = CallWebService(tm.getDeviceId(), loc.gps_lon, loc.gps_lat);
    
    
                    long delaytime =(record * 1000);
                    h.postDelayed(run, delaytime);
    
    
                    }
    
    
    
      //   public void refresh_location(View longitude_view, View latitude_view, View last_update_time_txtbx)
         public void refresh_location(View longitude_view, View latitude_view)
    
        {
    
            longitude_view = (TextView)findViewById(R.id.GPS_Long_txta);
            ((TextView) longitude_view).setText(" "+String.valueOf(LocationService.GPS_Long));
            latitude_view = (TextView)findViewById(R.id.GPS_Lat_txta);
            ((TextView) latitude_view).setText(" "+String.valueOf(LocationService.GPS_Lat));
    //      last_update_time_txtbx = (TextView)findViewById(R.id.last_update_time_txt);
    //      ((TextView) last_update_time_txtbx).setText(""+String.valueOf(java.text.DateFormat.getDateTimeInstance()
    //      .format(Calendar.getInstance().getTime())));
    
        }
    
    
        public void onNothingSelected(AdapterView<?> arg0) {
    
        }
    
            public void onClick(View view)
        {
            startActivity(new Intent(ETrackActivity.this,Configuration.class));
    
        }
    
            public String CallWebService(String imei, double longt, double lat) {
            String response = new String();
            String str = new String();
    
            try {
    
                String url = "http://helpdesk.cispl.com/etracking/track.php?imei="+imei+"&gps_lat="+ Double.toString(lat) +"&gps_lon="+Double.toString(longt);
                HttpClient client = new DefaultHttpClient();
                HttpGet request = new HttpGet();
                URI webservice = new URI(url);
                request.setURI(webservice);
                HttpResponse httpResponse = client.execute(request);
                HttpEntity responseEntity = httpResponse.getEntity();    
    
                 if (responseEntity != null) {
                        response = EntityUtils.toString(responseEntity);
                    }           
                } catch (Exception e1){
                    e1.printStackTrace();
                }
    
                return response;
            }
    
    
    } 
    
    请给我解决方案.....

1 个答案:

答案 0 :(得分:2)

要在后台运行该应用程序,您需要使用Service

使用服务的教程:herehere以及this