应用程序关闭后无法保持 Android 服务处于活动状态

2024-03-21

我正在尝试生成一个始终保持活动状态的服务,即使用户关闭应用程序也是如此。根据这些线程

当应用程序关闭时保持位置服务处于活动状态 https://stackoverflow.com/questions/21441232/keep-location-service-alive-when-the-app-is-closed?rq=1

应用程序关闭时 Android 服务停止 https://stackoverflow.com/questions/16651009/android-service-stops-when-app-is-closed

Android:当应用程序被杀死时保持服务运行 https://stackoverflow.com/questions/30525784/android-keep-service-running-when-app-is-killed

这可以通过 IntentServices 或 Service.START_STICKY 来完成

然而,我尝试了这两种类型的服务,但都没有成功。换句话说,当用户关闭应用程序时,我的服务就会被终止。有人可以指出这是否可以做到以及如何做到吗?这是我尝试过但没有成功的方法:

使用意图服务:

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:" + e.toString() + "]" +
                                trace.getClassName() + "->" + trace.getMethodName() + ":" + trace.getLineNumber());
                    }
                }
            }
        };
        mPollThread.start();
    }
}

以及服务:

public class MyService extends Service {
    public MyService() {
    }
    private final int mPollingTimeMS = 500;
    private int mInitializationPollingCount = 0;
    private Thread mPollThread;
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        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:" + e.toString() + "]" +
                                trace.getClassName() + "->" + trace.getMethodName() + ":" + trace.getLineNumber());
                    }
                }
            }
        };
        mPollThread.start();
        return Service.START_STICKY;
    }
    @Override
    public IBinder onBind(Intent intent) {
        // I tried to return null here, but this
        // service gets killed no matter what.
        return null;
    }
}

这是清单:

    <service
        android:name=".mycompany.MyService"
        android:enabled="true"
        android:exported="true"
        android:process=":process1">
    </service>
    <service
        android:name=".mycompany.MyIntentService"
        android:process=":process2"
        android:exported="false">
    </service>

我要补充一点,我不是使用关闭按钮关闭测试应用程序,而是使用 Android 操作系统应用程序管理器。见下图

最后,驱动程序活动(不多)

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent1 = new Intent(getBaseContext(), MyService.class);
        startService(intent1);
        Intent intent2 = new Intent(getBaseContext(), MyIntentService.class);
        startService(intent2);

    }
}

我还尝试添加通知并使其成为前台服务,但仍然是同样的事情。当我关闭应用程序时,所有内容都会被杀死。这是我添加的:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    showNotification();
...etc..

private void showNotification() {
    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);
    int iconId = R.mipmap.ic_launcher;
    int uniqueCode = new Random().nextInt(Integer.MAX_VALUE);
    Notification notification = new NotificationCompat.Builder(this)
            .setSmallIcon(iconId)
            .setContentText("Context Text")
            .setContentIntent(pendingIntent).build();
    startForeground(uniqueCode, notification);
}

这是我使用的前台服务的示例,该服务有效,当应用程序关闭时它仍然处于活动状态。当然,它也必须启动,并且对于该任务,应用程序必须乍一看正在运行,或者必须设置启动事件的接收器,但这是另一个故事。

public class MyService extends Service {
static final int NOTIFICATION_ID = 543;

public static boolean isServiceRunning = false;

@Override
public void onCreate() {
    super.onCreate();
    startServiceWithNotification();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent != null && intent.getAction().equals(C.ACTION_START_SERVICE)) {
        startServiceWithNotification();
    }
    else stopMyService();
    return START_STICKY;
}

// In case the service is deleted or crashes some how
@Override
public void onDestroy() {
    isServiceRunning = false;
    super.onDestroy();
}

@Override
public IBinder onBind(Intent intent) {
    // Used only in case of bound services.
    return null;
}


void startServiceWithNotification() {
    if (isServiceRunning) return;
    isServiceRunning = true;

    Intent notificationIntent = new Intent(getApplicationContext(), MyActivity.class);
    notificationIntent.setAction(C.ACTION_MAIN);  // A string containing the action name
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent contentPendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.my_icon);

    Notification notification = new NotificationCompat.Builder(this)
            .setContentTitle(getResources().getString(R.string.app_name))
            .setTicker(getResources().getString(R.string.app_name))
            .setContentText(getResources().getString(R.string.my_string))
            .setSmallIcon(R.drawable.my_icon)
            .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
            .setContentIntent(contentPendingIntent)
            .setOngoing(true)
//                .setDeleteIntent(contentPendingIntent)  // if needed
            .build();
    notification.flags = notification.flags | Notification.FLAG_NO_CLEAR;     // NO_CLEAR makes the notification stay when the user performs a "delete all" command
    startForeground(NOTIFICATION_ID, notification);
}

void stopMyService() {
    stopForeground(true);
    stopSelf();
    isServiceRunning = false;
}
}

然后我运行它

    Intent startIntent = new Intent(getApplicationContext(), MyService.class);
    startIntent.setAction(C.ACTION_START_SERVICE);
    startService(startIntent);

请注意用作操作的两个常量,它们是必须以包名称开头的字符串。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

