操作系统杀死android服务,再也不重启它
我已经创建了一个应用程序,它具有每分钟跟踪用户位置的功能,操作系统杀死android服务,再也不重启它,android,service,locationmanager,Android,Service,Locationmanager,我已经创建了一个应用程序,它具有每分钟跟踪用户位置的功能, 在服务器上发送(跟踪基于24*7*365)。但我面临的问题是,有一段时间操作系统会停止服务,再也不会重新启动。 我使用下面给出的代码 public class DisplaySystem extends Service implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedLi
在服务器上发送(跟踪基于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);
}
公共类DisplaySystem扩展服务实现
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,
com.google.android.gms.location.LocationListener{
@凌驾
公共空间{
//TODO自动生成的方法存根
super.ondestory();
}
公共静态UpdatedDressThread UpdatedDressThread;
public final String NAMESPACE=CommonUtilities.APP_NAMESPACE;
公共最终字符串URL=CommonUtilities.APP\u URL;
私有静态最终字符串标记=“@@@@@@@@@@@@@@@@@@@@@@@”;
私有静态字符串imei;
静态语境;
长最小时间=50*1000l;
浮动距离=50.0f;
专用静态定时器;
字符串为红色;
//静态字符串数据\光标;
静态字符串地址;
双区;
布尔启动;
静态最终字符串URLD=”http://maps.googleapis.com/maps/api/distancematrix/xml?";
静态最终字符串KEY\u ITEM=“element”;//父节点
静态最终字符串键\u ID=“status”;
专用静态位置1;
专用静态长毫秒gps;
公共静态字符串chaseforceLogMessage=“”;
静态ArrayList rowIdList=新ArrayList();
公共浮点数getPrv_lat(){
//if(ChaseForce.D)Log.D(“DisplaySyste”、“getPrv_lat”);
返回PreferenceData.getTempString(上下文,“prv_lat”);
//返回prv_lat;
}
公共无效设置PRV_lat(浮动LAST){
PreferenceData.setLocData(上下文,“prv_lat”,lastLat);
}
公共浮点数getPrv_long(){
返回PreferenceData.getTempString(上下文,“prv_long”);
}
公共无效设置PRV_long(浮动最长){
PreferenceData.setLocData(上下文,“prv_long”,lastLong);
}
公共静态位置getLocation1(){
返回位置1;
}
公共静态无效设置位置1(位置位置1){
DisplaySystem.location1=位置1;
}
//私有静态浮动[]结果;
@凌驾
公共IBinder onBind(意图arg0){
//TODO自动生成的方法存根
返回null;
}
//公共静态最终字符串广播\u ACTION=“com.example.Demo”;
//静态意图1;
//连接到位置服务的请求
私人位置请求mLocationRequest;
私人地点客户M地点客户;
共享参考文献;
SharedReferences.Editor mEditor;
布尔值mUpdatesRequested=true;
@凌驾
public void onCreate(){
super.onCreate();
context=getApplicationContext();
}
@SuppressLint(“唤醒锁”)
@抑制警告(“弃用”)
@凌驾
公共int onStartCommand(最终意图、int arg1、int arg2){
System.out.println(“服务已启动-->”;
//TODO自动生成的方法存根
writeprifrece1(“用户首次登录”,true);
ChaseForce.writeprifrece1(“服务启动”,false);
如果(强制力D)
Log.d(标记“onStart()”);
TelephonyManager TelephonyManager=(TelephonyManager)getSystemService(Context.TELEPHONY_服务);
//imei=“1234567890123”;
imei=telephonyManager.getDeviceId();
mLocationRequest=LocationRequest.create();
mlLocationRequest.setInterval((Long.parseLong)(ChaseForce
.ReadPrifrence(“位置更新间隔”)*1000)+1000);
//使用高精度
mLocationRequest.setPriority(位置请求.优先级高精度);
mlLocationRequest.SetFastTestInterval((Long.parseLong)(ChaseForce
.ReadPrifrence(“位置更新间隔”)*1000)+1000);
mUpdatesRequested=true;
mPrefs=getSharedReferences(
adm.enterprises.chaseforce.LocationUtils.SHARED_首选项,
上下文。模式(私人);
mEditor=mPrefs.edit();
mLocationClient=新位置客户端(这个,这个,这个);
// //////////////////////////////////////////
如果(计时器!=null){
timer.cancel();
}
定时器=新定时器();
on_start=true;
if(intent!=null){
if(intent.getBooleanExtra(“OnDuty”,true)!=false){
on_start=intent.getExtras().getBoolean(“OnDuty”);
}否则{
on_开始=错误;
}
}否则{
on_start=true;
}
if(servicesConnected()){
mLocationClient.connect();
}
toCallAsynchronousGetUpdate();
线程obj1=新线程(新可运行(){
@凌驾
公开募捐{
//TODO自动生成的方法存根
做{
Log.e(“内部”、“DO方法”);
SendLocationServer(红色);
/*唤醒屏幕()*/
试一试{
睡眠(120000);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}虽然(正确);
}
});
如果(!obj1.isAlive()){
obj1.start();
}
super.onstart命令(intent、arg1、arg2);
返回START\u STICKY\u兼容性;
}
public void onLowMemory(){
super.onLowMemory();
}
公共静态无效到CallAsynchronousGetUpdate(){
System.out.println(“本地云@@”);
TimerTask doAsynchronousTaskGetUpdate;
最终处理程序=新处理程序();
定时器=新定时器();
doAsynchronousTaskGetUpdate=新时间任务(){
@凌驾
公开募捐{
//TODO自动生成的方法存根
handler.post(新的Runnable(){
公开募捐{
如果(强制力D)
Log.e(“Cloud_Local”、“toCallAsynchronousGetUpdate”);
试一试{
GetUpdateTask GetUpdateTask=新建GetUpdateTask(
上下文);
getUpdateTask
.execute(新字符串[]{“local_cloud”});