私は自分のアプリケーションにサービスバインディングを実装しています。しかし、サービスにバインドする自分のアクティビティを開始すると、アプリケーションの強制は終了します。 Iveピンは、getApplicationContext()のためにそのことを指摘しました。私のコードをHeresし、呼び出され使用された場所... すべての助けがありがとうございます。代わりにgetApplicationContext()
を使用しての活動とサービスをバインドするために おかげgetApplicationContext()...強制終了しますか?
private LocalService mBoundService;
private boolean mIsBound;
Context context = getApplicationContext();
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with the service has been
// established, giving us the service object we can use to
// interact with the service. Because we have bound to a explicit
// service that we know is running in our own process, we can
// cast its IBinder to a concrete class and directly access it.
mBoundService = ((LocalService.LocalBinder)service).getService();
// Tell the user about this for our demo.
Context context = getApplicationContext();
Toast.makeText(context, "serviceconnected",
Toast.LENGTH_SHORT).show();
}
public void onServiceDisconnected(ComponentName className) {
// This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed.
// Because it is running in our same process, we should never
// see this happen.
mBoundService = null;
Toast.makeText(context, "serviceDisconnected",
Toast.LENGTH_SHORT).show();
}
};
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(context,
LocalService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
}
void doUnbindService() {
if (mIsBound) {
// Detach our existing connection.
unbindService(mConnection);
mIsBound = false;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
doUnbindService();
}
を使用する必要がありますしてください。.. –