操作系统杀死Android服务,永远不会重新启动它

时间:2014-06-25 10:06:10

标签: android service locationmanager

我创建了一个应用程序,它具有在每分钟跟踪用户位置的功能  在服务器上发送(跟踪基于24 * 7 * 365)。但我面临的问题就像有些时候操作系统终止服务而永远不会再次重启。 我正在使用这个代码。

public class DisplaySystem extends Service implements
    GooglePlayServicesClient.ConnectionCallbacks,
    GooglePlayServicesClient.OnConnectionFailedListener,
    com.google.android.gms.location.LocationListener {

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

}

public static UpdateAddressThread updateAddressThread;
public final String NAMESPACE = CommonUtilities.APP_NAMESPACE;
public final String URL = CommonUtilities.APP_URL;
private static final String TAG = "@@@@@@@DisplaySystem@@@@@@@@@@";
private static String imei;
static Context context;
long minTime = 50 * 1000l;
float minDistance = 50.0f;
private static Timer timer;
String mythred;
// static String data_cursor;
static String myAddress;
double dist;

boolean on_start;
static final String URLD = "http://maps.googleapis.com/maps/api/distancematrix/xml?";
static final String KEY_ITEM = "element"; // parent node
static final String KEY_ID = "status";
private static Location location1;
private static long milisec_gps;
public static String chaseforceLogMessage = "<---Service messages-->";
static ArrayList<String> rowIdList = new ArrayList<String>();

public float getPrv_lat() {
    // if (ChaseForce.D) Log.d("DisplaySyste","getPrv_lat");
    return PreferenceData.getTempString(context, "prv_lat");
    // return prv_lat;
}

public void setPrv_lat(float lastLat) {
    PreferenceData.setLocData(context, "prv_lat", lastLat);

}

public float getPrv_long() {
    return PreferenceData.getTempString(context, "prv_long");
        }

public void setPrv_long(float lastLong) {
    PreferenceData.setLocData(context, "prv_long", lastLong);
}

public static Location getLocation1() {
    return location1;
}

public static void setLocation1(Location location1) {
    DisplaySystem.location1 = location1;
}


// private static float[] results;
@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

// public static final String BROADCAST_ACTION = "com.example.Demo";
// static Intent intent1;
// A request to connect to Location Services
private LocationRequest mLocationRequest;

private LocationClient mLocationClient;

SharedPreferences mPrefs;

SharedPreferences.Editor mEditor;


boolean mUpdatesRequested = true;

@Override
public void onCreate() {
    super.onCreate();


    context = getApplicationContext();

}

@SuppressLint("Wakelock")
@SuppressWarnings("deprecation")
@Override
public int onStartCommand(final Intent intent, int arg1, int arg2) {

    System.out.println("SERVICE STARTED-->");

    // TODO Auto-generated method stub

    ChaseForce.WritePrifrence1("user_first_login", true);
    ChaseForce.WritePrifrence1("service_start", false);

    if (ChaseForce.D)
        Log.d(TAG, "onStart()");
    TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    // imei = "1234567890123";
    imei = telephonyManager.getDeviceId();
    mLocationRequest = LocationRequest.create();

    mLocationRequest.setInterval((Long.parseLong(ChaseForce
            .ReadPrifrence("Location_update_interval")) * 1000) + 1000);

    // Use high accuracy
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    mLocationRequest.setFastestInterval((Long.parseLong(ChaseForce
            .ReadPrifrence("Location_update_interval")) * 1000) + 1000);

    mUpdatesRequested = true;

    mPrefs = getSharedPreferences(
            adm.enterprises.chaseforce.LocationUtils.SHARED_PREFERENCES,
            Context.MODE_PRIVATE);

    mEditor = mPrefs.edit();

    mLocationClient = new LocationClient(this, this, this);

    // //////////////////////////////////////////
    if (timer != null) {
        timer.cancel();
    }
    timer = new Timer();

    on_start = true;
    if (intent != null) {
        if (intent.getBooleanExtra("OnDuty", true) != false) {

            on_start = intent.getExtras().getBoolean("OnDuty");
        } else {
            on_start = false;
        }

    } else {
        on_start = true;
    }

    if (servicesConnected()) {
        mLocationClient.connect();
    }

    toCallAsynchronousGetUpdate();

    Thread obj1 = new Thread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            do {
                Log.e("INSIDE ", "DO METHOD");

                sendLocationOnServer(mythred);
                /* wakeScreen(); */

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


                }
            } while (true);

        }
    });

    if (!obj1.isAlive()) {
        obj1.start();

    }

    super.onStartCommand(intent, arg1, arg2);
    return START_STICKY_COMPATIBILITY;
}


