服务一直在运行吗?

时间:2014-12-22 04:50:44

标签: android android-service android-broadcast android-background battery-saver

我正在尝试了解服务和BroadCastReceiver。 下面的代码是一种始终在后台运行的服务。问题是我不知道它会如何影响电池消耗。

我的目标是开启和关闭屏幕,因此当应用关闭或打开时我需要在后台运行服务...

是否会耗尽大量电池? 你能解释一下吗?

谢谢

public class MyService extends Service{

    private static final String TAG = "MyService";
    private BroadcastReceiver mScreenOnOffReceiver;
    private BroadcastReceiver OnOffBroadcastReceiver;
    private NotificationManager mNotificationManager;
    private Notification barNotif;

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onCreate() {

    }

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

    // here to show that your service is running foreground     
    mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Intent bIntent = new Intent(this, WidgetBroadCastReceiver.class);       
        PendingIntent pbIntent = PendingIntent.getActivity(this, 0 , bIntent, Intent.FLAG_ACTIVITY_CLEAR_TOP);
        NotificationCompat.Builder bBuilder =
                new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("STICKY")
                    .setContentText("Sub Title")
                    .setAutoCancel(true)
                    .setOngoing(true)
                    .setContentIntent(pbIntent);
        barNotif = bBuilder.build();
        this.startForeground(1, barNotif);

    // here the body of your service where you can arrange your reminders and send alerts

     Thread mThread = new Thread() {
         @Override
         public void run() {
         // Register the ScreenOnOffReceiver.java  
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        mScreenOnOffReceiver = new ScreenOnOffReceiver();
        registerReceiver(mScreenOnOffReceiver, filter);

        // initialize and register mScreenOnOffReceiver (no need the BroadcastReceiver class)
        OnOffBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                    Log.e("", "SERVICE Screen is: " +  "turned OFF.....");
                } 
           else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                    Log.e("", "SERVICE Screen is: " +  "turned ON......");
                }
            }
        };
        registerReceiver(OnOffBroadcastReceiver, new IntentFilter(filter));       
         }
     };

     mThread.start();   

    return START_STICKY;
    }


    @Override
    public void onStart(Intent intent, int startId) {
        Toast.makeText(this, "My Service has Started", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "MyService Stopped", Toast.LENGTH_SHORT).show();
        unregisterReceiver(mScreenOnOffReceiver);
        unregisterReceiver(OnOffBroadcastReceiver);
        stopForeground(true);
    }
}

1 个答案:

答案 0 :(得分:0)

在您的代码示例中,该服务没有执行消耗电池的某些操作(密集计算,网络请求等)。所以我个人认为你的服务消耗大量电池并不是很明显。但是服务在它启动后保留,这意味着你的应用程序将占用更多内存。在某种程度上,我认为大内存应用程序将占用更多系统的电池,系统需要做更多的额外工作以保持整体性能处于良好状态,这些额外的工作需要CPU,IO和内存。消耗电池的计算和读/写周期。

您没有在服务中启动新主题,这意味着您的服务的所有工作都将在主线程上运行。这样,如果你长时间操作阻止UI,它将导致ANR的可能性很高。在您的示例中,您只是开始通知并注册广播接收器,您的应用程序运行顺利且没有抱怨ANR,因为这两个操作很快就会完成。但你应该意识到这一点。

如果你将BroadcastReceiver放到AndroidManifest.xml,个人情况可能会更好,但这种情况并不像你那样常用。