Java stopService()方法不会停止服务
我正在学习前台服务,我创建了一个通知通道,以确保它保持服务的活动状态。问题是,即使我调用stopService方法,应用程序也不会停止。通知确实消失了,但在我手机的running Apps(运行应用程序)页面中,应用程序仍显示为正在运行 在这个特定的应用程序中,我创建了一个服务,它可以烤出随机数,直到服务运行为止。问题是当我按下按钮停止时,它会删除通知。但是祝酒会持续不断,应用程序在运行应用程序页面中显示正在运行。这是密码 MainActivity.classJava stopService()方法不会停止服务,java,android,service,Java,Android,Service,我正在学习前台服务,我创建了一个通知通道,以确保它保持服务的活动状态。问题是,即使我调用stopService方法,应用程序也不会停止。通知确实消失了,但在我手机的running Apps(运行应用程序)页面中,应用程序仍显示为正在运行 在这个特定的应用程序中,我创建了一个服务,它可以烤出随机数,直到服务运行为止。问题是当我按下按钮停止时,它会删除通知。但是祝酒会持续不断,应用程序在运行应用程序页面中显示正在运行。这是密码 MainActivity.class import androidx.a
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);
}
}
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;
}
}
MyService.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);
}
}
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);
}
}
}
您正在停止服务,但线程仍在执行,您需要停止倒计时,也许这会起作用。我建议您在destroy调用
timer.cancel()中分配一个全局变量CountDownTimer-timer
代码>
•尝试调用stopSelf()代码>明白了。我不知道倒计时计时器运行在不同的线程上。