0

我正在尝试在应用程序到达前台时获取应用程序的名称。我通过运行一个后台服务来实现这一点,在该服务中我实现了 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;
    }
4

1 回答 1

0

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

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

于 2013-06-22T11:11:17.967 回答