无法得到服务对象(onServiceConnected永远不会被调用) [英] Can't get service object (onServiceConnected never called)

查看:745
本文介绍了无法得到服务对象(onServiceConnected永远不会被调用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写一个XML的日志在Android上phonestates。首先,我写了一个类来记录事情,而该活动是有效的。现在,我试着写在XML文件中的服务的服务绑定到活动中。后来我想登录状态的背景,所以我将需要至少一个服务和广播reciever。我却在绑定,并在onServiceConnected()不能得到服务对象回来 - 方法。该方法不会被调用。我试图等待异步启动了服务的,就像我在另一篇文章阅读,但没有奏效。
我用eclipse和正常工作的调试器。

I try to write a XML-log for phonestates on android. First i wrote a a single class to log things while the activity is active. Now i tried to write the xml-file in a service an bind the service to the activity. Later i want to log the states in background, so i will need at least one service and a broadcast reciever. But i fail at the binding and can't get a service object back at the onServiceConnected() - method. The method is never called. I tried to wait for the asynchronous start up of the service, like i read in another post, but it didn't work. I work with eclipse and the normal debugger.

public class MyActivity extends Activity {
//The layout of the application.
private TableLayout t;
//The service, which is writing the xml-file.
private FileService fservice;
private boolean mIsBound;
private final Handler handler = new Handler(); 

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.
        fservice = ((FileService.LocalBinder)service).getService();

    }

    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.
        fservice = null;
    }
};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.table_layout);

    doBindService();

    //Initialization of the TableLayout. 
    t = (TableLayout) findViewById(R.id.myTableLayout);
}

@Override
public void onStart(){
    super.onStart();

    //Initialization of the TelephonyManager.
    final TelephonyManager myTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

    Runnable r = new Runnable()
    {
            public void run()
            {
                //Gets the IMSI and IMEI.
                String imsi = myTelephonyMgr.getSubscriberId();
                createRow(getTimeStamp(), "IMSI: "+imsi);
                createLine();
                String imei = myTelephonyMgr.getDeviceId();
                createRow(getTimeStamp(), "IMEI: "+imei);
                createLine();

                //Gets the cell informations.
                GsmCellLocation cell = (GsmCellLocation) myTelephonyMgr.getCellLocation();
                createRow(getTimeStamp(),"CID: "+cell.getCid());
                createLine();
                createRow(getTimeStamp(), "LAC: "+cell.getLac());
                createLine();

                //Gets the call states.
                myTelephonyMgr.listen(new MyPhoneStateListener(MyActivity.this), PhoneStateListener.LISTEN_CALL_STATE);
            }
    };

    handler.postDelayed(r, 4000);


}

/**
 * Creates a new TableRow.
 * 
 * @param stampText
 * @param actionText
 */
protected void createRow(String stampText, String actionText){
    ...
    fservice.createRow(stampText, actionText);
}

...


private 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(MyActivity.this, FileService.class), mConnection, Context.BIND_AUTO_CREATE);
    mIsBound = true;
}

private void doUnbindService() {
    if (mIsBound) {
        // Detach our existing connection.
        unbindService(mConnection);
        mIsBound = false;
    }
}


@Override
protected void onDestroy() {
    super.onDestroy();
    doUnbindService();
} 

}

公共类的FileService延伸服务{
...
    //这是从客户端接收的交互的对象。
    私人最终的IBinder mBinder =新LocalBinder();

public class FileService extends Service{ ... // This is the object that receives interactions from clients. private final IBinder mBinder = new LocalBinder();

/**
 * Class for clients to access.  Because we know this service always
 * runs in the same process as its clients, we don't need to deal with
 * IPC.
 */
public class LocalBinder extends Binder {
    FileService getService() {
        return FileService.this;
    }
}

@Override
public IBinder onBind(Intent intent) {
    return mBinder;

}
...

}

推荐答案

你的服务定义添加到的Andr​​oidManifest.xml 文件?像这样:

Did you add the Service definition to the AndroidManifest.xml file? Like so:

<manifest ... >
  ...
  <application ... >
      <service android:name=".ExampleService" />
      ...
  </application>
</manifest>

这篇关于无法得到服务对象(onServiceConnected永远不会被调用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