1

我是安卓新手。我开发了一个应用程序,我在其中计算我当前位置的经度和纬度,并在特定时间间隔使用它的处理程序更新日期和时间,它通过从该应用程序获取数据来映射谷歌地图上的位置,该应用程序可以准确地更新但一个问题是,当手机被锁定时,这个应用程序无法运行。

  1. 我想在后台运行这个应用程序,它会一直运行直到&,除非手机被关闭。当我从该应用程序返回后重新打开应用程序时,该应用程序也开始提供更新。
  2. 我想显示上次更新的日期和时间。不重新启动应用程序,也不从当前开放时间更新日期和时间。它在后台持续工作并显示在后台工作的活动。

请帮助...我将非常感谢

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

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;
        }


} 

请给我解决方案.....

4

1 回答 1

2

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

使用服务的教程:hereherethis

于 2012-11-08T05:38:21.480 回答