[英]Android bound service onStart does not work
I have a Service (Bound) which I am trying to start from my MainActivity.java. 我有一个服务(绑定),尝试从MainActivity.java开始。 The below is my Main Activity code:
以下是我的主要活动代码:
public class MainActivity extends AppCompatActivity {
Handler m_handler;
Runnable m_handlerTask ;
private static ServiceConnection sConnection;
static boolean serviceBound = false;
@Override
protected void onStart() {
super.onStart();
// Bind to LocalService
Intent intent = new Intent(this, DataDownloaderService.class);
bindService(intent, sConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.NoActionBar);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
if (!Global.isMyServiceRunning(DataDownloaderService.class, this)) {
sConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
DataDownloaderService.LocalBinder binder = (DataDownloaderService.LocalBinder) service;
Global.dService = binder.getService();
serviceBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
serviceBound = false;
}
};
}
}
catch(Exception ex ){
String p="";
}
}
@Override
protected void onResume(){
super.onResume();
m_handler = new Handler();
m_handlerTask = new Runnable()
{
@Override
public void run() {
RefreshNowPlaying();
m_handler.postDelayed(m_handlerTask, 1000);
}
};
m_handlerTask.run();
}
}
void RefreshNowPlaying(){
//Do something
}
}
The service code is as shown below: 服务代码如下所示:
public class DataDownloaderService extends Service {
private final IBinder dBinder = new LocalBinder();
public class LocalBinder extends Binder {
DataDownloaderService getService() {
// Return this instance of LocalService so clients can call public methods
return DataDownloaderService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return dBinder;
}
@Override
public void onCreate(){
}
@Override
public void onStart(Intent intent, int startid){
super.onStart(intent,startid);
Global.DataDownloaderServiceStatus = "STARTED";
}
@Override
public void onDestroy(){
Global.DataDownloaderServiceStatus = "STOPPED";
super.onDestroy();
}
}
I have also added the service to the AndroidManifest.xml under the Application element. 我还将服务添加到了Application元素下的AndroidManifest.xml中。 However, I see that the service's onStart is never fired or the override onServiceConnected in the MainActivity is never called.
但是,我看到该服务的onStart从未触发过,也从未调用过MainActivity中的重写onServiceConnected。 I am new to Anrdoid development and Java.
我是Anrdoid开发和Java的新手。 Can someone give me some pointers, please?
有人可以给我一些指示吗? I have noticed that the service's onCreate is getting hit, by putting breakpoints, but not onStart.
我注意到通过放置断点而不是onStart击中了服务的onCreate。
仅当通过context.startService()启动服务时,才会调用onStart()
,请参见此处 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.