Search code examples
androidservicewear-os

Android start activity from WearableListenerService


I use a WearableListenerService on my handheld to start an activity after a message from the wear is received. I tried all different kinds of Intent Flags and other options, none of them worked really. All I get, when the activity should start is a

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

My Listener (foreground-service):

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>

So what is wrong with the code?

Thanks in advance and sorry for bad code...


Solution

  • I had the same problem here for weeks.

    Finally I found that the problem is with my Xioami device.

    Maybe you have the same problem...

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