在我的Android应用程序中,我有一个类,它扩展android.app.Application
,并在其onCreate()
中绑定了一些服务,我的其他活动将在此应用程序中使用这些服务。何时调用Application的onCreate()方法?
另外我有一个BroadcastReceiver
它侦听并接收C2DM消息。当应用程序未运行时,此接收器接收到消息时,它将激活一个显示即将发生的消息的对话框,它将启动我的应用程序的“活动”。
我的问题是,当我开始一个没有任何与互动的活动时,我的的onCreate()
会被调用,因为该应用程序的一个Activity已经启动?
这里是我的的定义和清单:
public class DefaultApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
doBindService();
}
void doBindService() {
// Establish a connection with the service. We use an explicit
// class name because we want a specific service implementation that
// we know will be running in our own process (and thus won't be
// supporting component replacement by other applications).
bindService(new Intent(DefaultApplication.this, SocketService.class),
socketServiceConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
}
void doUnbindService() {
if (mIsBound) {
// Detach our existing connection.
unbindService(socketServiceConnection);
mIsBound = false;
}
}
}
清单如下:
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:name="com.mypackage.DefaultApplication"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:debuggable="true">
<service android:name="com.mypackage.services.SocketService"></service>
<activity android:name="TestActivity"
android:screenOrientation="landscape"></activity>
</application>
是的,它的工作原理是,我还没有看到其他别的... – atasoyh
我不unserstand为什么要扩展'Application'?你不应该延长'活动'吗? – Caner
@LAS_VEGAS在我的其余部分DefaultApplication中,我存储了一些数据并定义了一些在应用范围内使用的方法。我只把它的一部分复制到这里。 –