我有一个安装程序,我在Android中运行后台服务,附带一个BroadcastReceiver监听特定类型的Intents。这些意图然后由广播接收机用于调用服务中的特定方法。以下是描述服务结构的代码:从应用程序类广播意图到Android服务的麻烦
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");
}
}
我的问题是让这个服务和它的广播接收器捕获整个我的应用程序广播的意图。如果我做了类似以下的事情......
getApplicationContext().sendBroadcast(new Intent(Constants.ACTION_DO_SOMETHING));
..它只适用于如果我从一个活动内部调用它。但是,即使在应用程序首次启动时,我也希望能够“执行某些操作”,因此在创建MyService并启动它之后,我已将该语句放入onCreate()方法的自定义Application子类中。出于某种原因,当应用程序启动时,广播不能在Application类代码中工作,但是如果我将它放在某个活动内的某个代码中(例如在活动的onCreate()方法中),它就会工作。我可以确认服务的onCreate()和onStartCommand()方法在我的应用程序对象中广播意图之前被调用,但它似乎并没有得到意图并执行相关的工作。
任何帮助,将不胜感激。
更新:
这里是如何从我的应用程序发送的广播(请注意,我第一次启动该服务):
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));
}
}
注意,我开始服务其实我做之前,广播。该服务不接收广播,但如果我从稍后启动的活动执行类似的呼叫,它确实收到。
见我的更新上面,我会尽力实际广播之前启动该服务。 – Faisal 2011-12-16 19:27:23