gpt4 book ai didi

android - 无法获取服务对象(从未调用过 onServiceConnected)

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:15:32 26 4
gpt4 key购买 nike

我尝试为 Android 上的音素状态编写一个 XML 日志。首先,我编写了一个类来在 Activity 处于 Activity 状态时记录内容。现在我尝试在服务中编写 xml 文件并将服务绑定(bind)到 Activity 。后来我想在后台记录状态,所以我至少需要一个服务和一个广播接收器。但是我在绑定(bind)时失败了,无法在 onServiceConnected() - 方法中取回服务对象。该方法永远不会被调用。我试图等待服务的异步启动,就像我在另一篇文章中读到的那样,但它没有用。我使用 Eclipse 和普通调试器。

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();
}

公共(public)类 FileService 扩展服务{... //这是从客户端接收交互的对象。 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;

}
...

最佳答案

您是否将服务定义添加到 AndroidManifest.xml 文件?像这样:

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

关于android - 无法获取服务对象(从未调用过 onServiceConnected),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6008601/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com