创建服务以读取远程文件内容,并将gps坐标写入另一个文件(如果它包含1)

时间:2013-12-12 07:04:08

标签: android gps

我是android的新手,也是java的中途。我想为我的安全应用程序创建一个服务。服务每隔3秒检查一次远程文件的内容,如果文件中有一个strlen()= 1,那么gps cordinates将被写入另一个远程文件中。我使用php脚本编写gps cordinates和BufferReader来读取文件。这是我的代码:

package com.example.spotter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeoutException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.GpsSatellite;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MainService extends Service implements LocationListener {

    String str;
    int infinite = 0;
    GpsSatellite gpsat;
    double longi=0,lati=0;


    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Toast.makeText(this, "Service Stopped", Toast.LENGTH_SHORT).show();
        stopService(new Intent(getBaseContext(), MainService.class));
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        enter code hereToast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
        Notification note = new Notification(R.drawable.ic_launcher," ", System.currentTimeMillis());
        Intent i = new Intent(this, MainService.class);
        i.setFlags(Intent`enter code here`.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);
        note.setLatestEventInfo(this, " "," ", pi);
        note.flags |= Notification.FLAG_NO_CLEAR;       
        startForeground(1, note);
        //while(isNetworkAvailable()){
        while (true) {

            try {
                Thread.sleep(3000);

            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            //isNetworkAvailable();
            try {
                Log.d("spt", "read status started");
                URL urlreadstatus = new URL("http://xyz.abc.com/status.txt");
                BufferedReader in = new BufferedReader(new InputStreamReader(urlreadstatus.openStream()));
                str = in.readLine();
                Log.d("spt", str);
                in.close();
                Log.d("spt", "read status complete");
            } catch (MalformedURLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if (str.length()==1) {
                LocationManager l= (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                l.requestLocationUpdates("gps", 1, 1, this);      
            //  this.onLocationChanged(loc)
                DefaultHttpClient httpClient = new DefaultHttpClient();
                 HttpPost httpPost = new HttpPost("http://abc.xyz.com/writegps.php?value="+String.valueOf(longi)+","+String.valueOf(lati));
                 Toast.makeText(this, String.valueOf(lati)+String.valueOf(longi)+"written", Toast.LENGTH_SHORT).show();

                      try {
                        httpClient.execute(httpPost);
                    } catch (ClientProtocolException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                //break;


            }
            else{
                continue;   
            }
            //return START_STICKY;
        }
        //}//is net available

        //return START_STICKY;

    }

    @Override
    public void onLocationChanged(Location loc) {
        // TODO Auto-generated method stub  
            longi=loc.getLongitude();
        lati=loc.getLatitude();    
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-g`enter code here`enerated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }
    private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager 
              = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        Toast.makeText(this,"working internet", Toast.LENGTH_SHORT).show();
        return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
    }

}

这个问题是它在启动时被冻结,然后由于ANR而在一段时间后崩溃,gps坐标也没有被写入文件。读书还可以。

如果是解决方案,我该如何使用线程呢?请帮忙。

0 个答案:

没有答案