我是android新手。我正在使用报警管理器。当用户按下后退按钮时,我想停止警报管理器并停止使用GPSService。我尝试了这个,但我的应用程序也在后台运行。这是我的代码。请帮帮我
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);
}
}
}
答案 0 :(得分:2)
下面:
PendingIntent pendingUpdateIntent = PendingIntent.getService(
MainActivity2.this, 0, updateServiceIntent, 0);
PendingIntent.getService
方法检索将启动服务的PendingIntent
但是AlarmReceiver
正在延长BroadcastReceiver
所以需要使用PendingIntent.getBroadcast
来检索将执行广播的PendingIntent
像:
PendingIntent pendingUpdateIntent = PendingIntent.getBroadcast(
MainActivity2.this, 0, updateServiceIntent, 0);