我是新來的機器人。我正在使用鬧鐘管理器。我想停止報警管理器,並在用戶按下後退按鈕時停止使用GPS服務。我試過這個,但我的應用程序也在後臺運行。這是我的代碼。請幫我報警管理器和GPS服務不停止
public class MainActivity2 extends AppCompatActivity {
TextView tv_latitude, tv_longitude;
private Toolbar toolbar;
GoogleMap map;
LatLng dest;
GPSService mGPSService;
private PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
toolbar = (Toolbar) findViewById(R.id.app_bar);
toolbar.setTitle("Welcome");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
tv_latitude = (TextView) findViewById(R.id.latitude);
tv_longitude = (TextView) findViewById(R.id.longitude);
Intent alarmIntent = new Intent(MainActivity2.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity2.this, 0, alarmIntent, 0);
startAlarm();
mGPSService = new GPSService(MainActivity2.this);
mGPSService.getLocation();
if (mGPSService.isLocationAvailable == false) {
// Here you can ask the user to try again, using return; for that
Toast.makeText(MainActivity2.this, "Your location is not available", Toast.LENGTH_SHORT).show();
return;
// Or you can continue without getting the location, remove the return; above and uncomment the line given below
// address = "Location not available";
} else {
// Getting location co-ordinates
double latitude = mGPSService.getLatitude();
double longitude = mGPSService.getLongitude();
tv_longitude.setText("" + longitude);
tv_latitude.setText("" + latitude);
dest = new LatLng(latitude, longitude);
SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
map = supportMapFragment.getMap();
map.setMyLocationEnabled(true);
map.addMarker(new MarkerOptions().position(dest));
map.moveCamera(CameraUpdateFactory.newLatLng(dest));
map.animateCamera(CameraUpdateFactory.zoomTo(18));
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Log.e("Home back pressed", "called");
mGPSService.closeGPS();
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent updateServiceIntent = new Intent(MainActivity2.this, AlarmReceiver.class);
PendingIntent pendingUpdateIntent = PendingIntent.getService(MainActivity2.this, 0, updateServiceIntent, 0);
// Cancel alarms
try {
alarmManager.cancel(pendingUpdateIntent);
} catch (Exception e) {
}
Log.e("Home back pressed end", "called");
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
Log.e("back presed called", "clalles");
mGPSService.closeGPS();
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent updateServiceIntent = new Intent(MainActivity2.this, AlarmReceiver.class);
PendingIntent pendingUpdateIntent = PendingIntent.getService(MainActivity2.this, 0, updateServiceIntent, 0);
// Cancel alarms
try {
alarmManager.cancel(pendingUpdateIntent);
} catch (Exception e) {
}
Log.e("back presed endd", "clalles");
}
public void startAlarm() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
/* Repeating on every 20 minutes interval */
manager.setRepeating(AlarmManager.RTC_WAKEUP, 0,
10 * 60 * 20, pendingIntent);
// Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}
@Override
protected void onResume() {
super.onResume();
// Other onResume() code here
this.registerReceiver(mMessageReceiver, new IntentFilter("some_unique_name"));
}
@Override
protected void onPause() {
super.onPause();
this.unregisterReceiver(mMessageReceiver);
// Other onPause() code here
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
double lat = intent.getDoubleExtra("lat", 0);
double longg = intent.getDoubleExtra("long", 0);
tv_latitude.setText("" + lat);
tv_longitude.setText("" + longg);
Log.e("Broadcast Receiver lat", "" + lat);
Log.e("Broadcast Receiver long", "" + longg);
map.clear();
dest = new LatLng(lat, longg);
SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
map = supportMapFragment.getMap();
map.setMyLocationEnabled(true);
map.addMarker(new MarkerOptions().position(dest));
map.moveCamera(CameraUpdateFactory.newLatLng(dest));
map.animateCamera(CameraUpdateFactory.zoomTo(18));
//update the TextView
// Toast.makeText(context, "Alarm Settttttttt", Toast.LENGTH_SHORT).show();
}
};
}
這AlarmReceiver類
public class AlarmReceiver extends BroadcastReceiver {
GPSService gps;
@Override
public void onReceive(Context context, Intent intent) {
updateYourActivity(context);
}
private void updateYourActivity(Context context) {
Intent intent = new Intent("some_unique_name");
gps = new GPSService(context);
gps.getLocation();
if (gps.isLocationAvailable == false) {
Toast.makeText(context, "Your location is not available, please try again.", Toast.LENGTH_SHORT).show();
return;
} else {
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
intent.putExtra("lat", latitude);
intent.putExtra("long", longitude);
context.sendBroadcast(intent);
}
}
}
我相信你正在創建警報管理器的兩個實例。一個人再次壓抑,這是你正在取消的那個人。您需要取消您在startAlarm()函數中創建的實例 – Dhina
@DhinakaranThennarasu謝謝...以及gpsservice – Priyanka