如何防止服务应用程序在启动时启动其 ui activity?

How do I prevent a service app from launching its ui activity when it starts?

我想从移动应用向穿戴应用发送一条消息,当穿戴应用收到它时,它会显示一些 UI activity.

我已准备就绪并为此工作(移动应用程序正在使用 Wearable.MessageApi.sendMessage 并且穿戴式应用程序正在扩展 WearableListenerService)。但是我的问题是,当我 运行 wear 应用程序启动时出现 UI,我应该更改什么以便在 wear 应用程序收到来自手机的消息之前不显示任何内容?如何防止在应用程序启动时创建 Activity?

清单:

   <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.DeviceDefault" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".WearMessageListenerService">
            <intent-filter>
                <action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
            </intent-filter>
        </service>
    </application>

Activity:

public class MainActivity extends Activity {
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i("WEAR", "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
        stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
            @Override
            public void onLayoutInflated(WatchViewStub stub) {
                mTextView = (TextView) stub.findViewById(R.id.text);
            }
        });
    }
}

服务:

public class WearMessageListenerService extends WearableListenerService {
    private static final String START_ACTIVITY = "/start_activity";
    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        Log.i("WEAR", "WearableListenerService:onMessageReceived");
        if( messageEvent.getPath().equalsIgnoreCase( START_ACTIVITY ) ) {
            Intent intent = new Intent( this, MainActivity.class );
            intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
            startActivity( intent );
        } else {
            super.onMessageReceived(messageEvent);
        }
    }
}

不确定这是否是执行此操作的好方法,但您可以从您的服务中发送额外的意向。在 Activity 的 onCreate 方法上,您可以检查是否收到任何额外信息,如果没有,您的应用程序作为启动器启动 Activity 然后您可以使用 finish() 销毁 Activity, 否则如果你收到额外的,这意味着电话来自服务,你显示你必须显示的内容。

清单中的更改

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

您已在主 Activity 中的 intent-filter 中设置了 LAUNCHER 类别,这意味着此 activity 可以从启动器启动 - 在 Android在 按下表盘 启动“立即讲话”屏幕并向下滚动到 开始.. .。你会在那里看到一个 "launchable" 应用程序列表,从你的描述来看,这也是你想要避免的。

通过删除此 intent-filter,您可以确保用户无法手动启动此 Activity,因此启动它的唯一方法是直接从您的代码 "when the wear app receives the message from the mobile" .

您的最终清单应如下所示:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.DeviceDefault" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
    </activity>
    <service android:name=".WearMessageListenerService">
        <intent-filter>
            <action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
        </intent-filter>
    </service>
</application>

配置更改

Android Studio 将不允许您在没有 "default Activity" 的情况下启动应用程序(如果您之前已经定义了它)。您需要点击 "Edit Configuration",然后点击您的 "wear" 模块和 "General" 选项卡中的 select "Do not launch activity"。