Android начать деятельность с WearableListenerService - PullRequest
0 голосов
/ 21 января 2020

Я использую сервис WearableListenerService на своем контроллере, чтобы начать работу после получения сообщения об износе. Я перепробовал различные типы флагов и другие варианты, но ни один из них не работал. Все, что я получаю, когда начинается действие, это

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

Мой слушатель (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);
    }
}
}

Манифест. 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>

Так что не так с кодом?

Заранее спасибо и извините за плохой код ...

Ответы [ 2 ]

0 голосов
/ 06 февраля 2020

У меня была такая же проблема вот уже несколько недель.

Наконец-то я обнаружил, что проблема в моем устройстве Xioami.

Может быть, у вас та же проблема ...

MIUI 10 не позволяет запускать службу - Xiaomi Redmi Note

0 голосов
/ 21 января 2020

Попробуйте заменить следующий код:

@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);
}
...