我有一个Android应用程序,它由活动,接收器和服务组成。 安装后只启动一次活动以设置用户名和密码,然后启动服务。电话启动后,接收器会调用该服务。电话进入睡眠状态后,服务会进入休眠状态,我需要全部工作时间 。我已将调用网站放在一个帖子中。这是服务
public class MyService extends Service {
public int indikator=0;
public String mojsadrzaj=new String();
public String pib=new String();
public String username=new String();
public String password=new String();
public String url=new String();
public URL myURL;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this,"Service created ...",Toast.LENGTH_LONG).show();
final SQLiteDatabase db=openOrCreateDatabase("TruckMe", MODE_PRIVATE, null);
String idvozaca=new String();
db.execSQL("create table if not exists vozac (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, idvozaca VARCHAR(15));");
Cursor d=db.rawQuery("select idvozaca from vozac", null);
if(d.getCount() > 0)
{
d.moveToLast();
idvozaca=d.getString(d.getColumnIndex("idvozaca"));
mojsadrzaj=idvozaca;
}
final Time t=new Time();
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
public void run() {
if(indikator != 0)
{
try {
URL myURL = new URL(url);
URLConnection myURLConnection = myURL.openConnection();
myURLConnection.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(myURLConnection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
catch (MalformedURLException e) {
// new URL() failed
// ...
}
catch (IOException e) {
// openConnection() failed
// ...
}
}
indikator=1;
handler.postDelayed(this, 180000);
}
};
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
final LocationManager m=(LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
String locationprovider =m.getBestProvider(criteria,true);
LocationListener l=new LocationListener() {
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
}
@Override
public void onProviderDisabled(String arg0) {
}
@Override
public void onLocationChanged(Location arg0) {
String zauzetost=new String();
zauzetost="ZAUZET";
String format="MM/dd/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.US);
String date= sdf.format(new Date());
t.setToNow();
String longitude=new String();
String latitude=new String();
String speed=new String();
if((int)arg0.getSpeed()==0)
{
speed="0";
}
else
{
speed=""+(((int)arg0.getSpeed()*3600)/1000);
}
longitude=""+ arg0.getLongitude();
latitude=""+ arg0.getLatitude();
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
mydate=mydate.replaceAll("\\s","");
url="http://www.compensatemeonline.com/truckmeonline/TruckMeOnline/S.php?id="+mojsadrzaj+"&a="+longitude+"&b="+latitude+"&c="+speed+"&d="+mydate+"&e="+date+"&f="+zauzetost;
Log.e("Poruka", url);
// TODO Auto-generated method stub
}
};
runnable.run();
m.requestLocationUpdates(locationprovider, 180000, 0, l);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onStart(Intent intent, int startId) {
//Toast.makeText(this, "Service started onstart...",Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
super.onDestroy();
final SQLiteDatabase db=openOrCreateDatabase("TruckMe", MODE_PRIVATE, null);
db.execSQL("drop table vozac;");
}
}
由于我不想让我的服务永远休眠,我应该将m.requestLocationUpdates(locationprovider,180000,0,l)减少到60000,把所有内容从调用gps从oncreate到onstartcommant,或者我应该做什么。< / p>