我有一台带有Android 5.0的华为P8用于测试应用程序.该应用程序需要在后台运行,因为它跟踪BLE区域.
我发现华为内置了一个名为Protected Apps的"功能",可以通过手机设置(电池管理器>受保护的应用程序)进行访问.这允许选定的应用程序在屏幕关闭后继续运行.
对华为来说很明智,但不幸的是,对于我来说,它看起来像是选择加入,即默认情况下应用程序已经关闭,你必须手动将它们放入.这不是一个showstopper,因为我可以在常见问题解答或打印中建议用户关于修复的文档,但我最近安装了Tinder(用于研究目的!),并注意到它被自动放入受保护的列表中.
有谁知道我的应用程序如何做到这一点?它是清单中的设置吗?这是华为为Tinder启用的东西,因为它是一个受欢迎的应用程序?
我想要一个服务在我的应用程序中一直运行.因此,即使用户强行关闭,我也想重新启动它.肯定有一种方法可以做到像facebook这样的应用程序正在这样做.(它没有使用推送通知,即使互联网关闭,facebook也会重启其服务).
任何帮助,将不胜感激.谢谢!
我正在开发一个使用Google推送通知的应用程序.应用程序在小米手机中运行时会收到通知,否则当它被杀死时它不会收到通知.
如果我们想要在应用程序被杀死时收到通知,那么我们需要从xiaomi的安全应用程序手动允许自动重启应用程序.我想要任何技巧以编程方式执行此操作而无需询问用户.有没有办法做到这一点?

我的应用程序中的服务需要一直在后台运行,在所有设备中它的工作正常除了小米,我已经阅读了一些我们需要在应用程序的设置中启用自动启动以保持服务运行的地方.
所以请告诉我如何以编程方式启用自动启动,因为用户永远不会这样做.
任何帮助将不胜感激.
与以前的版本不同,在4.4中,从最近的任务中滑动应用程序会永久杀死应用程序及其服务(如强制停止),即使它正在运行后台服务.它显示0个进程1服务,但服务也不起作用.理想情况下,它不应该杀死后台服务,它不会在4.3之前的版本中.知道为什么会发生在4.4?
我已经startforeground()在我用了Service,但是Service当我清除所有最近的应用程序时,Android一直在杀死我.
这是我的服务代码:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("Text")
.setTicker("Text")
.setContentText("Text")
.setSmallIcon(R.drawable.icon)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon))
.build();
startForeground(100, notification);
return START_STICKY;
}
Run Code Online (Sandbox Code Playgroud)
我用这段代码做错了吗?
我的应用程序有后台服务,即使应用程序被任务管理器杀死也继续运行但是当我在MI(xiaomi)手机上测试我的应用程序时,当我从任务管理器关闭应用程序并且我的服务继续运行时,它会终止我的应用程序服务即使我从任务管理器中删除我的应用程序,也能在除MI之外的所有设备上完美地工
那么即使应用程序被任务管理器杀死,如何让我的服务在后台运行?
我最近使用Google推出的Activity Transition API来检测用户何时进出车辆.下面是我用来实现相同的代码.
val intent = Intent(context, ActivityTransitionReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT)
transitions.apply {
add(ActivityTransition.Builder()
.setActivityType(DetectedActivity.IN_VEHICLE)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
.build())
add(ActivityTransition.Builder()
.setActivityType(DetectedActivity.IN_VEHICLE)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
.build())
}
var transitionRequest = ActivityTransitionRequest(transitions)
// myPendingIntent is the instance of PendingIntent where the app receives callbacks.
val task = ActivityRecognition.getClient(context).requestActivityTransitionUpdates(transitionRequest, pendingIntent)
task.addOnSuccessListener {
// Handle success
context.toast("Task added successfully")
}
task.addOnFailureListener {
// Handle error
context.toast("Error adding task")
}
Run Code Online (Sandbox Code Playgroud)
该应用程序工作正常,并在未从最近的应用程序选项卡中删除时接收广播.但是,当应用程序从最近的应用程序选项卡中删除或被系统杀死时,我无法接收广播.我不知道我做错了什么.还有其他方法可以达到同样的目的吗?
我考虑使用前台服务,以便应用程序不被杀死,但后来认为显示永久通知只是为了检测用户活动过渡不是一个好主意.任何帮助将不胜感激.谢谢.
编辑:我已经看到一些应用程序要求特殊权限,例如"忽略电池优化",当获得此权限时,该应用程序似乎始终有效并且不断发送通知.
android broadcastreceiver android-location activity-recognition
我每次都使用Broadcast Receiver触发incoming messages。它在Android O任一应用程序中的工作正常是否关闭。但Android P它仅在应用程序处于活动状态和应用程序关闭时才有效。无论应用程序是关闭还是不在Android P. 我遵循了这个链接和许多其他链接,但问题仍然存在。
清单中的接收者注册
<receiver
android:name=".Broadcast.SmsListener"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
广播接收器类
public class SmsListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Resulted12", "Into onReceive()");
context.startService(new Intent(context, BackgroundService.class));
}
}
Run Code Online (Sandbox Code Playgroud)
还有什么我错过的吗?
在这里,我想问一个基本的问题,我在做代码时找不到答案。
据我了解,在应用程序onCreate()中创建单例实例的脆弱性低于android服务。在我的应用程序中,我想收听位置更新,该位置更新的可能性较小。如果我保留服务,它可能会在内存不足的情况下被杀死,但是仍然可以在后台运行该应用程序,从而保留实例。因此,我想使用单例实例而不是服务。
这是正确的方法吗?
这是我的服务:
public class KeepAliveService extends Service {
Alarm alarm = new Alarm();
public void onCreate()
{
Log.i("","KEEPALIVE onCreate");
super.onCreate();
Notification notification = new Notification();
startForeground(1337, notification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
alarm.setAlarm(this);
Log.i("","KEEPALIVE onCreate start command");
return START_STICKY;
}
@Override
public void onStart(Intent intent, int startId)
{
alarm.setAlarm(this);
Log.i("","KEEPALIVE onStart");
}
@Override
public IBinder onBind(Intent intent)
{
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
我的清单中有这个:
<service
android:name="com.vidyo.vidyomod.KeepAliveService"
android:exported="true"
android:process=":KeepAliveService"
android:enabled="true">
</service>
<receiver android:process=":remote" android:name="com.vidyo.vidyomod.utils.Alarm"></receiver>
<receiver android:name="com.vidyo.vidyomod.utils.AutoStart">
<intent-filter> …Run Code Online (Sandbox Code Playgroud) START_STICKY每当我杀死我的应用程序后就无法在我的设备上工作,而服务无法再次启动,我的设备名称为Redmi Note 3 Pro,但是每当我在android模拟器中运行相同的应用程序时,当我杀死该应用程序并服务直到我通过stopService()方法将其停止时才停止
请帮帮我
问题解决了
完成此操作:
设置>权限>自动启动, 然后打开我的应用程序的开关,然后完成!
我在此链接中找到了解决方案:解决方案链接