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

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

问题描述

我创建了一个服务,并希望直到我的手机重新启动或强制关闭始终运行此服务。 服务应该在后台运行。 创建服务的示例code和启动服务。

I created a service and want to run this service always until my phone restart 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 pause and anything else. After some time my application goes pause and the services also going 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());
    }
    

  • 您需要修改清单推出单顶模式的活动。

  • 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天全站免登陆