前台服务在Oreo(Android 8.1.0)上不起作用
我正在制作一个有两个活动的应用程序,其中一个是主要活动。当应用程序启动时,它会创建一个前台服务,为用户注册一个广播侦听器。当广播侦听器获得广播时,它触发第二个活动。我的代码面临两个问题: 1) 前台服务通知未发出。但是,该服务仍在运行。 2) 当我的应用程序是手机上的活动应用程序时,广播侦听器工作正常。但是如果我的应用程序在后台,广播侦听器就不能工作。但是服务正在运行,因为日志中有消息说它已被终止/停止 如何解决这两个问题 我正在使用安卓8.1.0的Oppo CPH1859手机上运行该应用程序 Android.manifest +++++++++++++前台服务在Oreo(Android 8.1.0)上不起作用,android,android-8.0-oreo,foreground-service,Android,Android 8.0 Oreo,Foreground Service,我正在制作一个有两个活动的应用程序,其中一个是主要活动。当应用程序启动时,它会创建一个前台服务,为用户注册一个广播侦听器。当广播侦听器获得广播时,它触发第二个活动。我的代码面临两个问题: 1) 前台服务通知未发出。但是,该服务仍在运行。 2) 当我的应用程序是手机上的活动应用程序时,广播侦听器工作正常。但是如果我的应用程序在后台,广播侦听器就不能工作。但是服务正在运行,因为日志中有消息说它已被终止/停止 如何解决这两个问题 我正在使用安卓8.1.0的Oppo CPH1859手机上运行该应用程序
<uses-sdk android:minSdkVersion="26" />
<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">
<service
android:name=".MyFGService"
android:enabled="true"
android:exported="true"></service>
<activity
android:name=".DisplayMessageActivity"
android:parentActivityName=".MainActivity">
<!-- The meta-data tag is required if you support API level 15 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
++++++++++++
<uses-sdk android:minSdkVersion="26" />
<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">
<service
android:name=".MyFGService"
android:enabled="true"
android:exported="true"></service>
<activity
android:name=".DisplayMessageActivity"
android:parentActivityName=".MainActivity">
<!-- The meta-data tag is required if you support API level 15 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
在MyFG服务中
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service (re)starting", Toast.LENGTH_SHORT).show();
Log.v("FIRST APP", "Service (re)starting");
// If we get killed, after returning from here, restart
return START_STICKY;
}
private String createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "TEST Name";
String description = "TEST Description";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel( "FirstAppNotifChannel", name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
return channel.getId();
}
return null;
}
@Override
public void onCreate(){
super.onCreate();
Log.v("FIRST APP", "Service created");
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, notificationIntent, 0);
String notificationChannelID = createNotificationChannel();
Log.v("FIRST APP","notification channel ID =" + notificationChannelID);
Notification myNotification = new Notification.Builder(this, notificationChannelID)
.setContentTitle("TEST Title")
.setContentText("TEST Content")
.setContentIntent(pendingIntent)
.build();
startForeground(101, myNotification);
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_USER_PRESENT);
registerReceiver(mReciever, filter);
}
@Override
public void onDestroy() {
Toast.makeText(this, "service destroyed", Toast.LENGTH_SHORT).show();
Log.v("FIRST APP", "Service destroyed");
unregisterReceiver(mReciever);
}