Witam
Próbuję stworzyć aplikację, w której Service będzie miało za zadanie otworzenie aplikacji jeśli ta została zminimalizowana/usunięta z ostatnio używanych. Jedyne rozwiązanie jakie znalazłem na intrenecie to użyć startService(Intent), jednak ono nie działa. Pozostałe instrukcje w Service działają poprawnie. Ważne wspomnienia jest także, że Service poprawnie przekierowuje do docelowego Activity ale tylko gdy aplikacja jest włączona.
Service:
public class AlarmService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
//Toast.makeText(this, "Service", Toast.LENGTH_SHORT).show();
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("AAAAAAA","Service");
Toast.makeText(this, "Service", Toast.LENGTH_SHORT).show();
Intent intent1 = new Intent(getApplicationContext(),MainActivity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent1);
return super.onStartCommand(intent, flags, startId);
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alarmclock">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<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">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".BroadcastReciver"/>
<service android:name=".AlarmService"
android:exported="true"
android:enabled="true"/>
</application>
</manifest>