Questions tagged «background-service»

8
升级到Android 8.1后,startForeground失败
将手机升级到8.1 Developer Preview之后,我的后台服务无法正常启动。 在长期运行的服务中,我实现了startForeground方法来启动正在进行的通知,该通知在create时被调用。 @TargetApi(Build.VERSION_CODES.O) private fun startForeground() { // Safe call, handled by compat lib. val notificationBuilder = NotificationCompat.Builder(this, DEFAULT_CHANNEL_ID) val notification = notificationBuilder.setOngoing(true) .setSmallIcon(R.drawable.ic_launcher_foreground) .build() startForeground(101, notification) } 错误信息: 11-28 11:47:53.349 24704-24704/$PACKAGE_NAMEE/AndroidRuntime: FATAL EXCEPTION: main Process: $PACKAGE_NAME, PID: 24704 android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for …

5
如何从后台服务和更新UI更新ViewModel的LiveData
最近,我正在探索Google最近引入的Android体系结构。从文档中我发现: public class MyViewModel extends ViewModel { private MutableLiveData<List<User>> users; public LiveData<List<User>> getUsers() { if (users == null) { users = new MutableLiveData<List<Users>>(); loadUsers(); } return users; } private void loadUsers() { // do async operation to fetch users } } 活动可以访问此列表,如下所示: public class MyActivity extends AppCompatActivity { public void …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.