gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 13:17:14 26 4
gpt4 key购买 nike

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

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

启动服务:

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;
}
}

list 声明:

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

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

最佳答案

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

是的。

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

    public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
    }
  2. 使用 startService(MyService) 在后台启动服务,这样无论绑定(bind)客户端的数量如何,它都始终保持 Activity 状态。

    Intent intent = new Intent(this, PowerMeterService.class);
    startService(intent);
  3. 创建 Binder 。

    public class MyBinder extends Binder {
    public MyService getService() {
    return MyService.this;
    }
    }
  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;
    }
    };
  5. 使用 bindService 绑定(bind)到服务。

            Intent intent = new Intent(this, MyService.class);
    bindService(intent, m_serviceConnection, BIND_AUTO_CREATE);
  6. 对于您的服务,您可能希望收到通知,以便在其关闭后启动相应的 Activity 。

    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());
    }
  7. 您需要修改 list 以在单顶模式下启动 Activity 。

              android:launchMode="singleTop"
  8. 请注意,如果系统需要资源并且您的服务不是很活跃,它可能会被终止。如果这是 Not Acceptable ,则使用 startForeground 将服务置于前台。

            startForeground(NOTIFICATION_ID, m_notificationBuilder.build());

关于java - Android 服务需要始终运行(从不暂停或停止),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15758980/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com