[英]What am I doing wrong in my service?
這是我首次創建服務並將其合並到我的應用程序中的嘗試。 由於某種原因,我無法調用任何公共方法(用於該位置)。
活動
public class MileageActivityV2 extends Activity {
MileageService mService;
Boolean mBound;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.example_activity);
}
@Override
protected void onStart() {
super.onStart();
// bind to MileageService
Intent intent = new Intent(this, MileageService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
mService.getCurrentLocation();
}
@Override
protected void onStop() {
super.onStop();
// unbind from the service
if (mBound) {
unbindService(mConnection);
mBound = false;
}
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
// get the MileageService instance
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
}
服務
public class MileageService extends Service implements IMileageService{
private final IBinder binder = new LocalBinder();
private List<Location> allLocations;
private Location startLocation;
private Location lastLocation;
private Location currentLocation;
private float totalDistance = 0;
NotificationManager nMgr;
/**
* Class used for the client Binder. Since we know this service always runs
* in the same process as the clients, dealing with IPC is not necessary.
*/
public class LocalBinder extends Binder {
public MileageService getService() {
// return this instance of MileageService so clients can call public methods
return MileageService.this;
}
}
@Override
public void onCreate() {
nMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// display a notification about the service starting
showNotification();
}
@Override
public void onDestroy() {
// cancel the persistent notification
nMgr.cancel(R.string.mileage_service_started);
// tell the user we stopped
Toast.makeText(this, R.string.mileage_service_stopped, Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
/**
* Show a notification while this service is running.
*/
private void showNotification() {
// In this sample, we'll use the same text for the ticker and the
// expanded notification
CharSequence text = getText(R.string.mileage_service_started);
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.car_1, text, System.currentTimeMillis());
// The PendingIntent to launch our activity if the user selects this
// notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MileageActivity.class), 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, getText(R.string.mileage_service_started), text, contentIntent);
// Send the notification.
// We use a string id because it is a unique number. We use it later to
// cancel.
nMgr.notify(R.string.mileage_service_started, notification);
}
private static final int BUMP_MSG = 1;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case BUMP_MSG:
break;
default:
super.handleMessage(msg);
}
}
};
@Override
public List<Location> getLocations() {
return this.allLocations;
}
@Override
public Location getCurrentLocation() {
return this.currentLocation;
}
@Override
public Location getStartLocation() {
return this.startLocation;
}
@Override
public Location getLastLocation() {
return this.lastLocation;
}
@Override
public Float getDistance() {
return this.totalDistance;
}
}
您的問題是在您的onStart方法中,您的服務實際上尚未啟動。 通話
Intent intent = new Intent(this, MileageService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
是異步的。 您需要等待服務綁定,然后才能對此調用任何方法。 有很多方法可以做到這一點,但是最快的方法是添加調用mService.getCurrentLocation();
到onServiceConnected
方法的末尾。 請注意,這將在應用程序的主線程上運行,因此請勿在其上進行阻塞操作。
隨着您的前進,您可能會發現需要從連接結束處啟動一個異步任務,以完成所需的所有工作而不影響ui。 網上有一些例子。
您不應從我的UI線程之外的其他線程中調用處理UI的UI方法。 而且您的服務當然不是ibn UI線程。 如果要從服務線程修改UI,則必須將異步任務發布到UI線程。 例如:
http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.