【问题标题】:startForeground fails to show my notificationstartForeground 无法显示我的通知
【发布时间】:2021-07-01 02:20:56
【问题描述】:

startForeground() 未能显示下面的通知,这是为了让监控接近传感器变化的服务保持活动状态。 logcat中没有错误。

这是我启动服务的地方

    Intent intent = new Intent(ctx, ProximityService.class);
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        ctx.startForegroundService(intent);
    } else {
        ctx.startService(intent);
    }

ProximityService 类

public class ProximityService extends IntentService implements SensorEventListener {

@RequiresApi(api = Build.VERSION_CODES.O)
private void startMyOwnForeground(){

    String NOTIFICATION_CHANNEL_ID = "com.example.xxxxxx";
    String channelName = "Background Proximity Service";
    NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_HIGH);
    chan.setLightColor(Color.BLUE);
    chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    assert manager != null;
    manager.createNotificationChannel(chan);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
    Notification notification = notificationBuilder.setOngoing(true)
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setContentTitle("App is running in background")
            .setPriority(NotificationManager.IMPORTANCE_HIGH)
            .setCategory(Notification.CATEGORY_SERVICE)
            .build();
    startForeground(1, notification);
}

@Override
public void onCreate() {
    super.onCreate();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        startMyOwnForeground();
    else
        startForeground(1, new Notification());
}

@Override
public int onStartCommand(Intent aIntent, int aFlags, int aStartId){
    super.onStartCommand(aIntent, aFlags, aStartId);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        startMyOwnForeground();
    else
        startForeground(1, new Notification());


    return START_STICKY;
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {

        if (event.values[0] < event.sensor.getMaximumRange()) {
            // near
            Toast.makeText(getApplicationContext(), "near", Toast.LENGTH_SHORT).show();
        } else {
            // far
            Toast.makeText(getApplicationContext(), "far", Toast.LENGTH_SHORT).show();
        }
    }
}

public ProximityService() {
    super("Proximity Service");
}


protected void onHandleIntent(Intent workIntent) {
    SensorManager m_sensor_manager = (SensorManager) getSystemService(SENSOR_SERVICE);
    Sensor m_prox = m_sensor_manager.getDefaultSensor(Sensor.TYPE_PROXIMITY);

    m_sensor_manager.registerListener(this, m_prox, SensorManager.SENSOR_DELAY_NORMAL);
}

AndroidManifest.xml

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<service android:enabled="true" android:name=".ProximityService"/>

build.gradle

android {
compileSdkVersion 29
buildToolsVersion "29.0.3"

defaultConfig {
    applicationId "com.example.xxxxxx"
    minSdkVersion 16
    targetSdkVersion 29
    versionCode 1
    versionName "1.0"

【问题讨论】:

  • 尝试将NotificationManager.IMPORTANCE_NONE改为NotificationManager.IMPORTANCE_LOW
  • @Zain 我将它设置为 IMPORTANCE_HIGH 并且它不显示。我将在问题中更新代码。任何其他建议
  • 不是在某些 API 级别上显示的吗?
  • @Zain 我只有一台搭载 Android 10 的设备可供测试。
  • 尝试将startForeground(1, notification); 替换为startForeground(1, notification, FOREGROUND_SERVICE_TYPE_MANIFEST);.. 不完全正确,但尝试限制问题

标签: android android-notifications foreground-service proximity proximitysensor


【解决方案1】:

问题是自定义前台服务是从IntentService扩展而来,而不是Service类;当onHandleIntent() 完成时,IntentService 将自动停止,因此不会出现任何通知。 Check here 全面了解。

因此,您需要扩展 Service 类,并相应地覆盖 onBind()。因此,将其附加到您的课程中:

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

另请注意,IntentService 自 API 级别 30 起已弃用,取而代之的是 WorkManagerJobIntentService

【讨论】:

  • 非常感谢 Zain 的帮助和如此全面的回答,非常感谢,它解决了我的问题 :)
猜你喜欢
  • 2012-02-02
  • 1970-01-01
  • 2019-08-28
  • 2020-08-27
  • 2012-06-13
  • 2020-12-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多