每次我执行我的应用程序时,都会出现“startForeground的错误通知不良”的错误。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.pennskanvtic, PID: 17447
android.app.RemoteServiceException$CannotPostForegroundServiceNotificationException:
Bad notification for startForeground
at android.app.ActivityThread.throwRemoteServiceException(ActivityThread.java:1978)
at android.app.ActivityThread.-$$Nest$mthrowRemoteServiceException(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2237)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7884)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)`
我的服务只启动一个计时器并显示通知(我尝试在NotificationCompat.Builder中使用setChannelId,但没有任何变化):
public class MyService extends Service {
private int i_NotifID = 1336;
private String CHANNEL_ID = "MyServiceChannel";
private CountDownTimer mCptRebours;
private Notification notification;
private NotificationCompat.Builder builder;
private NotificationManagerCompat notificationManager;
private long l_TimerTime_ms;
private MainActivity mainActivity;
@Override
public void onCreate() {
super.onCreate();
//Resources res = this.getResources();
builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle("PennSkanvTic...Tac...");
builder.setContentText("waiting for start...");
//builder.setChannelId(CHANNEL_ID);
//builder.setOngoing(true);
//builder.setPriority(Notification.PRIORITY_HIGH);
notificationManager = NotificationManagerCompat.from(this);
//notificationManager.notify(i_NotifID, builder.build());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
l_TimerTime_ms = intent.getLongExtra("l_TimerTime_ms", 55000);
builder.setContentText(Long.toString(l_TimerTime_ms / 1000));
mainActivity = MainActivity.getMainActivity();
startForeground(i_NotifID, builder.build());
startTimer();
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void startTimer() {
mCptRebours = new CountDownTimer(l_TimerTime_ms, 1000) {
@Override
public void onTick(long l_RemainingTime_ms) {
// Update notification content
builder.setContentText(Long.toString(l_RemainingTime_ms / 1000));
notificationManager.notify(i_NotifID, builder.build());
// Update frontend (MainActivity value)
mainActivity.UpdateFrontend(l_RemainingTime_ms);
}
@Override
public void onFinish() {
//Log.d("MonApplication", "MyForegroundService onFinish");
}
}.start();
}
}
我的MainActivity在onStart()方法中请求权限:
String[] permissionArray = {Manifest.permission.FOREGROUND_SERVICE, Manifest.permission.VIBRATE, Manifest.permission.POST_NOTIFICATIONS};
ActivityCompat.requestPermissions(this, permissionArray, 0);
并启动服务:
Intent serviceIntent = new Intent(MainActivity.this, MyService.class);
serviceIntent.putExtra("l_TimerTime_ms", mValCompteReboursConfig);
ContextCompat.startForegroundService(MainActivity.this, serviceIntent);`
我的 AndroidManifest.xml 文件声明了服务和权限:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="Manifest.permission.POST_NOTIFICATIONS"/>
<service
android:name=".MyService"
android:enabled="true"
android:exported="true"/>`
我请求在我的 build.gradle 文件中使用 SDK 版本 33:
defaultConfig {
applicationId "com.example.pennskanvtic"
minSdk 21
targetSdk 33
compileSdk 33
targetSdkVersion 33
compileSdkVersion 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
这就是我找到的所有尝试。我现在不知道为什么在启动我的前台服务时仍然会出错。