【问题标题】:Service gets killed in Jelly Bean服务在 Jelly Bean 中被杀死
【发布时间】:2013-06-19 10:42:11
【问题描述】:

我正在尝试在应用程序到达前台时获取它的名称。我通过运行一个后台服务来实现这一点,在该服务中我实现了 Runnable 以每秒运行该服务。这在除 Jelly Bean 之外的所有操作系统中都可以正常工作。我的服务被杀死了。我知道如果一个应用程序消耗更多的 RAM,那么它会杀死可用的后台服务来管理所需的空间。例如高分辨率游戏。但我会丢失数据。我实现了前台服务。它工作正常。我的疑问是,这对物质效率或电池消耗有任何影响吗?除了前台还有其他方法吗??

这是我做前台的服务。

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        ReceiverRunning = true;
        context = this;

        Intent intent1 = new Intent(this, ComponentSelector.class);  
          PendingIntent pendingIndent = PendingIntent.getActivity(this, 1, intent1, 0);  
          Notification mNotification = new Notification(R.drawable.ic_launcher, "BackgroundApp", System.currentTimeMillis());  
          mNotification.setLatestEventInfo(this, "BatteryUSage", "Click to open to app", pendingIndent);  
          mNotification.flags = mNotification.flags|Notification.FLAG_ONGOING_EVENT;  
            startForeground(1000, mNotification);

        // Start service if not started.
        if (!ForegroundApp.isRunning == true) {
            context.startService(new Intent(context, Brightness.class));
        }

        boolean has_tele = getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_TELEPHONY);
        if (has_tele == true) {
            TelephonyManager teleman = (TelephonyManager) getBaseContext()
                    .getSystemService(Context.TELEPHONY_SERVICE);
            if (teleman != null)
                deviceId = teleman.getDeviceId();
        }

        uId = deviceInfo();

        try {
            NetworkInfo info = (NetworkInfo) ((ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE))
                    .getActiveNetworkInfo();
            if (info != null) {
                Log.d("wifiRun", "Network available");
                ConnectivityManager conMan = (ConnectivityManager) context
                        .getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo.State wifi = null;
                if (conMan.getNetworkInfo(1).isAvailable())
                    wifi = conMan.getNetworkInfo(1).getState();
                if (wifi == NetworkInfo.State.CONNECTED
                        || wifi == NetworkInfo.State.CONNECTING) {
                    wifiCheck = true;
                    context.startService(new Intent(context, WiFi.class));
                    Log.d("wifiRun","wifiCheck: " +wifiCheck);
                } else {
                    Log.d("wifiRun","wifiCheck: " +wifiCheck);
                }
            }
            } catch (Exception e) {
                e.printStackTrace();
            }

        // Register for Screen On and Screen Off.
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
        intentFilter.addAction(Intent.ACTION_SCREEN_ON);
        sReceiver = new ServiceDefinition();
        registerReceiver(sReceiver, intentFilter);
        //return super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }

【问题讨论】:

    标签: android android-service android-4.2-jelly-bean foreground


    【解决方案1】:

    我想,别无他法。您也可以使您的服务具有粘性(http://developer.android.com/reference/android/app/Service.html#START_STICKY),但在这种情况下,使服务前台更为正确。当然,你的服务会消耗电池,所以尽量不要在你的服务中使用“繁重”的操作。

    如果您提供服务的源代码,我们将检查您是否实现了所有优化。

    【讨论】:

    • 为什么要在主服务中启动2个附加服务?它们都是前景吗?
    • 这两个附加服务不是前台。那是我获取前台活动详细信息的地方。
    • 那么,您有前台服务,您可以在其中启动两个非前台服务来获取任何数据?在您的代码中,您每 1 秒启动一次前台服务以保持非前台服务的存活?
    • 在这个前台服务中,我正在启动一个非前台服务。我已经在非前台服务中实现了 1 秒可运行。当屏幕关闭时,我将停止非前台服务。
    • 但是系统可以杀死你的非前台服务,你也会遇到同样的问题。我认为重构会更好——不要启动额外的服务,而是使用你的主要前台服务进行任何操作。
    猜你喜欢
    • 1970-01-01
    • 2019-11-17
    • 1970-01-01
    • 2012-08-26
    • 1970-01-01
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多