public void onLowMemory() {

    super.onLowMemory();

}

public static void toCallAsynchronousGetUpdate() {
    System.out.println("local cloud@@@ ");
    TimerTask doAsynchronousTaskGetUpdate;
    final Handler handler = new Handler();
    Timer timer = new Timer();
    doAsynchronousTaskGetUpdate = new TimerTask() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            handler.post(new Runnable() {
                public void run() {
                    if (ChaseForce.D)
                        Log.e("Cloud_Local", "toCallAsynchronousGetUpdate");

                    try {
                        GetUpdateTask getUpdateTask = new GetUpdateTask(
                                context);
                        getUpdateTask
                                .execute(new String[] { "local_cloud" });
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                    }
                }
            });
        }
    };
    timer.schedule(doAsynchronousTaskGetUpdate, 2 * 60 * 1000,
            1 * 60 * 60 * 1000);// execute
        }

@SuppressWarnings("unused")
public static void sendLocationOnServer(String my_thred_nmae) {

    System.out.println("send location onserver method");
    try {

        Database smsDatabase = new Database(context);
        SQLiteDatabase db = smsDatabase.getWritableDatabase();
        Cursor cursor = db.query("LOCATION", new String[] { "rowid",
                "Latitude", "Longitude", "KM", "IEMI", "DateTime",
                "milliseconds", "Battery_Level", "Gps_Status", "Address",
                "CurrentKm" }, null, null, null, null, "milliseconds");
        String locationData = "";
        String Lastmili = new String();
        double Lat = 0.0;
        double Long = 0.0;

        if (ChaseForce.ReadPrifrence("location_data").equals("")
                || ChaseForce.ReadPrifrence("location_data").equals(null)) {

            if (cursor.getCount() != 0) {
                rowIdList.clear();
                cursor.moveToFirst();
                int i = 0;
                double km = 0.0;
                do {

                    locationData += fetchDataFromCursor(cursor);
                    System.out.println("cursor data-->" + locationData);

                    Lastmili = cursor.getString(cursor
                            .getColumnIndex("milliseconds"));
                    Lat = Double.parseDouble(cursor.getString(cursor
                            .getColumnIndex("Latitude")));
                    Long = Double.parseDouble(cursor.getString(cursor
                            .getColumnIndex("Longitude")));

                    System.out.println("Rowid value--->"
                            + cursor.getString(cursor
                                    .getColumnIndex("rowid")));
                    rowIdList.add(cursor.getString(cursor
                            .getColumnIndex("rowid")));
                    if (ChaseForce.D)
                        Log.d("DisplaySyste", "cursor.moveToNext()" + i);
                    i++;
                } while (cursor.moveToNext() && i < 10);

                ChaseForce.WritePrifrence("location_data", locationData);

                int j = 0;
                for (int k = 0; k < DisplaySystem.rowIdList.size(); k++) {
                    j += db.delete("LOCATION", "rowid = ?",
                            new String[] { DisplaySystem.rowIdList.get(k) });
                }

                System.out.println("Number of Rows deleted::--> " + j);
            }

        } else {
            System.out.println("Do nothing!!!!");
        }
        cursor.close();
        db.close();

        ChaseForce.WritePrifrence("milisec", Lastmili);
        if (ChaseForce.ReadPrifrence("location_data").equals("")
                || ChaseForce.ReadPrifrence("location_data").equals(null)) {
            // ChaseForce.WritePrifrence("location_data", locationData);
            Log.e("Location data", "Empty");
            /*
             * String ThredException =
             * "Location data is empty beacuse OnLocationChange() not working"
             * ; logPrint(ThredException);
             */
        } else {
            String dataLocation = ChaseForce.ReadPrifrence("location_data");
            /*
             * String dataLocation = my_thred_nmae + "  " +
             * String.valueOf(ChaseForce.packetId++) + "#" +
             * ChaseForce.ReadPrifrence("location_data");
             */
            ConThread conThread1 = new ConThread(3, dataLocation, Lastmili,
                    context);
            conThread1.start();
        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        System.out.println("FORCE CLOSE REASON--->");
    }
}

public static String fetchDataFromCursor(Cursor cursor) {

    // TODO Auto-generated method stub
    String lat, lon, km, imei, datetime, battlevel, gpsstatus, address, currentkm;
    String data_cursor = "";
    lat = cursor.getString(cursor.getColumnIndex("Latitude"));
    lon = cursor.getString(cursor.getColumnIndex("Longitude"));
    km = cursor.getString(cursor.getColumnIndex("KM"));
    imei = cursor.getString(cursor.getColumnIndex("IEMI"));
    datetime = cursor.getString(cursor.getColumnIndex("DateTime"));
    battlevel = cursor.getString(cursor.getColumnIndex("Battery_Level"));
    gpsstatus = cursor.getString(cursor.getColumnIndex("Gps_Status"));

    System.out.println("DATA-->" + lat + lon + km + imei + datetime
            + battlevel + gpsstatus);
    address = cursor.getString(cursor.getColumnIndex("Address"));
    System.out.println("DATA-->" + lat + lon + km + imei + datetime
            + battlevel + gpsstatus + address);

    if (address.equalsIgnoreCase("null")) {
        /*
         * String ThredException =
         * "Null adress found when net is disconnected!!" +
         * String.valueOf(System.currentTimeMillis());
         * DisplaySystem.logPrint(ThredException);
         */
        System.out.println("Adress after fettng null--->" + address);
        address = getAddressFromWeb(Double.parseDouble(lat),
                Double.parseDouble(lon));

    }
    System.out.println("Adress after fettng --->" + address);
    currentkm = cursor.getString(cursor.getColumnIndex("CurrentKm"));

    data_cursor = (new String(lat + "~" + lon + "~" + km + "~" + imei + "~"
            + datetime + "~" + battlevel + "~" + gpsstatus + "~" + address
            + "~" + currentkm + "~" + ","));
    return data_cursor;

}

public static String getAddressFromWeb(double latitude, double longitude) {
    // TODO Auto-generated method stub
    myAddress = "null";
    System.out.println("<---MY CODE IS  WORKING NOW--->");
    // TODO Auto-generated method stub
    final String TAG_CONTACTS = "results";

    String address = String.format(Locale.ENGLISH,
            "http://maps.googleapis.com/maps/api/geocode/json?latlng="
                    + latitude + "," + longitude + "&sensor=false&time"
                    + System.currentTimeMillis() + latitude, longitude);

    System.out.println("fetching path : " + address);
    // Creating JSON Parser instance

    try {
        System.out.println("INSIDE TRY-->");
        JSONArray results = null;
        @SuppressWarnings("unused")
        JSONArray Allcontacts = null;
        JSONParser jParser = new JSONParser(context);
        JSONObject json = jParser.getJSONFromUrl(address);

        String json_status = json.getString("status");
        System.out.println("after ststus" + " " + json.getString("status"));
        if (json.getString("status").equalsIgnoreCase("OK")) {

            results = json.getJSONArray(TAG_CONTACTS);

            JSONObject c = results.getJSONObject(0);
            String strReturnedAddress = c.getString("formatted_address");
            myAddress = strReturnedAddress.toString().replace(",", " ");

            ChaseForce.WritePrifrence("JournyAdd", myAddress);

        } else {
            System.out.println("Insert value-->" + myAddress + "STATUS "
                    + json_status);
            return myAddress + "STATUS " + json_status;
        }
    } catch (Exception e) {
        // TODO: handle exception
        System.out.println("My exception-->" + e.getMessage());

        myAddress = "null";
        return myAddress;

    }
    return myAddress;

}

public float getBatteryLevel() {
    Intent batteryIntent = registerReceiver(null, new IntentFilter(
            Intent.ACTION_BATTERY_CHANGED));
    int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
    if (ChaseForce.D)
        Log.d("Battary Level : ",
                String.valueOf(((float) level / (float) scale) * 100.0f));

    return ((float) level / (float) scale) * 100.0f;
}

private String GetBattryAlertString(String battry_level) {
    if (ChaseForce.D)
        System.out.println("battry_level : " + battry_level);
    String alert_string = new String();
    Time sys_time = new Time();
    sys_time.setToNow();
    String cur_time = String.valueOf(sys_time.hour)
            + String.valueOf(sys_time.minute);
    if (ChaseForce.D)
        System.out.println("cur_time: " + cur_time);
    Database smsDatabase = new Database(context);
    SQLiteDatabase db = smsDatabase.getWritableDatabase();
    Cursor cursor = db
            .rawQuery(
                    "select * from ALERT where AlertName=? and FromTimeInterval<? and ToTimeInterval>?  and Status=?",
                    new String[] { "2", cur_time, cur_time, "0" });
    ArrayList<Alerts> arrayAlerts = new ArrayList<Alerts>();
    Gson gson = new Gson();
    if (cursor.getCount() != 0) {
        cursor.moveToFirst();

        do {

            int AlertId = cursor.getInt(cursor.getColumnIndex("AlertId"));
            String AlertName = cursor.getString(cursor
                    .getColumnIndex("AlertName"));
            String Alert_type = cursor.getString(cursor
                    .getColumnIndex("Alert_type"));
            int Occerancetype = cursor.getInt(cursor
                    .getColumnIndex("Occerancetype"));
            String FromTimeInterval = cursor.getString(cursor
                    .getColumnIndex("FromTimeInterval"));
            String ToTimeInterval = cursor.getString(cursor
                    .getColumnIndex("ToTimeInterval"));
            String Threshold = cursor.getString(cursor
                    .getColumnIndex("Threshold"));
            int Status = cursor.getInt(cursor.getColumnIndex("Status"));
            int CurrentStatus = cursor.getInt(cursor
                    .getColumnIndex("CurrentStatus"));
            String Mobile_No = cursor.getString(cursor
                    .getColumnIndex("Mobile_No"));

            Alerts alert = new Alerts(AlertId, AlertName, Alert_type,
                    Occerancetype, FromTimeInterval, ToTimeInterval,
                    Threshold, Status, Mobile_No);
            if (ChaseForce.D)
                System.out.println("Battry Level "
                        + "Status: "
                        + Status
                        + "CurrentStatus : "
                        + (CurrentStatus == 0)
                        + "Occerancetype : "
                        + (Occerancetype == 0)
                        + "FromTimeInterval : "
                        + (Integer.parseInt(FromTimeInterval) < Integer
                                .parseInt(cur_time)));
            if (Integer.parseInt(Threshold) > Integer
                    .parseInt(battry_level)) {

                // if (alert.getOccerancetype() == 0) {
                // arrayAlerts.add(alert);
                // } else {
                if (CurrentStatus == 0) {
                    arrayAlerts.add(alert);
                }
                // }
            } else {
                if (CurrentStatus == 1) {
                    ContentValues updateValues = new ContentValues();
                    updateValues.put("CurrentStatus", 0);
                    db.update("ALERT", updateValues, "AlertId=?",
                            new String[] { String.valueOf(AlertId) });
                }
            }
            // arrayAlerts.add(alert);

        } while (cursor.moveToNext());
        alert_string = gson.toJson(arrayAlerts);
        if (ChaseForce.D)
            System.out.println("Battry alert_string: " + alert_string);
    } else {
        if (ChaseForce.D)
            System.out.println("Battry alert_string: null");
        alert_string = null;
    }

    cursor.close();
    db.close();
    return alert_string;
}

/**
 * Verify that Google Play services is available before making a request.
 * 
 * @return true if Google Play services is available, otherwise false
 */
private boolean servicesConnected() {

    // Check that Google Play services is available
    int resultCode = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(this);

    // If Google Play services is available
    if (ConnectionResult.SUCCESS == resultCode) {
        // In debug mode, log the status
        if (ChaseForce.D)
            Log.d(LocationUtils.APPTAG,
                    getString(R.string.play_services_available));

        // Continue
        return true;
        // Google Play services was not available for some reason
    } else {
        if (ChaseForce.D)
            Log.d(LocationUtils.APPTAG, "Play Service is not available");
        return false;
    }
}

@Override
public void onConnectionFailed(ConnectionResult result) {
    // TODO Auto-generated method stub

}

@Override
public void onConnected(Bundle connectionHint) {
    // TODO Auto-generated method stub
    startPeriodicUpdates();
}

@Override
public void onDisconnected() {
    // TODO Auto-generated method stub

}

@SuppressWarnings("unused")
@Override
public void onLocationChanged(Location location) {

    ChaseForce.WritePrifrence("currentmilisec",
            Double.toString(location.getTime()));
    final Calendar ct1 = Calendar.getInstance();
    ct1.setTimeInMillis(location.getTime());
    int f_Year1 = ct1.get(Calendar.YEAR);
    int f_Month1 = ct1.get(Calendar.MONTH) + 1;
    int f_Day1 = ct1.get(Calendar.DAY_OF_MONTH);
    String from_date1 = String.format(f_Year1 + "/" + f_Month1 + "/"
            + f_Day1 + " " + ct1.get(Calendar.HOUR_OF_DAY) + ":"
            + ct1.get(Calendar.MINUTE) + ":" + ct1.get(Calendar.SECOND));
    System.out.println("Gps Lat lng" + location.getLatitude() + ";"
            + location.getLongitude() + "Gps time-->" + from_date1);

    double km;

    if (location != null) {
        if (ChaseForce.D)
            Log.d("location Display System: ", location.toString());

        /*
         * intent1.putExtra("location", location);
         * 
         * sendBroadcast(intent1);
         */
        // /////////////////////////new ////////////////////
        if (getPrv_lat() != 0.0f && getPrv_long() != 0.0f) {
            double earthRadius = 6371.00;
            double dLat = Math.toRadians(location.getLatitude()
                    - getPrv_lat());
            double dLng = Math.toRadians(location.getLongitude()
                    - getPrv_long());
            double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
                    + Math.cos(Math.toRadians(getPrv_lat()))
                    * Math.cos(Math.toRadians(location.getLatitude()))
                    * Math.sin(dLng / 2) * Math.sin(dLng / 2);
            double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
            dist = earthRadius * c;

            Date d3 = new Date(location.getTime());
            Date d4 = new Date(ChaseForce.ReadLongPriference("MIliGps"));

            System.out.println("Calculating distance date" + d3 + "-->"
                    + d4);
            if ((dist >= 0.3 && (location.getTime() - ChaseForce
                    .ReadLongPriference("MIliGps")) > (Long
                    .parseLong(ChaseForce
                            .ReadPrifrence("Location_update_interval")) * 1000) + 1000)
                    && (d3.compareTo(d4) > 0)) {

                km = (dist + ChaseForce.ReadFloatPrifrence("Km"));
                System.out.println("kilometer aftre adding--->" + km);
                ChaseForce.WritePrifrence("meterreading",
                        Double.toString(km));
                System.out.println("Metereeding Value--->"
                        + Double.toString(km));
                ChaseForce.WritePrifrence("Km", (float) km);

                setPrv_lat((float) location.getLatitude());
                setPrv_long((float) location.getLongitude());

            } else {
                km = ChaseForce.ReadFloatPrifrence("Km");

                System.out.println("Inside First Else Km-->" + km);
                ChaseForce.WritePrifrence("meterreading",
                        String.valueOf(km));

                /*
                 * ChaseForce.WritePrifrence("meterreading",
                 * ChaseForce.ReadPrifrence("meterreading"));
                 */
                System.out.println("actual km2-->" + km);
            }
        } else {

            Log.d("DisplaySyste", "getPrv_lat()!=0.0&&getPrv_long()!=0.0");
            km = ChaseForce.ReadFloatPrifrence("Km");
            System.out.println("Inside Second Else Km-->" + km);
            /*
             * ChaseForce.WritePrifrence("meterreading",
             * ChaseForce.ReadPrifrence("meterreading"));
             */

            ChaseForce.WritePrifrence("meterreading", String.valueOf(km));

            setPrv_lat((float) location.getLatitude());
            setPrv_long((float) location.getLongitude());
        }
        String from_date = null;
        Date d1 = new Date(location.getTime());
        Date d2 = new Date(ChaseForce.ReadLongPriference("MIliGps"));

        System.out.println("Date before inserting-->" + d1 + "-->" + d2);
        if ((d1.compareTo(d2) > 0)) {

            if (((location.getTime() - ChaseForce
                    .ReadLongPriference("MIliGps")) > (Long
                    .parseLong(ChaseForce
                            .ReadPrifrence("Location_update_interval")) * 1000) + 1000)) {

                System.out.println("<----Data Inserted Sucessfully--->");
                ChaseForce.WritePrifrence("MIliGps", location.getTime());
                Database smsDatabase = new Database(context);
                SQLiteDatabase db = smsDatabase.getWritableDatabase();

                ContentValues insertValues = new ContentValues();
                insertValues.put("Latitude",
                        Double.toString(location.getLatitude()));
                insertValues.put("Longitude",
                        Double.toString(location.getLongitude()));

                insertValues.put("KM", km);

                insertValues.put("IEMI", ChaseForce.ReadPrifrence("ID"));
                insertValues.put("Battery_Level",
                        String.valueOf(getBatteryLevel()));
                final Calendar ct = Calendar.getInstance();
                ct.setTimeInMillis(location.getTime());
                int f_Year = ct.get(Calendar.YEAR);
                int f_Month = ct.get(Calendar.MONTH) + 1;
                int f_Day = ct.get(Calendar.DAY_OF_MONTH);
                from_date = String.format(f_Year + "/" + f_Month + "/"
                        + f_Day + " " + ct.get(Calendar.HOUR_OF_DAY) + ":"
                        + ct.get(Calendar.MINUTE) + ":"
                        + ct.get(Calendar.SECOND));
                insertValues.put("DateTime", from_date);

                LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                    insertValues.put("Gps_Status", 0);
                } else {
                    insertValues.put("Gps_Status", 1);
                }

                String add_data = getAddressFromWeb(location.getLatitude(),
                        location.getLongitude());
                System.out.println("google api geocoder-->" + add_data);

                insertValues.put("Address", add_data);

                // srs code added
                System.out.println("Current km" + dist);
                insertValues.put("CurrentKm", dist);

                insertValues.put("milliseconds",
                        Long.toString(location.getTime()));

                if (db.isOpen()) {
                    db.insert("LOCATION", null, insertValues);
                    db.close();
                } else {
                    db.close();
                }
            } else {
                System.out.println("Data is not inserted-->");
            }
        } else {
            System.out.println("TIME DOSENT EXCEDEED");
        }

        final Calendar ct2 = Calendar.getInstance();
        ct2.setTimeInMillis(location.getTime());
        int f_Year2 = ct2.get(Calendar.YEAR);
        int f_Month2 = ct2.get(Calendar.MONTH) + 1;
        int f_Day2 = ct2.get(Calendar.DAY_OF_MONTH);
        String from_date2 = String
                .format(f_Year2 + "/" + f_Month2 + "/" + f_Day2 + " "
                        + ct2.get(Calendar.HOUR_OF_DAY) + ":"
                        + ct2.get(Calendar.MINUTE) + ":"
                        + ct2.get(Calendar.SECOND));
        System.out.println("from date2" + from_date2);
        ChaseForce.WritePrifrence("DateTime", from_date2);

        generateNotification(context, "Location updated at " + from_date2);

        String a_string = GetBattryAlertString(String
                .valueOf((int) getBatteryLevel()));
        if (a_string != null && a_string.length() > 2) {

            SendAlertLowBattry sendAlertLowBattry = new SendAlertLowBattry(
                    context, new String[] { "SOS_Alert", imei, a_string });
            sendAlertLowBattry.start();

        }
    } else {
        if (ChaseForce.D)
            Log.d("DisplaySyste", "location is null");

    }

}

