为什么我不';无法从Android O上的startService获取iligal状态表达式
据此, 在0:56中,这意味着您的应用程序处于后台时无法启动服务 它将导致为什么我不';无法从Android O上的startService获取iligal状态表达式,android,service,foreground-service,Android,Service,Foreground Service,据此, 在0:56中,这意味着您的应用程序处于后台时无法启动服务 它将导致java.lang.IllegalStateException:不允许启动服务Intent。但是,当我尝试在桌面上启动服务时,或者当应用程序在后台时,它不会崩溃,我希望它会崩溃。我使用的是Pixel2API27模拟器 主课 switch (view.getId()) { case R.id.button: Log.d(TAG, "onClick: start "); intent =
java.lang.IllegalStateException:不允许启动服务Intent
。但是,当我尝试在桌面上启动服务时,或者当应用程序在后台时,它不会崩溃,我希望它会崩溃。我使用的是Pixel2API27模拟器
主课
switch (view.getId()) {
case R.id.button:
Log.d(TAG, "onClick: start ");
intent =new Intent(this,MyService.class);
intent.putExtra("key123", editText.getText().toString());
startService(intent);
break;
case R.id.button2:
Log.d(TAG, "onClick: stop");
stopService(intent);
break;
}
MyService.class
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand: ");
String input=intent.getStringExtra("key123");
Intent notificationIntent=new Intent(this,MainActivity.class);
PendingIntent pendingIntent= PendingIntent.getActivity(this,0,notificationIntent,0);
Notification notification= new NotificationCompat.Builder(this,CHANNEL_ID)
.setContentTitle("TITLE").setContentText(input)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_drive_eta_black_24dp)
.build();
startForeground(1,notification);
return START_NOT_STICKY;
}
您正在使用前台服务
只有当您尝试在后台启动后台服务时,才会获得IllegalStateException