我想在 Android 中创建一个服务来启动一个长时间的活动,但它崩溃并显示以下消息:
android.app.ForegroundServiceDidNotStartInTimeException:Context.startForegroundService()然后没有调用Service.startForeground():ServiceRecord {2134ac4 u0 com.example.foregroundservice / .MyForegroundService} 在 android.app.ActivityThread.throwRemoteServiceException(ActivityThread.java:1923) 在 android.app.ActivityThread.access 2700 美元(ActivityThread.java:247) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2148) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loopOnce(Looper.java:201) 在 android.os.Looper.loop(Looper.java:288) 在 android.app.ActivityThread.main(ActivityThread.java:7839) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
这是我的主要活动:
@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;
}
而且是前台服务
@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);
}
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());
}
我该怎么做才能解决这个问题?
在您的
Service.onStartCommand()
中,您创建了一个 Notification
,但您从未使用过它。您需要致电 startForeground()
并传递您的 Notification
。如果 Activity
调用 startForegroundService()
,则 Android 预计 Service
将在 startForeground()
内调用 onStartCommand()
。