Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android getApplicationContext()。。。强行关闭?_Android - Fatal编程技术网

Android getApplicationContext()。。。强行关闭?

Android getApplicationContext()。。。强行关闭?,android,Android,我正在我的应用程序中实现服务绑定。但是,当我启动绑定到服务的活动时,应用程序强制关闭。Ive pin指出这是由于getApplicationContext()引起的。。。这是我的代码,在哪里调用和使用。。。 感谢所有的帮助。 谢谢 为了将服务与活动绑定,您应该使用getBaseContext()或this关键字,而不是使用getApplicationContext(),如果您想启动服务,请在活动内调用startService方法。砰,砰,砰!使用doBindService();然后mBoundS

我正在我的应用程序中实现服务绑定。但是,当我启动绑定到服务的活动时,应用程序强制关闭。Ive pin指出这是由于getApplicationContext()引起的。。。这是我的代码,在哪里调用和使用。。。 感谢所有的帮助。 谢谢


为了将服务与活动绑定,您应该使用
getBaseContext()
this
关键字,而不是使用
getApplicationContext()
,如果您想启动服务,请在活动内调用
startService
方法。砰,砰,砰!使用doBindService();然后mBoundService.onStart(intent,0,0);谢谢
 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();
 }