gpt4 book ai didi

android - 无法从应用程序类向 Android 服务广播 Intent

转载 作者:太空狗 更新时间:2023-10-29 16:23:27 25 4
gpt4 key购买 nike

我有一个设置,我在 Android 中运行后台服务,附加的 BroadcastReceiver 监听特定类型的 Intent。然后广播接收器使用这些 Intent 来调用服务中的特定方法。下面是描述服务结构的代码:

public class MyService extends Service {

private class ServiceBroadcastReceiver extends BroadcastReceiver {


private MyService getMyService() {
return MyService.this;
}

public IntentFilter getASReceiverFilter() {
IntentFilter filter = new IntentFilter()
filter.addAction(Constants.ACTION_DO_SOMETHING);
return filter;
}

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
MyService svc = getMyService();
String intentAction = intent.getAction();
Log.i(Constants.LOG_TAG, "Now trying to dispatch following action: " + intentAction);
//Dispatch to the appropriate method in MyService.
if (intentAction.equals(AppServiceConstants.ACTION_DO_SOMETHING)) {
svc.doSomething();
}
}
}


private ServiceBroadcastReceiver mRequestReceiver = new ServiceBroadcastReceiver();


@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}


@Override
public void onCreate() {

Log.i(Constants.LOG_TAG, "Service Created!");
//Register for broadcast messages indicating the add music button was pressed
mRequestReceiver = new ServiceBroadcastReceiver();
registerReceiver(mRequestReceiver, mRequestReceiver.getASReceiverFilter());
}



@Override
public int onStartCommand(Intent intent, int flags, int startId) {

Log.i(Constants.LOG_TAG, "Service Starting ... ");

// If we get killed, after returning from here, restart
return START_STICKY;
}



/***********************************************************************************
* Service Dispatch Methods
*/
protected void doSomething() {
Log.i(Constants.LOG_TAG, "Doing something");
}



}

我的问题是让这项服务及其广播接收器从我的整个应用程序中捕获广播的 Intent 。如果我做类似下面的事情......

getApplicationContext().sendBroadcast(new Intent(Constants.ACTION_DO_SOMETHING));

.. 只有在我从 Activity 中调用它时它才有效。但是,我希望即使在应用程序首次启动时也能“做某事”,所以我在创建 MyService 并启动它之后立即将该语句放在 onCreate() 方法中我自己的自定义 Application 子类中。出于某种原因,当应用程序启动时,广播在 Application 类代码中不起作用,但如果我将它放在 Activity 中的某些代码中(例如在 Activity 的 onCreate() 方法中),它就会起作用。我可以确认在我的 Application 对象中广播 Intent 之前调用了服务的 onCreate() 和 onStartCommand() 方法,但它似乎根本没有获得 Intent 并执行相关工作。

如有任何帮助,我们将不胜感激。

更新:

下面是我如何从我的应用程序发送广播(注意我先启动服务):

 public class MyApplication extends Application {
@Override
public void onCreate() {
Intent intent = new Intent(getApplicationContext(), MyService.class);
getApplicationContext().startService(intent);
getApplicationContext().sendBroadcast(new Intent(Constants.ACTION_DO_SOMETHING));

}

}

请注意,我是在实际进行广播之前启动服务的。该服务不接收广播,但如果我从稍后启动的 Activity 中发出类似的调用,它会收到。

最佳答案

您的 Application 在此过程中先于任何其他组件创建。所以在 onCreate() 中没有 Serivce 运行。如果您在调用应用程序的 onCreate() 时发送广播,则 Service 没有注册广播接收器,因为它甚至还没有创建。

因此,换句话说,您的服务没有收到广播通知,因为广播接收器尚未注册。而且它还没有注册,因为 Service 还没有启动。并且它不能在应用程序的 onCreate() 设计返回之前启动。

您可以使用应用程序的 onCreate() 中的 Context.startService() 直接启动服务。您将能够通过 Intent 访问所有必需的数据。


更新问题的更新答案:

当你打电话时:

getApplicationContext().startService(intent);

您要求 Android 稍后启动服务。服务没有立即启动。它只计划在以后开始。事实上,在您从 Application 的 onCreate() 函数返回之前,它肯定不会启动。这是因为所有服务的生命周期回调函数(如 onCreate()onStart())都是从主 UI 线程调用的。 Applicaiton 的 onCreate 当前正在阻塞主 UI 线程。

因此当您使用这些代码行发送广播时:

getApplicationContext().startService(intent);
getApplicationContext().sendBroadcast(new Intent(Constants.ACTION_DO_SOMETHING));

您实际上要求在稍后的某个时间开始服务,然后您立即发送广播。而且因为服务甚至没有机会启动和注册这个广播通知,当它真正开始时它不会收到任何东西。

您可以更改服务的 onStart() 代码来处理 Intent ,然后使用以下方式启动服务:

Intent intent = new Intent(getApplicationContext(), MyService.class);
intent.setAction(Constants.ACTION_DO_SOMETHING);
getApplicationContext().startService(intent);

关于android - 无法从应用程序类向 Android 服务广播 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8537630/

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