Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
TEACHING-MATERIALS/Week_09_Services_and_wearables/MyBoundServices/app/src/main/java/com/example/jianhuayang/myboundservices/LocalService.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
40 lines (33 sloc)
1.03 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.jianhuayang.myboundservices; | |
import android.app.Service; | |
import android.content.Intent; | |
import android.os.Binder; | |
import android.os.IBinder; | |
import java.util.Random; | |
/** | |
* Created by jianhuayang on 18/11/2017. | |
*/ | |
public class LocalService extends Service { | |
// Binder given to clients | |
private final IBinder mBinder = new LocalBinder(); | |
// Random number generator | |
private final Random mGenerator = new Random(); | |
/** | |
* Class used for the client Binder. 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 { | |
LocalService getService() { | |
// Return this instance of LocalService so clients can call public methods | |
return LocalService.this; | |
} | |
} | |
@Override | |
public IBinder onBind(Intent intent) { | |
return mBinder; | |
} | |
/** method for clients */ | |
public int getRandomNumber() { | |
return mGenerator.nextInt(100); | |
} | |
} |