これまでのところ、ContextCompat.startForegroundService(context, intentService);
を使用してサービスを開始するようにコードをアシストしました。この方法では、アンドロイド< 26とアンドロイド26(オレオ)でも動作します。Oreo - フォアグラウンドサービスでフォアグラウンド通知が表示されない
私はまだ違いが見えます。アンドロイド・オレオ私のカスタム・フォアグラウンド通知は表示されません(「アプリはバックグラウンドで実行中」通知のみ表示されます)。私もそこに何かを調整する必要がありますか?
は私のサービスは、次のようになります。
public class BaseOverlayService extends Service {
@Override
public void onCreate() {
super.onCreate();
moveToForeground();
}
private void moveToForeground() {
Notification notification = ...;
super.startForeground(NOTIFICATION_ID, notification);
}
}
公式例
この例(https://github.com/googlesamples/android-play-location/blob/master/LocationUpdatesForegroundService/app/src/main/java/com/google/android/gms/location/sample/locationupdatesforegroundservice/LocationUpdatesService.java#L200が)私は、次の使用する必要があることを、コメントとして示しているが、startServiceInForeground
存在しません...
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) {
NotificationManager mNotificationManager = ((NotificationManager) getSystemService(NOTIFICATION_SERVICE));
mNotificationManager.startServiceInForeground(new Intent(this, BaseOverlayService.class), NOTIFICATION_ID, notification);
} else {
startForeground(NOTIFICATION_ID, notification);
}
私の通知は、今までのAPI < 26とAndroidデバイスの数千人に取り組んでおり、このように作成されます。
protected Notification foregroundNotification(int notificationId)
{
boolean paused = MainApp.getPrefs().sidebarServicePaused();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.icon_not);
builder.setContentIntent(notificationIntent());
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon));
builder.setContentTitle(getString(R.string.derived_app_name));
builder.setContentText(getString(checkStatus() ? (paused ? R.string.service_notification_text_paused : R.string.service_notification_text_running) : R.string.service_notification_text_preparing));
builder.setColor(Color.parseColor("#25baa2"));
if (MainApp.getPrefs().hideNotificationIcon())
builder.setPriority(NotificationCompat.PRIORITY_MIN);
else
builder.setPriority(NotificationCompat.PRIORITY_MAX);
if (paused)
builder.addAction(R.drawable.ic_play_arrow_black_24dp, getString(R.string.resume), resumeIntent(this));
else
builder.addAction(R.drawable.ic_pause_black_24dp, getString(R.string.pause), pauseIntent(this));
return builder.build();
}
のように使用することができ、追加したリンクは、おかげでとにかくかなり良いです。しかし、1つの質問:これは、私は必ずしもフォアグラウンドサービスの通知をアンドロイドオレオで表示する必要はありません、この変更は意味ですか?人々は私のアプリが通知なしでアンドロイド・オレオに取り組んでいると私に言いました... – prom85
eghmmm ...実際には通知なしで動作することはできません。背景や仕事は、活動や通知などの目に見える構成要素がないとアンドロイドで許可されません。可視コンポーネントシステムなしでサービスを実行すると、しばらくしてからサービスが終了します。バックグラウンドジョブをしたい場合は、サービスの通知が必要です。 期間を置いて何かをバックグラウンドで実行する必要がある場合は、[firebase job dispatcher](https://github.com/firebase/firebase-jobdispatcher-android)をチェックする必要があります。スケジュールされたサービスを実行できます。 –
私は 'WindowManager'にUI要素を持つサイドバーアプリケーションを持っています。これは今まで私が使用していた目に見えない通知(見えない通知はチャネルなしのもの)とうまくいくようです。数日後には何の不満もありませんでした。エミュレータは5秒以内にサービスを終了しません – prom85