我尝试在此链接中使用示例代码,但它似乎已过时,但它不起作用.那么当Android完成启动时,我需要做些什么更改以及让我的应用自动启动哪些文件?
将手机升级到8.1开发人员预览后,我的后台服务不再正常启动.
在我长期运行的服务中,我实现了一个startForeground方法来启动正在进行的通知,该通知在create上调用.
@TargetApi(Build.VERSION_CODES.O)
private fun startForeground() {
// Safe call, handled by compat lib.
val notificationBuilder = NotificationCompat.Builder(this, DEFAULT_CHANNEL_ID)
val notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.build()
startForeground(101, notification)
}
Run Code Online (Sandbox Code Playgroud)
错误信息:
11-28 11:47:53.349 24704-24704/$PACKAGE_NAMEE/AndroidRuntime: FATAL EXCEPTION: main
Process: $PACKAGE_NAME, PID: 24704
android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=My channel pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x42 color=0x00000000 vis=PRIVATE)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1768)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Run Code Online (Sandbox Code Playgroud)
服务通知的通道无效,显然我的旧通道DEFAULT_CHANNEL_ID不再适用于我假设的API 27.什么是合适的渠道?我试图浏览一下文档
service android background-service android-notifications kotlin
最近我们突然看到了下面的一些堆栈跟踪.为什么会这样?这是应用程序尝试通过媒体通知和所有内容将音频评论服务移动到前台时.
java.lang.SecurityException: Permission Denial: startForeground from pid=1824, uid=10479 requires android.permission.FOREGROUND_SERVICE
at android.os.Parcel.createException(Parcel.java:1942)
at android.os.Parcel.readException(Parcel.java:1910)
at android.os.Parcel.readException(Parcel.java:1860)
at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:5198)
at android.app.Service.startForeground(Service.java:695)
at com.example.app.services.AudioService.setUpMediaNotification(AudioService.java:372)
at com.example.app.services.AudioService.setUpAndStartAudioFeed(AudioService.java:328)
at com.example.app.services.AudioService.onStartCommand(AudioService.java:228)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3667)
at android.app.ActivityThread.access$1600(ActivityThread.java:199)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1681)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.am.ActivityManagerService.enforcePermission(ActivityManagerService.java:9186)
at com.android.server.am.ActiveServices.setServiceForegroundInnerLocked(ActiveServices.java:1189)
at com.android.server.am.ActiveServices.setServiceForegroundLocked(ActiveServices.java:870)
at com.android.server.am.ActivityManagerService.setServiceForeground(ActivityManagerService.java:20434)
at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:976)
Run Code Online (Sandbox Code Playgroud) 我创建了一项服务,并希望始终运行此服务,直到我的手机重新启动或强制关闭.该服务应该在后台运行.
创建的服务和启动服务的示例代码:
启动服务:
Intent service = new Intent(getApplicationContext(), MyService.class);
getApplicationContext().startService(service);
Run Code Online (Sandbox Code Playgroud)
服务:
public class MyService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO do something useful
HFLAG = true;
//smsHandler.sendEmptyMessageDelayed(DISPLAY_DATA, 1000);
return Service.START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// TODO for communication return IBinder implementation
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
清单声明:
<service
android:name=".MyService"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
</service>
Run Code Online (Sandbox Code Playgroud)
是否可以像应用程序暂停和其他任何操作一样运行此服务.一段时间后,我的应用程序暂停,服务也暂停或停止.那么如何在后台运行此服务并始终如此.
我有一个音乐播放器,试图启动Service
中onResume()
的一个Activity
.为清晰起见,我删除了几行,但代码有效:
@Override
protected void onResume() {
super.onResume();
startService(new Intent(this, MusicService.class));
}
Run Code Online (Sandbox Code Playgroud)
根据崩溃日志,这在运行Android P的某些设备上抛出异常:
Caused by java.lang.IllegalStateException: Not allowed to start service Intent { cmp=another.music.player/com.simplecity.amp_library.playback.MusicService }: app is in background uid UidRecord{6a4a9c6 u0a143 TPSL bg:+3m25s199ms idle change:cached procs:1 seq(1283,1283,1283)}
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1577)
at android.app.ContextImpl.startService(ContextImpl.java:1532)
at android.content.ContextWrapper.startService(ContextWrapper.java:664)
at android.content.ContextWrapper.startService(ContextWrapper.java:664)
at com.simplecity.amp_library.utils.MusicServiceConnectionUtils.bindToService(SourceFile:36)
at com.simplecity.amp_library.ui.activities.BaseActivity.bindService(SourceFile:129)
at com.simplecity.amp_library.ui.activities.BaseActivity.onResume(SourceFile:96)
Run Code Online (Sandbox Code Playgroud)
在调用onResume()
(和super.onResume()
)之后,我的应用程序是如何处于后台的?
这对我没有任何意义.这可能是一个平台错误吗?受此次崩溃影响的所有3500多名用户都在Android P.
我正在监控收到的短信.
我的应用程序与a完美配合BroadcastReceiver
.但它是从一个Activity开始工作,并希望一直保持BroadcastReceiver
运行(而不仅仅是我的Activity运行时).
我怎样才能做到这一点?我已经查看了生命周期,BroadcastReceiver
但文档中提到的所有内容都是生命周期仅限于onReceive
方法,而不是保持BroadcastReceiver
检查传入SMS 的生命周期.
我怎么能坚持这个?
谢谢
如文档中所述:
"An app holding the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission can trigger a system dialog to let the user add the app to the whitelist directly, without going to settings. The app fires a ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS Intent to trigger the dialog."
Can someone tell me the proper way to fire this intent?
optimization android android-intent android-6.0-marshmallow android-doze
我将我的 flutter 应用程序部署到 Google Play 商店内部测试,一小时后,我在预发布报告详细信息中看到了此异常。我什至不知道从哪里开始调试。有人可以帮忙吗?由于我使用的是 Flutter,因此对 Android 的了解不多。
异常 java.lang.RuntimeException:无法启动接收器 com.google.android.finsky.instantapps.PhenotypeUpdateReceiver:java.lang.IllegalStateException:不允许启动服务 Intent { act=com.google.android.gms.phenotype.UPDATE cmp =com.android.vending/com.google.android.finsky.instantapps.PhenotypeUpdateService }:应用程序在后台 uid UidRecord{2a75503 u0a192 TRNB bg:+2m53s648ms 空闲更改:未缓存的过程:4 seq(0,0,0)}在 android.app.ActivityThread.handleReceiver (ActivityThread.java:4035)
在 android.app.ActivityThread.access$1400 (ActivityThread.java:237)
在 android.app.ActivityThread$H.handleMessage (ActivityThread.java:1924) 在 android .os.Handler.dispatchMessage (Handler.java:106) 在 android.os.Looper.loop (Looper.java:223) 在 android.app.ActivityThread.main (ActivityThread.java:7664) 在 java.lang.reflect。 Method.invoke (Method.java) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:592) 在 com.android.internal.os.ZygoteInit.main (ZygoteInit.java:947) 引起java.lang.IllegalStateException:不允许启动服务 Intent { act=com.google.android.gms.phenotype.UPDATE cmp=com.android.vending/com.google.android.finsky.instantapps.PhenotypeUpdateService }:应用程序处于背景 uid UidRecord{2a75503 u0a192 TRNB bg:+2m53s648ms 空闲更改:未缓存的 procs:4 seq(0,0,0)} 在 android.app.ContextImpl.startServiceCommon (ContextImpl.java:1715)
在 android.app.ContextImpl.startService (ContextImpl.java:1670) 在 android.content.ContextWrapper.startService (ContextWrapper.java:720) 在 alj.a (alj.java:2) 在 …
从onCreate()
我的活动,
我试图通过以下代码启动服务:
Intent intent = new Intent();
intent.setClassName(SERVICE_PKG_NAME, SERVICE_NAME);
context.startService(intent); //context = MainActivity.this
Run Code Online (Sandbox Code Playgroud)
但是,我收到以下异常:
java.lang.IllegalStateException:不允许启动服务Intent(service-name):app在后台
知道这可能是什么原因吗?现在坚持几个小时.
android ×9
service ×2
android-doze ×1
flutter ×1
google-play ×1
java ×1
kotlin ×1
optimization ×1
startup ×1