Start Activity From Service When Arrived 消息来自手表

Start Activity From Service When Arrived Message From Watches

我正在编写一个适用于 Wear OS 的应用程序。有必要在手表上启动应用程序时,应用程序也立即在 phone 上启动。手表上的启动命令成功到达,但是phone上的应用程序没有启动。尝试使用 Intent 过滤器启动:

    if (messageEvent?.path.equals(START_ACTIVITY_ON_PHONE_PATH)) {
        val startIntent = Intent("com.example.app.gui.activities.SplashActivity")
        startIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        startActivity(startIntent)
    }

在清单中:

   <activity
        android:name="com.example.app.gui.activities.SplashActivity"
        android:noHistory="true"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.SplashActivity"
        android:windowSoftInputMode="stateHidden|stateAlwaysHidden">
        <intent-filter>
            <action android:name="com.example.app.gui.activities.SplashActivity" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

还尝试使用显式调用 SplashActivity:

   if (messageEvent?.path.equals(START_ACTIVITY_ON_PHONE_PATH)) {
        val startIntent = Intent(this,SplashActivity::class.java)
        startIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        startActivity(startIntent)
    }

带有路径的部分正在运行并且总是在日志中看到:

I/Timeline: Timeline: Activity_launch_request time:3649351

调用 Activity 的服务必须在前台启动并设置权限 FOREGROUND_SERVICE 清单:

    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

从 wears 监听事件的服务:

    <service
        android:name="com.example.app.services.ListenerServiceFromWear"
        android:enabled="true"
        android:exported="true"
        android:process=":wearlistener">
        <intent-filter>
            <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
            <data
                android:host="*"
                android:pathPrefix="/start-activity-on-phone"
                android:scheme="wear" />
        </intent-filter>
    </service>

在启动服务的receiver中需要添加:

    val startServiceIntent = Intent(context, ListenerServiceFromWear::class.java)
    context.startForegroundService(startServiceIntent)