我正在尝试取消之前已注册的alarmmanager
。
我正在注册我的alarmmanager
,如下所示:
Intent i = new Intent(SetProfileOnlineActivity.this, GpsPingService.class);
PendingIntent pendingIntent = PendingIntent.getService(SetProfileOnlineActivity.this, 1545,
i, 0);
alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + 5000, pendingIntent);
我试图使用以下代码取消它:
Intent i = new Intent(SetProfileOnlineActivity.this, GpsPingService.class);
PendingIntent p = PendingIntent.getService(SetProfileOnlineActivity.this, 1545, i, 0);
alarmManager.cancel(p);
但它并没有取消。为了调试它,我把这个代码放在try catch块中,但是它不能捕获任何错误。请让我知道它发生的原因,我该如何解决?
可能是重复但我无法理解this问题中给出的答案和我尝试过的答案之间有什么区别。所以,这不是重复的问题,因为我已经完成了this回答中建议的事情。
我正在使用以下代码块开始我的服务:
AlertDialog.Builder builder = new AlertDialog.Builder(SetProfileOnlineActivity.this);
builder.setTitle("Need More Accuracy?");
builder.setMessage("Hello user, you have set too small area for geofence. May be it will " +
"perform action with delay in time. If you want to get more accuracy in action" +
" then press \"YES\" button. More accuracy will consume more bettery then normal.");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startService(new Intent(SetProfileOnlineActivity.this, GpsPingService.class));
try {
Intent i = new Intent(SetProfileOnlineActivity.this, GpsPingService.class);
PendingIntent pendingIntent = PendingIntent.getService(SetProfileOnlineActivity.this, 1545,
i, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(SetProfileOnlineActivity.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + 5000, pendingIntent);
Log.e("JK-->> ", "Alarmmanager setted!");
} catch (Exception e) {
Log.e("JK-->>", "Error gpsping register -->> " + e.toString());
}
dialog.dismiss();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
try {
Intent i = new Intent(SetProfileOnlineActivity.this, GpsPingService.class);
PendingIntent p = PendingIntent.getService(SetProfileOnlineActivity.this, 1545, i, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(SetProfileOnlineActivity.ALARM_SERVICE);
alarmManager.cancel(p);
stopService(i);
} catch (Exception e) {
Log.e("JK-->>", "Cancel alarmmanager error-->> " + e.getMessage().toString());
}
dialog.dismiss();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
服务类:
public class GpsPingService extends Service {
LocationManager locationManager;
LocationListener locationListener;
FusedLocationProviderClient fusedLocationProviderClient;
LocationRequest locationRequest;
@Override
public void onDestroy() {
super.onDestroy();
Log.e("JK-->>", "Gpsping Service stopped!");
}
@Override
public void onCreate() {
Log.e("JK-->>", "Gpsping Service started!");
}
public GpsPingService() {
Log.e("JK-->>", "constructor");
}
@SuppressLint("MissingPermission")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("JK-->>", "Gpsping Service onstartcommand!");
try {
Intent i = new Intent(this, GpsPingService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 1545,
i, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + 5000, pendingIntent);
locationRequest = new LocationRequest()
.setInterval(1000)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setFastestInterval(1000);
LocationCallback locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
Log.e("JK-->>", "location updated by gpsping service-->> " + locationResult.getLastLocation().getLatitude() +
" " + locationResult.getLastLocation().getLongitude());
Toast.makeText(getApplicationContext(), "Location changed!", Toast.LENGTH_SHORT).show();
}
};
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getApplicationContext());
fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper());
} catch (Exception e) {
Log.e("JK-->>Gpsping-->> ", e.getMessage());
}
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
答案 0 :(得分:0)
尝试创建不同的AlarmManager
;
Intent i = new Intent(SetProfileOnlineActivity.this, GpsPingService.class);
PendingIntent pendingIntent = PendingIntent.getService(SetProfileOnlineActivity.this, 1545,i, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + 5000, pendingIntent);
当你取消它时,你会这样做:
Intent i = new Intent(SetProfileOnlineActivity.this, GpsPingService.class);
PendingIntent p = PendingIntent.getService(SetProfileOnlineActivity.this, 1545, i, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(p);
这样做对我有用。
答案 1 :(得分:0)
我认为你向PendingIntent
提供了错误的标记。根据{{3}}
可以是FLAG_ONE_SHOT,FLAG_NO_CREATE,FLAG_CANCEL_CURRENT,FLAG_UPDATE_CURRENT,FLAG_IMMUTABLE或Intent.fillIn()支持的任何标志,以控制实际发送时可以提供的意图的哪些未指定部分。
创建闹钟时尝试使用PendingIntent.FLAG_UPDATE_CURRENT
,然后取消它。
PendingIntent.getService(SetProfileOnlineActivity.this, 1545, i, PendingIntent.FLAG_UPDATE_CURRENT);
我也看到你从服务本身获得了上下文。检查null
的上下文,或者可能没有运行。