繁体   English   中英

在活动中启动前台服务

[英]Starting a Foreground Service in Activity

如何在Activity中启动服务?

我试着打电话:

Notification notification = new Notification(R.drawable.ic_launcher, getText(R.string.app_name),System.currentTimeMillis());
    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.setLatestEventInfo(this, getText(R.string.app_name),getText(R.string.app_name), pendingIntent);
    startForeground(1, notification);

但是,它“无法解析方法'startforeground(int,android.app.Notification)'”

如何调用startForeground? 我试过getApplicationContext()无济于事。

如何在Activity中启动服务?

调用startService()

但是,它“无法解析方法'startforeground(int,android.app.Notification)'”

startForeground()Service上的一个方法。 您的Service需要自己调用startForegrond() (例如,在服务的onCreate() )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM