gpt4 book ai didi

android - 为什么 Service 在 Activity 被销毁时会 self 销毁?

转载 作者:行者123 更新时间:2023-11-30 00:12:17 25 4
gpt4 key购买 nike

我想从一个 Activity 启动一个服务,然后让它在 Activity 退出/完成时继续运行。然后,当我再次启动应用 Activity 时,它会重新附加到现有服务。

我已经阅读了有关 bindService 的文档,但每次我点击后退按钮退出 Activity 时,都会自动调用服务的 onUnBind,从而有效地终止服务。我想我必须在服务解除绑定(bind)之前手动调用 unbindService。

我确实知道,一旦 Activity 被销毁,成员变量 mService、mBound 和 tv 就必须在新 Activity 创建后重新填充,因此下面的简单代码将显示 “bindService 已启动”也就不足为奇了。 " 但我本以为 LocalService 实例中的所有成员变量都是相同的,没有变化,所以 "bindService 已经启动。Val = "+ mService.myval; 应该保持每次我重新启动一个新的 Activity 时都一样,不是吗?

主 Activity

public class MainActivity extends AppCompatActivity
{
LocalService mService;
Boolean mBound = false;
TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tv = (TextView)findViewById(R.id.textView);
}

@Override
protected void onStart()
{
super.onStart();
}

@Override
protected void onStop()
{
super.onStop();
}

@Override
protected void onDestroy()
{
super.onDestroy();
}

public void onButtonStart(View v)
{
String str;

// Bind to LocalService
if (!mBound)
{
Intent intent = new Intent(this, LocalService.class);

if (!bindService(intent, mConnection, Context.BIND_AUTO_CREATE))
{
unbindService(mConnection);
str = "bindService failed?!";
}
else
{
str = "bindService started.";
}
}
else
str = "bindService already Started. Val = " + mService.myval;

tv.setText(str);
}

public void onButtonStop(View v)
{
if (mBound)
{
mService.stop();
unbindService(mConnection);
mBound = false;

tv.setText("Stopped Service");
}
else
tv.setText("Service is not running");
}

private ServiceConnection mConnection = new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName className, IBinder service)
{
// We've bound to LocalService, cast the IBinder and get LocalService instance
LocalService.LocalBinder binder = (LocalService.LocalBinder) service;
mService = binder.getService();
mBound = true;
}

@Override
public void onServiceDisconnected(ComponentName arg0)
{
mBound = false;
}
};
}

本地服务

public class LocalService extends Service
{
private final IBinder mBinder = new LocalBinder();
private final Random mGenerator = new Random();
public int myval = getRandomNumber();

@Override
public void onCreate()
{
super.onCreate();
}

// Service is being Destroyed
@Override
public void onDestroy()
{
super.onDestroy();
}

public int getRandomNumber()
{
return mGenerator.nextInt(100);
}

@Override
public IBinder onBind(Intent intent)
{
return mBinder;
}

// Client is Unbinding via the unbindService() call
@Override
public boolean onUnbind(Intent intent)
{
return super.onUnbind(intent);
}

public class LocalBinder extends Binder
{
public LocalService getService()
{
return LocalService.this;
}
}
}

最佳答案

正如我所见,您正在使用 bindService()

A bound service typically lives only while it serves another application component and does not run in the background indefinitely.

因此,如果您的 Activity (绑定(bind)到服务)完成,则服务将不再运行。

要在后台运行服务,您必须创建带有标志 START_STICKYSTICKY 服务。

 @Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Do your task here
return START_STICKY;
}

START_STICKY 服务可以在后台运行而无需任何 Activity 。您可以通过回调与服务通信。

要创建一个粘性服务,请阅读Service .

关于android - 为什么 Service 在 Activity 被销毁时会 self 销毁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47957887/

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