2015-12-01 52 views
0

我是新来的机器人。我正在使用闹钟管理器。我想停止报警管理器,并在用户按下后退按钮时停止使用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); 
    } 

} 

} 
+0

我相信你正在创建警报管理器的两个实例。一个人再次压抑,这是你正在取消的那个人。您需要取消您在startAlarm()函数中创建的实例 – Dhina

+0

@DhinakaranThennarasu谢谢...以及gpsservice – Priyanka

回答

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); 
+0

感谢此工作,但GPSService不停止。 – Priyanka

+0

@Priyanka:你怎么知道'GPSService不停止',因为你没有在代码中调用startService? –