简体   繁体   中英

Android 6.0 Marshmallow - Service not starting on test with API 23, works fine on API 21

I have the following test Service. It runs fine on API 21, but on API 23 the service doesn't start.

I have gone through the API change docs, but cannot see anything like changes to registration, etc.

This exact code is working on API 21. What am I missing?

Manifest segment:

<service
    android:name="com.mytest.service.TestService"
    android:label="@string/app_name"
    android:permission="android.permission.SYSTEM_ALERT_WINDOW" />

MainActivity segment:

this.startService(new Intent(this, TestService.class));

Service:

public class TestService extends Service
{
    @Nullable
    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        Log.d("OOP", "onstartTest");
        return super.onStartCommand(intent, flags, startId);
    }
}

Remove android:permission="android.permission.SYSTEM_ALERT_WINDOW" . You do not need it, as your service is not exported, so no third-party app can talk to that service anyway.

My guess is that your app does not hold that permission, as the user has not manually gone in and allowed your app to draw over top of other apps (via Settings).

Maybe you need to declare a permission explicitly for your test, here is an example to declare Coarse Location Permission from test for Marshmallow:

private void requestUserPermission() {
    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION)) {
        displayCoarseLocationPermissionExplanation();
    } else {
        ActivityCompat.requestPermissions(MyActivityNameWherePermissionIsNeeded.this,
                                          new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                                          PERMISSION_REQUEST_COARSE_LOCATION);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM