【问题标题】:How can I stop a NotificationService from another class?如何停止另一个类的 NotificationService?
【发布时间】:2017-06-11 20:31:24
【问题描述】:

我有NotificationService 类扩展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;
}

【问题讨论】:

    标签: java android android-activity service


    【解决方案1】:

    这样试试

    @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;
    }
    

    【讨论】:

      【解决方案2】:

      这可能不是最好的方法,但我使用公共静态变量作为 IntentService 的标志。如果设置了标志,则执行任何必要的清理、停止线程、退出循环,并让它自然地流向 stopSelf() 和 stopForeground()(你已经拥有)。

      How to check if a service is running on Android?

      【讨论】:

      • 我该如何使用这个标志?我想从另一个类停止我当前的服务,而不是 NotificationService 类
      • 你能把代码贴出来吗?我把这个静态变量放在哪里?我无法停止我在其他班级的服务,请帮帮我
      • 我更新了答案,请看其他用户已经提供的例子:How to Check if a Service is Running on Android
      • 好的,但是我现在服务启动了,但是我不知道如何杀死它
      • 来自doc,它说“一个启动的服务必须管理自己的生命周期......服务必须通过调用 stopSelf() 来停止自己,或者另一个组件可以通过调用 stopService() 来停止它”如果您使用布尔值禁用另一个类的服务,那么对于服务中的任何循环 - 测试布尔值是否为真并跳出循环,然后 stopForeground() 和 stopSelf() 将停止服务。
      猜你喜欢
      • 1970-01-01
      • 2014-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多