2012-02-16 8 views
0

これはサービスを作成してアプリケーションに組み込む最初の試みです。なんらかの理由で、(場所の)パブリックメソッドを呼び出すことはできません。私のサービスで何が間違っていますか?

活動

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

答えて

2

あなたの問題は、あなたのONSTART方法では、あなたのサービスが実際に開始されていないということです。お電話

Intent intent = new Intent(this, MileageService.class); 
bindService(intent, mConnection, Context.BIND_AUTO_CREATE); 

は非同期です。このメソッドを呼び出す前に、サービスがバインドされるのを待つ必要があります。これを行う方法はたくさんありますが、onServiceConnectedメソッドの最後にmService.getCurrentLocation();コールを追加するのが最も簡単です。これはアプリのメインスレッドで実行されるので、ブロック操作をしないでください。

さらに進んでいくうちに、接続の最後から非同期タスクを開始して、uiに影響を与えずにすべての作業を行う必要があるかもしれません。ウェブの周りの例があります。

+0

サービスが接続されているかどうかを知るには良い、認識された方法がありますか?または、メソッドを接続するための「サービス待ち」? – Cody

+0

あなたが提案したものを試しました。私の 'getService'メソッドはnullを返します。 – Cody

+0

バインドの準備ができたら、サービスバインダーを使用してonServiceConnectedメソッドが呼び出されます。あなたの指示です。 onSeriveConnectedクラスの 'mService = binder.getService();'という行がnullを返すと言っていますか?もしそうなら、あなたの投稿をlogcatログの出力で更新してください。 –

-1

汝は私のUIスレッド以外のスレッドからUIを扱うUIのmethdodsを呼び出してはなりません。あなたのサービスは確かにUIスレッドではありません。サービススレッドからUIを変更する場合は、非同期タスクをUIスレッドに投稿する必要があります。これで例えば:

http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable

+0

私はUIを変更していません。私はメソッドを呼び出すことを試みているだけです。 – Cody

+0

@コンスタンチン私は、私はどちらかの変更を参照してくださいか分からない。私はトーストの呼び出しが重要だとは思わない。 –

+0

申し訳ありませんが、トースト通知は開発者のウェブサイトの例で使用されていました。さらに、私はそれが私のサービスが働かない原因になるとは思わない。 – Cody

関連する問題