private void startPeriodicUpdates() {
    try {
        mLocationClient.requestLocationUpdates(mLocationRequest, this);
    } catch (IllegalStateException e) {
        // TODO: handle exception
        if (ChaseForce.D)
            Log.e(TAG, "IllegalStateException on connect");
        }

}

@SuppressWarnings("unused")
private void stopPeriodicUpdates() {
    mLocationClient.removeLocationUpdates(this);

}

@SuppressWarnings("deprecation")
private static void generateNotification(Context context, String message) {
    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);
    String title = context.getString(R.string.app_name);

    Intent notificationIntent = new Intent(context, CurrentStatus.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(5, notification);

}

}

1 个答案:

答案 0 :(得分:1)

您需要有一个前台服务才能让它一直工作。当操作系统资源不足时,它可能会终止后台服务,后台服务可能无法再次启动。为确保服务始终运行,必须将其作为前台服务启动。

EDIT2:

  

public final void startForeground(int id,Notification notification)

     

使此服务在前台运行,提供正在进行的服务   在此状态下向用户显示通知。默认情况下   服务是后台的,这意味着如果系统需要杀死它们   回收更多内存(例如在网络中显示大页面)   浏览器),他们可以在没有太大伤害的情况下被杀死。你可以设置它   如果查杀您的服务会对用户造成破坏,例如   如果您的服务正在执行背景音乐播放,那么用户   会注意到他们的音乐是否停止播放。

如需进一步阅读,请参阅http://developer.android.com/reference/android/app/Service.html