应用程序关闭后无法保持 Android 服务处于活动状态 的相关文章

  • Flutter android 风味生成 apk

    我正在尝试使用 flutter 设置 Android 风格 我有两个入口点 lib main prod dart lib main dev dart 我还在我的 gradle 文件中添加了以下内容 flavorDimensions vers
  • 如何检测 Android 设备中的新应用

    我想检测用户何时安装或删除应用程序 但没有找到BroadcastReceiver就是这样做的 在我的应用程序中 我获取了有关该类已安装应用程序的信息PackageManager 但我不想定期扫描应用程序 有没有BroadcastReceiv
  • 是否可以在图片上叠加图标

    我正在创建一个允许用户上传图片的应用程序 当图片上传成功后 我想在右上角添加一个绿色的勾号 可绘制 失败时也一样 但有一个十字 Atm 我正在使用 Glide 在屏幕上显示 URI 我怎样才能做到这一点 您可以通过调用 glide 侦听器来
  • 如何在 TableRow 的一个单元格中添加超过 1 个视图?

    如上所述 如何将 2 个视图放入一个单元格中tablerow 我创建了一个表格布局 并通过代码添加行 下面是我的代码 TableLayout v TableLayout inflater inflate R layout featureit
  • R 无法解析为变量

    我想修复这个错误 R 无法解析为变量 我查了很多答案 但找不到正确的答案 我尝试了一切 有人可以帮助我吗 我的主要活动是自动创建的 显示错误的是从以下位置开始的三行case R id button1 package de vogella a
  • android/eclipse 中的网络服务

    我需要通过soap 方法使用android eclipse 的Web 服务 即 我必须提供输入并根据用户输入从网络服务显示适当的结果 如何做到这一点 java类 public class Demo webserviceActivity ex
  • 自动调整文本视图大小不起作用

    您好 我在我的 Android 应用程序中使用自动调整大小文本视图 我的应用程序 minSdkVersion 是 19 所以我正在使用支持库 https developer android com guide topics ui look
  • Android - 尝试重新打开已关闭的对象:使用 loaderManager 的 SQLiteQuery

    我对 android 相当陌生 我对过滤后的 listView 和它从横向模式更改为纵向模式的活动有一些问题 反之亦然 我有一个用于过滤 drinkSearch 的 editText 只要我不更改视角 纵向与横向 此过滤就有效 这是我得到的
  • 在 MVVM 中使用 LiveData 进行观察时,如何在 onChanged() 上编写单独的代码? [未解决]

    所以 我实施了 public class DriverLoginActivity extends BaseActivity implements Observer
  • Kotlin自定义get执行方法调用

    为了提高对 SharedPreferences Editor 调用的可读性 我想使用一个 Kotlin 变量 每次需要新的 SharedPreferences Editor 时都会执行 getSharedPreferences edit 最
  • Android:进程已死亡 - 资源不足?

    我编写的简单 HTTP GET 请求有一个小问题 该请求每 X 分钟请求一个 URL 我每天都会遇到一两次进程在 GET 请求期间停止的情况 这是调试日志的示例 12 07 16 29 22 650 V TAG 11655 Executin
  • 是否可以将 BitmapDescriptor 转换为 Bitmap?

    我需要将 BitmapDescriptor 转换为 Bitmap 我可以使用以下代码将位图转换为 BitmapDescriptor BitmapDescriptor bd BitmapDescriptorFactory fromBitmap
  • 相机预览越来越长

    我写了一个用于显示相机的代码 以下是我的布局文件的代码
  • Facebook 登录按钮:应用自定义样式

    我在使用新的 Facebook android sdk 4 时遇到了一个奇怪的问题 对于旧的 sdk 版本 我使用的是
  • Android mediacontroller 播放暂停控件无法正确刷新

    我在我的活动中使用了 MediaController 它工作正常 但是当我第一次播放视频时 应该有 b 可见的暂停按钮 但相反 有播放 当我按下该按钮时 视频会正确暂停 状态保持不变 之后它工作正常 视频完成时也会发生同样的事情 这是一个错
  • 旋转器扩展到屏幕边界之外

    我有一个布局 其中包含一个三行的 TableLayout 每行都有一个 TextView 和一个 Spinner Spinners 的柱子设置为拉伸 我的问题是 当旋转器包含长字符串时 旋转器会超出屏幕边缘 我希望他们截断字符串 这是问题的
  • 为 Android 编译时显示 FFMPEG 错误

    我正在尝试将 ffmpeg 添加到我的 android 项目中 我使用的是 ubuntu 14 04 操作系统 我正在关注此链接 Link https software intel com en us android blogs 2013
  • 在选择项目之前设置微调器的文本

    我有一个包含三个项目的微调器 我使用 XML 字符串数组资源来为其提供数据 当您打开活动时 微调器通常会显示数组列表中的第一项 我想更改它并在选择项目之前在微调器中显示文本 选择一个 我怎样才能做到这一点 您可以通过以下两种方式之一进行操作
  • 多次调用 startService 时实际会发生什么

    我正在打电话startService 多次对同一服务有不同的意图 很明显 任何时候都只存在一个服务实例startCommand 被称为每个startService 我的问题是 调用时有性能影响吗startService 多次 意图会按照调用
  • sqlite3权限被拒绝android

    我试图直接在 Nexus 上访问我正在开发的应用程序的数据库 但当我尝试执行 sqlite3 命令时 我收到 权限被拒绝 的消息 我还尝试在 root mod 中启动 adb 但再次 设备上的权限被拒绝 我想我必须使用模拟器来做到这一点 但

随机推荐