geo*_*667 4 java android-notifications android-13
每次执行应用程序时,我都会收到“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)`
Run Code Online (Sandbox Code Playgroud)
我的服务仅启动计时器并显示通知(我尝试在NotificationCompat.Builder中使用setChannelId而不进行任何更改):
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)`
Run Code Online (Sandbox Code Playgroud)
我的 MainActivity 在 onStart() 内请求许可:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
并启动服务:
String[] permissionArray = {Manifest.permission.FOREGROUND_SERVICE, Manifest.permission.VIBRATE, Manifest.permission.POST_NOTIFICATIONS};
ActivityCompat.requestPermissions(this, permissionArray, 0);
Run Code Online (Sandbox Code Playgroud)
我的 AndroidManifest.xml 声明服务和权限:
Intent serviceIntent = new Intent(MainActivity.this, MyService.class);
serviceIntent.putExtra("l_TimerTime_ms", mValCompteReboursConfig);
ContextCompat.startForegroundService(MainActivity.this, serviceIntent);`
Run Code Online (Sandbox Code Playgroud)
我在 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"
}
Run Code Online (Sandbox Code Playgroud)
这就是我想尝试的一切。我现在不知道为什么启动前台服务时仍然出现错误。
geo*_*667 16
终于找到问题了:我忘记创建通知通道...添加的代码解决了我的问题:
Run Code Online (Sandbox Code Playgroud)NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "PennSkanvTicChannel", NotificationManager.IMPORTANCE_MAX); channel.setDescription("PennSkanvTic channel for foreground service notification"); notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel);
然后,在创建通知(NotificationCompat.Builder)时需要使用相同的CHANNEL_ID。
希望这对某人有帮助。
| 归档时间: |
|
| 查看次数: |
6201 次 |
| 最近记录: |