【问题标题】:Android InstantApp: Foreground serviceAndroid InstantApp:前台服务
【发布时间】:2017-06-20 22:49:30
【问题描述】:

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

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

Android 文档说,

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

代码:

// 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();
    }
}

【问题讨论】:

  • 快速问题:您能否确认从 PendingIntent.getActivity 返回的待处理意图不为空?并不是说它与您遇到的问题直接相关,但我看到没有创建待处理的意图。

标签: android service android-instant-apps foreground-service


【解决方案1】:

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

【讨论】:

  • 你有票号吗?
  • 很遗憾没有公开显示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-11
  • 1970-01-01
  • 1970-01-01
  • 2020-06-05
  • 2014-05-25
  • 2023-03-28
  • 2011-09-15
相关资源
最近更新 更多