Android 服务需要始终运行(永不暂停或停止) [英] Android Service needs to run always (Never pause or stop)

查看:19
本文介绍了Android 服务需要始终运行(永不暂停或停止)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个服务,并希望一直运行该服务,直到我的手机重新启动或强制关闭.该服务应在后台运行.

I created a service and want to run this service always until my phone restarts or force closed. The service should run in background.

创建服务和启动服务的示例代码:

Sample code of created service and start services:

启动服务:

Intent service = new Intent(getApplicationContext(), MyService.class);
getApplicationContext().startService(service);

服务:

public class MyService extends Service {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO do something useful
        HFLAG = true;
        //smsHandler.sendEmptyMessageDelayed(DISPLAY_DATA, 1000);
        return Service.START_NOT_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO for communication return IBinder implementation
        return null;
    }
}

清单声明:

<service
    android:name=".MyService"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
</service>

是否可以在应用程序暂停和其他任何情况下始终运行此服务.一段时间后,我的应用程序暂停,服务也暂停或停止.那么我如何才能在后台始终运行此服务.

Is it possible to run this service always as when the application pauses and anything else. After some time my application goes pause and the services also go pause or stop. So how can I run this service in background and always.

推荐答案

是否可以在应用程序暂停和其他任何情况下始终运行此服务?"

"Is it possible to run this service always as when the application pause and anything else?"

是的.

  1. 在服务的 onStartCommand 方法中返回 START_STICKY.

  1. In the service onStartCommand method return START_STICKY.

public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
}

  • 使用 startService(MyService) 在后台启动服务,以便无论绑定的客户端数量如何,它始终保持活动状态.

  • Start the service in the background using startService(MyService) so that it always stays active regardless of the number of bound clients.

    Intent intent = new Intent(this, PowerMeterService.class);
    startService(intent);
    

  • 创建活页夹.

  • Create the binder.

    public class MyBinder extends Binder {
            public MyService getService() {
                    return MyService.this;
            }
    }
    

  • 定义服务连接.

  • Define a service connection.

    private ServiceConnection m_serviceConnection = new ServiceConnection() {
            public void onServiceConnected(ComponentName className, IBinder service) {
                    m_service = ((MyService.MyBinder)service).getService();
            }
    
            public void onServiceDisconnected(ComponentName className) {
                    m_service = null;
            }
    };
    

  • 使用 bindService 绑定到服务.

  • Bind to the service using bindService.

            Intent intent = new Intent(this, MyService.class);
            bindService(intent, m_serviceConnection, BIND_AUTO_CREATE);
    

  • 对于您的服务,您可能需要在服务关闭后启动相应活动的通知.

  • For your service you may want a notification to launch the appropriate activity once it has been closed.

    private void addNotification() {
            // create the notification
            Notification.Builder m_notificationBuilder = new Notification.Builder(this)
                    .setContentTitle(getText(R.string.service_name))
                    .setContentText(getResources().getText(R.string.service_status_monitor))
                    .setSmallIcon(R.drawable.notification_small_icon);
    
            // create the pending intent and add to the notification
            Intent intent = new Intent(this, MyService.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
            m_notificationBuilder.setContentIntent(pendingIntent);
    
            // send the notification
            m_notificationManager.notify(NOTIFICATION_ID, m_notificationBuilder.build());
    }
    

  • 您需要修改清单以在单顶模式下启动 Activity.

  • You need to modify the manifest to launch the activity in single top mode.

              android:launchMode="singleTop"
    

  • 请注意,如果系统需要资源并且您的服务不是很活跃,它可能会被终止.如果这是不可接受的,请使用 startForeground 将服务置于前台.

  • Note that if the system needs the resources and your service is not very active it may be killed. If this is unacceptable bring the service to the foreground using startForeground.

            startForeground(NOTIFICATION_ID, m_notificationBuilder.build());
    

  • 这篇关于Android 服务需要始终运行(永不暂停或停止)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