gpt4 book ai didi

java - stopService() 方法不会停止服务

转载 作者:行者123 更新时间:2023-12-02 08:41:47 27 4
gpt4 key购买 nike

我正在学习前台服务,并创建了一个通知 channel 以确保它保持服务处于 Activity 状态。问题是,即使我调用 stopService 方法,应用程序也不会停止。通知确实消失了,但应用程序仍然显示在我手机的“正在运行的应用程序”页面中正在运行。

在这个特定的应用程序中,我创建了一个服务,该服务在服务运行之前会抛出随机数。问题是,当我按下按钮停止时,它会删除通知。但 Toast 会持续不断,并且应用程序在“正在运行的应用程序”页面中显示为正在进行中。这是代码。

MainActivity.class

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

TextView timerTextView;
private Button startButton;
private Button stopButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

timerTextView = findViewById(R.id.timerTextView);
startButton = findViewById(R.id.startButton);
stopButton = findViewById(R.id.stopButton);

startButton.setOnClickListener(this);
stopButton.setOnClickListener(this);
}

@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MyService.class);

if(v == startButton)
ContextCompat.startForegroundService(this, intent);

if (v == stopButton)
stopService(intent);
}
}

MyService.class

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import java.util.Random;

import static com.example.servicespractice.NotificationClass.CHANNEL_ID;

public class MyService extends Service {

Random randomGenerator;


@Override
public void onCreate() {
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

Intent notificationIntent = new Intent(this, MainActivity.class);
final PendingIntent pendingIntent = PendingIntent.getActivity(this,0, notificationIntent, 0);

Notification notification = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
.setContentTitle("Example Service")
.setContentText("Countdown In Progress")
.setSmallIcon(R.drawable.ic_android)
.setContentIntent(pendingIntent)
.build();

new CountDownTimer(60000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
randomGenerator = new Random();
Toast.makeText(getApplicationContext(), Integer.toString(randomGenerator.nextInt(1000)), Toast.LENGTH_SHORT).show();
}

@Override
public void onFinish() {
Toast.makeText(getApplicationContext(), "All Numbers Generated", Toast.LENGTH_SHORT).show();
onDestroy();
}
}.start();

startForeground(1, notification);
return START_NOT_STICKY;
}

@Override
public void onDestroy() {
super.onDestroy();
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}

NotificationClass.java

import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;

public class NotificationClass extends Application {
public static final String CHANNEL_ID = "exampleServiceChannel";

public void onCreate() {
super.onCreate();
createNotificationChannel();
}

public void createNotificationChannel(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){

NotificationChannel notificationChannel = new NotificationChannel(
CHANNEL_ID,
"Example Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
);

NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(notificationChannel);
}
}

}

最佳答案

您正在停止服务,但线程仍在执行,您需要停止CountDownTimer,也许这会起作用。我建议您分配一个全局变量CountDownTimer计时器并在销毁调用timer.cancel();

• 尝试调用stopSelf();

关于java - stopService() 方法不会停止服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61347233/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com