我一直onTaskRemoved()在服务中使用方法来检测应用程序从设备最新列表中删除的时间.我预先形成一些日志记录和其他需要在发生这种情况时发生的操作.它完美地运作.
然后我在运行Android 6.0的HUAWEI设备上检查了这个方法.该方法永远不会被调用.我还添加了一个Log.d调用,正如预期的那样,这个日志从未出现过.在XIOMI设备上也是如此.
任何想法为什么会发生以及如何解决这个问题?或者有没有另一种方法来检测应用程序是否已从RECENT列表中删除而不依赖onTaskRemoved()?
谢谢
我正在尝试创建一个允许用户记录路线(位置/ GPS)的应用程序.为了确保即使屏幕关闭也会记录位置,我已经foreground service为位置记录创建了一个.我将位置存储在一个Room Database注入我的服务中的位置Dagger2.
但是,这项服务被Android杀死,当然这并不好.我可以订阅低内存警告,但这并没有解决我的服务在运行Android 8.0的现代高端手机上大约30分钟后被杀的根本问题
我创建了一个只有"Hello world"活动和服务的最小项目:https://github.com/RandomStuffAndCode/AndroidForegroundService
该服务在我的Application课程中启动,路由记录通过以下方式启动Binder:
// Application
@Override
public void onCreate() {
super.onCreate();
mComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
Intent startBackgroundIntent = new Intent();
startBackgroundIntent.setClass(this, LocationService.class);
startService(startBackgroundIntent);
}
// Binding activity
bindService(new Intent(this, LocationService.class), mConnection, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT);
// mConnection starts the route logging through `Binder` once connected. The binder calls startForeground()
Run Code Online (Sandbox Code Playgroud)
我可能不需要BIND_AUTO_CREATE旗帜,我一直在测试不同的旗帜,试图不让我的服务被杀 - 到目前为止没有运气.
使用分析器看起来我没有任何内存泄漏,内存使用率稳定在~35mb:
使用adb shell dumpsys activity …
android android-service android-location android-lifecycle android-doze
如果在背景中,MyApp的接收器工作正常,它是:
public class MySmsReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("triggered sms");
if(intent.getAction().equals(Telephony.Sms.Intents.SMS_RECEIVED_ACTION)) {
Toast.makeText(context, "message Received", Toast.LENGHT_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
我的接收器清单文件是
<receiver android:name=".MySmsReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
文件说,如果我在清单文件中声明一个接收器,它应该始终有效.但就我而言,它不是......
但每当我通过刷它清除我最近的应用程序时它就停止了工作.在我去任务管理员后看到MyApps强制停止如下所示 
我想出了一些应用程序,如watsapp和fb总是留在内存中说这个bcoz刷完清楚的最近的应用程序仍然任务管理器有如下所示的跟随状态
我怎么能在我的应用程序中执行此操作..什么会使我的应用程序像其他第三方应用程序,如watspp和Facebook ...我怎么能让我的应用程序在内存中总是我问这个然后只有我的接收器将始终工作..如果我错了,请给我一个解决方案来做到这一点......
我一直在搜索这个问题但仍然无法找到解决方案...我错了吗?还是真的有办法做到这一点?请有人帮帮我...这让我一周!!! 希望我在这里解释我的问题,如果我不问我,我会立即给你答复.
service android taskmanager broadcastreceiver google-cloud-messaging
我正在尝试生成一个始终保持活动的服务,即使用户关闭了应用程序.根据这些线程
这可以通过IntentServices或Service.START_STICKY来完成
然而,我尝试了两种类型的服务而没有成功.换句话说,当用户关闭应用程序时,我的服务就会被杀死.有人可以指出这是否可以做到以及如何做?这是我没有成功的尝试:
使用IntentService:
public class MyIntentService extends IntentService {
private final int mPollingTimeMS = 500;
private int mInitializationPollingCount = 0;
private Thread mPollThread;
public MyIntentService() {
super("MyIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
mPollThread = new Thread() {
public void run() {
while (true) {
try {
Log.e(Constants.Engine.LOGGER_TAG_DEV,
"SDK Service Running: " +
mInitializationPollingCount * mPollingTimeMS +
"ms have elapsed");
mInitializationPollingCount++;
sleep(mPollingTimeMS);
} catch (Exception e) {
StackTraceElement trace = new Exception().getStackTrace()[0];
Logger.e(Constants.Engine.LOGGER_TAG_APP, "[Exception:" + …Run Code Online (Sandbox Code Playgroud) 我有一个服务的应用程序.服务由app在create method的主要活动中启动.我退出应用程序时出现问题:服务停止,有时立即重启,有时最近重启.我在4.1.2版和2.3.6版中测试过.版本2.3.6中不再提供服务.我的方法有误吗?在停止和重新启动时间之间,必须阻止的呼叫可以到来.退出应用程序时有没有办法停止服务?注意:我无法使用远程服务.
我添加了startForeground服务,但同样的问题.当我退出应用程序服务停止而不重启版本2.3.3甚至我看到notification.But phoneStateListener取空值我怎样才能确保当我退出应用程序时,服务不会停止
//application main metod
@Override
protected void onCreate(Bundle savedInstanceState) {
//.....
startService(new Intent(getApplicationContext(), MyPhoneStateListener.class));
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
setCallListener();
setNoti();
return START_STICKY;
}
private void setCallListener()
{
try
{
if (phoneStateListener==null)
{
phoneStateListener = new StateListener();
telephonymanager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
telephonymanager.listen(phoneStateListener,PhoneStateListener.LISTEN_CALL_STATE);
}
}
catch(Exception ex)
{
}
}
Run Code Online (Sandbox Code Playgroud)
private void setNoti(){
Notification notification= new Notification(R.drawable.ic_launcher, getResources().getString(R.string.app_name), System.currentTimeMillis());
Intent main = new Intent(this, MainActivity.class);
main.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, …Run Code Online (Sandbox Code Playgroud) 我需要一个在后台运行的服务并计算两个位置之间的每分钟距离.我使用Thread来每分钟执行一个方法,然后我明白当应用程序关闭时,服务也会停止,因为应用程序和服务使用相同的线程.我怎样才能创建一个每1分钟调用一次的简单方法,即使应用程序关闭也是如此?