Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. public class MainActivity extends Activity {
  2.  
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. MainService.get(this);
  8. }
  9.  
  10. }
  11.  
  12. public class MainService extends Service {
  13. public static Object sWait = new Object();
  14. public static MainService instance;
  15.  
  16. @Override
  17. public IBinder onBind(Intent intent) {
  18. return null;
  19. }
  20.  
  21. public static MainService get(Context mContext) {
  22. if (instance == null) {
  23. Intent intent = new Intent(mContext, MainService.class);
  24. mContext.startService(intent);
  25. }
  26. while (true) {
  27. if (instance != null) {
  28. Log.v("myLogs", "all is good!");
  29. break;
  30. }
  31. synchronized (sWait) {
  32. try {
  33. sWait.wait();
  34. } catch (InterruptedException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. }
  39. return instance;
  40. }
  41.  
  42. @Override
  43. public void onCreate() {
  44. Log.v("myLogs", "created!");
  45. instance = this;
  46. synchronized (sWait) {
  47. sWait.notify();
  48. }
  49. }
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement