加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 在Activity中启动前台服务

发布时间:2020-12-15 04:23:57 所属栏目:Java 来源:网络整理
导读:如何在Activity中启动服务? 我试着打电话: Notification notification = new Notification(R.drawable.ic_launcher,getText(R.string.app_name),System.currentTimeMillis()); Intent notificationIntent = new Intent(this,MainActivity.class); PendingI
如何在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,notificationIntent,0);
    notification.setLatestEventInfo(this,pendingIntent);
    startForeground(1,notification);

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

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

解决方法

How can I start a service in Activity?

调用startService().

However,it “Cannot resolve method ‘startforeground(int,android.app.Notification)'”

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读