Android InstantApp:前台服务

编程入门 行业动态 更新时间:2024-10-17 23:31:48
本文介绍了Android InstantApp:前台服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

无法在 InstantApp 功能模块中使用前台服务.低于运行时安全异常.

Unable to use foreground service inside InstantApp feature module. Getting below runtime security exception.

java.lang.RuntimeException:无法启动活动ComponentInfo{..XYZActivity}:java.lang.SecurityException:方法类 android.app.ActivityManagerProxy.getServices 不可用于免安装应用

java.lang.RuntimeException: Unable to start activity ComponentInfo{..XYZActivity}: java.lang.SecurityException: Method class android.app.ActivityManagerProxy.getServices not available to instant apps

Android 文档说,

Android document says,

受限功能:在用户不知情的情况下在设备上运行.前台服务可用.免安装应用只能通过支持应用链接的活动启动,因此服务、内容提供商或广播接收器将无法启动您的应用.

Restricted features: Run on the device without users being aware. Foreground services are available. Instant apps can only be started through activities that support App Links, so services, content providers or broadcast receivers won't be able to start your app.

代码:

// Starting service getAppContext().startService(new Intent(getAppContext(), FirebaseAuthService.class)); // Foreground service class public class FirebaseAuthService extends Service { private static final String TAG = "FirebaseAuthService"; private boolean isRunning = false; private String mUserId; private FirebaseAuth mAuth; @Override public void onCreate() { Log.d(TAG, "Service onCreate"); startForeground(); isRunning = true; } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i(TAG, "Service onStartCommand"); new Thread(new Runnable() { @Override public void run() { myTask(); } }).start(); return Service.START_STICKY; } @Override public IBinder onBind(Intent arg0) { Log.i(TAG, "Service onBind"); return null; } @Override public void onDestroy() { isRunning = false; Log.i(TAG, "Service onDestroy"); } private void startForeground() { Intent notificationIntent = new Intent(this, HomeActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); Notification notification = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.noti_logo) .setContentTitle("Title") .setContentText("Preparing...") .setContentIntent(pendingIntent).build(); startForeground(1337, notification); } private void myTask() { // At end // Stop service once it finishes its task stopSelf(); } }

推荐答案

您的代码是正确的,但由于 Instant Apps 主管中的一个已知问题,前台服务目前无法运行.

Your code is correct but Foreground services are not working at the moment due to a known issue in Instant Apps supervisor.

更多推荐

Android InstantApp:前台服务

本文发布于:2023-11-27 07:56:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637181.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:前台   Android   InstantApp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!