我通过该警报管理器拥有警报管理器,我正在启动一项服务,但是当我停止该警报管理器时,该服务的 onDestroy 方法永远不会被调用。
我的代码:
启动服务
//----------------------------------------------------------------------------------------
//Starting Alarm Manager for Lat Lng
//----------------------------------------------------------------------------------------
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*1, pendingIntent);
//----------------------------------------------------------------------------------------
//Starting Alarm Manager for Lat Lng
//----------------------------------------------------------------------------------------
停止服务
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
我的服务
public class MyServiceForLatLng extends Service {
double nlat;
double nlng;
double glat;
double glng;
LocationManager glocManager;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onStart(Intent intent, int startId) {
LocationManager nlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener nlocListener = new MyLocationListenerNetWork();
nlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
1,
1,
nlocListener);
glocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener glocListener = new MyLocationListenerGPS();
glocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
1,
1,
glocListener);
super.onStart(intent, startId);
}
@Override
public void onDestroy() {
Log.d("destroyed", "On destroyed called");
super.onDestroy();
}
public class MyLocationListenerNetWork implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
nlat = loc.getLatitude();
nlng = loc.getLongitude();
saveInPreference("nlat",nlat+"");
saveInPreference("nlng",nlng+"");
Log.d("LAT & LNG Network:", nlat + " " + nlng);
}
@Override
public void onProviderDisabled(String provider)
{
Log.d("LOG", "Network is OFF!");
}
@Override
public void onProviderEnabled(String provider)
{
Log.d("LOG", "Thanks for enabling Network !");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
public class MyLocationListenerGPS implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
glat = loc.getLatitude();
glng = loc.getLongitude();
saveInPreference("glat",glat+"");
saveInPreference("glng",glng+"");
Log.d("LAT & LNG GPS:", glat + " " + glng);
}
@Override
public void onProviderDisabled(String provider)
{
Log.d("LOG", "GPS is OFF!");
}
@Override
public void onProviderEnabled(String provider)
{
Log.d("LOG", "Thanks for enabling GPS !");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
//===================================================================================================================================
//Preference variable
//===================================================================================================================================
//--------------------------------------------
// method to save variable in preference
//--------------------------------------------
public void saveInPreference(String name, String content) {
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(name, content);
editor.commit();
}
//--------------------------------------------
// getting content from preferences
//--------------------------------------------
public String getFromPreference(String variable_name) {
String preference_return;
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(this);
preference_return = preferences.getString(variable_name, "");
return preference_return;
}
//===================================================================================================================================
//Preference variable
//===================================================================================================================================
}