【问题标题】:Android start activity from WearableListenerServiceAndroid 从 WearableListenerService 开始活动
【发布时间】:2020-02-06 05:15:39
【问题描述】:

收到来自 Wear 的消息后,我在手持设备上使用 Wea​​rableListenerService 启动活动。 我尝试了所有不同类型的 Intent Flags 和其他选项,但没有一个真正起作用。我得到的只是活动应该开始的时间是一个

I/Timeline: Timeline: Activity_launch_request time:54107858 intent:Intent

我的听众(前台服务):

public class MessageService extends WearableListenerService {

private static final int ID_SERVICE = 101;

@Override
public void onCreate() {
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    Intent notiIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notiIntent, 0);
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String channelId = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? createNotificationChannel(notificationManager) : "";
    Notification notification = new NotificationCompat.Builder(this, channelId)
            .setContentTitle(getString(R.string.app_name))
            .setContentText("Test")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pendingIntent)
            .build();

    startForeground(1, notification);

    return START_REDELIVER_INTENT;
}

@Override
public void onDestroy() {
    super.onDestroy();

}

@RequiresApi(Build.VERSION_CODES.O)
private String createNotificationChannel(NotificationManager notificationManager){
    String channelId = "my_service_channelid";
    String channelName = "My Foreground Service";
    NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
    // omitted the LED color
    channel.setImportance(NotificationManager.IMPORTANCE_NONE);
    channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
    notificationManager.createNotificationChannel(channel);
    return channelId;
}

@Override
public void onMessageReceived(MessageEvent messageEvent) {
    if (messageEvent.getPath().equals("/wear_control")) {
        Intent i = new Intent(this, MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(i);
    }
    else {
        super.onMessageReceived(messageEvent);
    }
}
}

Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.ronston.wearcontrol">

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:ignore="GoogleAppIndexingWarning">
    <activity
        android:name=".newaction.NewAction"
        android:screenOrientation="portrait"/>

    <service
        android:name=".MessageService"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
            <data
                android:host="*"
                android:pathPrefix="/wear_control"
                android:scheme="wear" />
        </intent-filter>
    </service>

    <activity
        android:name=".MainActivity"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

那么代码有什么问题呢?

提前致谢,抱歉代码错误...

【问题讨论】:

    标签: android service wear-os


    【解决方案1】:

    我在这几个星期都遇到同样的问题。

    最后我发现问题出在我的小米设备上。

    也许你也有同样的问题……

    MIUI 10 doesn't let service start activity - Xiaomi Redmi Note

    【讨论】:

    • 这会更好作为评论作为对OP的问题,然后您可以在确认后添加答案
    【解决方案2】:

    尝试替换以下代码:

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
    if (messageEvent.getPath().equals("/wear_control")) {
        Intent i = new Intent(this, MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(i);
    }
    else {
        super.onMessageReceived(messageEvent);
    }
    

    有了这个:

        @Override
        public void onMessageReceived(MessageEvent messageEvent) {
        Intent i = new Intent(this, MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(i);
    }
    

    【讨论】:

    • 不幸的是不起作用,就像我说的,我尝试了所有这些标准解决方案,同样的“错误”:Timeline: Activity_launch_request time:541773 intent:Intent
    猜你喜欢
    • 2016-01-08
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    • 2015-12-21
    • 1970-01-01
    相关资源
    最近更新 更多