尝试启动然后绑定前台服务时,我的应用程序的前台服务崩溃。
奇怪的是,只有使用所有者帐户以外的帐户进行下载和安装时,才会发生这种情况。如果首先从所有者帐户下载,则一切正常。我使用Nexus 7 Adnroid 6.0.1进行测试,并且该应用是从Google Play内部测试下载的。
代码段:
public void startAndBindBackendService{
....
context.startService(buildBackendServiceIntent());
context.bindService(buildBackendServiceIntent(), backendServiceConn , Service.BIND_AUTO_CREATE | Service.BIND_ABOVE_CLIENT);
}
private Intent buildBackendServiceIntent(){
Intent intent = new Intent(BACKEND_SERVICE_INTENT_ACTION);
intent.setPackage(PKG_NAME);
intent.putExtra("serverIp", "x.x.x.x");
intent.putExtra("isAppInBackground", isAppInBackground);
return intent;
}
public class BackendService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
startForeground(FOREGROUND_SERVICE_NOTI_ID, new Notification.Builder(context)
.build());
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
....
serviceStub = (BackendServiceInterface.Stub) buildServiceBinder(); // BackendServiceInterface is AIDL interface
return serviceStub;
}
...
}
清单:
<application
android:name=".MainApp"
android:allowBackup="false"
android:icon="@mipmap/hippo_icon"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
....
<service
android:name=".service.***"
android:process=":remote"
android:stopWithTask="false"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="****"/>
</intent-filter>
</service>
....
</application>
例外:
02-11 15:14:34.378 28390-28390/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.***.*****:remote, PID: 28390
android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid service notification: Notification(pri=0 contentView=com.pc.*****/0x1090085 vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1507)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)