Android服务需要始终运行(从不暂停或停止)

Ash*_*sik 67 java service android

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

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

启动服务:

Intent service = new Intent(getApplicationContext(), MyService.class);
getApplicationContext().startService(service);
Run Code Online (Sandbox Code Playgroud)

服务:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

清单声明:

<service
    android:name=".MyService"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
</service>
Run Code Online (Sandbox Code Playgroud)

是否可以像应用程序暂停和其他任何操作一样运行此服务.一段时间后,我的应用程序暂停,服务也暂停或停止.那么如何在后台运行此服务并始终如此.

Ste*_*ker 88

"是否可以像应用程序暂停和其他任何事情一样运行此服务?"

是.

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

    public int onStartCommand(Intent intent, int flags, int startId) {
            return START_STICKY;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 使用startService(MyService)在后台启动服务,使其始终保持活动状态,而不管绑定客户端的数量.

    Intent intent = new Intent(this, PowerMeterService.class);
    startService(intent);
    
    Run Code Online (Sandbox Code Playgroud)
  3. 创建活页夹.

    public class MyBinder extends Binder {
            public MyService getService() {
                    return MyService.this;
            }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  4. 定义服务连接.

    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;
            }
    };
    
    Run Code Online (Sandbox Code Playgroud)
  5. 使用bindService绑定到服务.

            Intent intent = new Intent(this, MyService.class);
            bindService(intent, m_serviceConnection, BIND_AUTO_CREATE);
    
    Run Code Online (Sandbox Code Playgroud)
  6. 对于您的服务,您可能需要通知,以便在关闭后启动相应的活动.

    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());
    }
    
    Run Code Online (Sandbox Code Playgroud)
  7. 您需要修改清单以单顶模式启动活动.

              android:launchMode="singleTop"
    
    Run Code Online (Sandbox Code Playgroud)
  8. 请注意,如果系统需要资源而您的服务不是非常活跃,则可能会被终止.如果这是不可接受的,请使用startForeground将服务带到前台.

            startForeground(NOTIFICATION_ID, m_notificationBuilder.build());
    
    Run Code Online (Sandbox Code Playgroud)

  • 你能详细说明#8吗?这是在哪里执行的?关于#5,这是在与启动服务的地方相同的地方执行的吗?我是否正确理解您使用通知来始终保持服务的活跃性? (21认同)
  • @Stephen Donecker为什么我要绑定它? (2认同)
  • @SilviaHisham你没有.绑定通常在服务需要与活动通信时完成 (2认同)

Jua*_*uan 8

要在其自己的进程中启动服务,必须在xml声明中指定以下内容.

<service
  android:name="WordService"
  android:process=":my_process" 
  android:icon="@drawable/icon"
  android:label="@string/service_name"
  >
</service> 
Run Code Online (Sandbox Code Playgroud)

在这里你可以找到一个对我很有用的好教程

http://www.vogella.com/articles/AndroidServices/article.html

希望这可以帮助