I want to create a Service in Android to start a long activity but it crashes with the following message shown:
android.app.ForegroundServiceDidNotStartInTimeException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{2134ac4 u0 com.example.foregroundservice/.MyForegroundService} > at android.app.ActivityThread.throwRemoteServiceException(ActivityThread.java:1923)
at android.app.ActivityThread.access$2700(ActivityThread.java:247)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2148)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Here is my MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (!foregroundServiceRunning())
{
Intent serviceIntent = new Intent(this, MyForegroundService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(serviceIntent);
}
}
}
public void getTimeButtonPressed(View v)
{
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(MyForegroundService.getsystemtime());
}
public boolean foregroundServiceRunning()
{
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service: activityManager.getRunningServices(Integer.MAX_VALUE))
{
if(MyForegroundService.class.getName().equals(service.service.getClassName()))
{
return true;
}
}
return false;
}
And it is the foreground service
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
new Thread(
new Runnable()
{
@Override
public void run()
{
while (true)
{
Log.e("Service", "Service is running...");
try
{
Thread.sleep(2000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
).start();
final String CHANNELID ="Foreground Service ID";
NotificationChannel channel = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
channel = new NotificationChannel(CHANNELID, CHANNELID, NotificationManager.IMPORTANCE_LOW);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
getSystemService(NotificationManager.class).createNotificationChannel(channel);
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
Notification.Builder notification = new Notification.Builder(this, CHANNELID)
.setContentText("Service is running...")
.setContentTitle("Service enabled")
.setSmallIcon(R.drawable.ic_launcher_background);
}
startForeground(1001, notification.build());
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public static String getsystemtime()
{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("ss:mm:hh dd/MM/yyyy", Locale.US);
return simpleDateFormat.format(new Date());
}
What I shall I do to fix the problem?
Edit
I add a new line to the onStartCommand. A strange thing happens: it runs smoothly on my emulator but it crashes immediately when running on my phone (redmi 10) with the following messages shown: