Java 如何停止另一个类的NotificationService?
我有Java 如何停止另一个类的NotificationService?,java,android,android-activity,service,Java,Android,Android Activity,Service,我有NotificationService类扩展Service并且我启动了Service whit方法,但是我不知道如何停止当前的服务,我阅读了一些指南和示例,但是使用stopService()方法我无法停止我的服务。这是我班的代码,我怎么能停下来 public class NotificationService extends Service { private final long TIME_WAKE_UP = 6000;//60 * 60 * 1000; private long tim
NotificationService
类扩展Service
并且我启动了Service whit方法,但是我不知道如何停止当前的服务,我阅读了一些指南和示例,但是使用stopService()
方法我无法停止我的服务。这是我班的代码,我怎么能停下来
public class NotificationService extends Service {
private final long TIME_WAKE_UP = 6000;//60 * 60 * 1000;
private long timeStart;
private Esercizio es;
@Override
public void onCreate() {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
Notification mNt = mBuilder.build();
startForeground(2, mNt);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
timeStart = intent.getExtras().getLong("timeStart");
es = (Esercizio) intent.getExtras().get("esercizio");
while(System.currentTimeMillis() < timeStart + TIME_WAKE_UP);
atWork();
stopForeground(true);
stopSelf();
return START_STICKY;
}
private void atWork() {
Intent intent = new Intent(this, TransitionArchive.class);
Bundle bundle = new Bundle();
bundle.putString("notificationService","");
bundle.putString("KEY_EXCERSIZE",es.getNameEx());
intent.putExtras(bundle);
NotificationCompat.Builder mBuilderOffline =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo_app_android)
.setContentTitle(es.getNameEx())
.setContentText("C'è un nuovo contenuto per te!")
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_MAX)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
1,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilderOffline.setContentIntent(pendingIntent);
//PendingIntent call the receive class
Notification note = mBuilderOffline.build();
note.defaults |= Notification.DEFAULT_SOUND;
note.defaults |= Notification.DEFAULT_VIBRATE;
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, note);
}
public static void startNotificationService(Context mContext, Esercizio es) {
Intent mIntent = new Intent(mContext, NotificationService.class);
mIntent.putExtra("esercizio", es);
mIntent.putExtra("timeStart", System.currentTimeMillis());
startWakefulService(mContext, mIntent);
}
@Override
@Nullable
public IBinder onBind(Intent intent) {
return null;
}
公共类NotificationService扩展服务{
私人最终长时间唤醒=6000;//60*60*1000;
私人长时间启动;
私营企业;
@凌驾
public void onCreate(){
NotificationCompat.Builder mBuilder=新的NotificationCompat.Builder(此);
通知mNt=mBuilder.build();
起始地面(2,mNt);
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
timeStart=intent.getExtras().getLong(“timeStart”);
es=(Esercizio)intent.getExtras().get(“Esercizio”);
while(System.currentTimeMillis()
这可能不是最好的方法,但我使用了一个公共静态变量作为IntentService的标志。如果设置了该标志,则执行任何必要的清理、停止线程、退出循环,并让它自然地流到stopSelf()和stopForeground()(您已经设置了)
像这样试试
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
timeStart = intent.getExtras().getLong("timeStart");
es = (Esercizio) intent.getExtras().get("esercizio");
while(System.currentTimeMillis() < timeStart + TIME_WAKE_UP);
atWork();
stopForeground(true);
stopSelf();
return START_NOT_STICKY;
}
@覆盖
公共int onStartCommand(Intent Intent、int标志、int startId){
timeStart=intent.getExtras().getLong(“timeStart”);
es=(Esercizio)intent.getExtras().get(“Esercizio”);
while(System.currentTimeMillis()
我如何使用这个标志?我想从另一个类停止我当前的服务,而不是NotificationService类。你可以发布代码吗?我把这个静态变量放在哪里?我不能从另一个类停止我的服务,请帮我更新答案,请看其他用户已经提供的示例:。好的,但我现在当服务ice已启动,但我不知道如何从中终止它,它说“已启动的服务必须管理其自身的生命周期…该服务必须通过调用stopSelf()来停止自身,或者其他组件可以通过调用stopService()来停止它。”如果您使用布尔值禁用另一个类中的服务,那么对于服务中的任何循环——测试布尔值是否为true并中断循环,然后停止前台()和停止自我()将停止服务。