SHOW:
|
|
- or go back to the newest paste.
1 | package com.lacronicus.rxjavaleaktest.app; | |
2 | ||
3 | import android.app.Activity; | |
4 | import android.os.Bundle; | |
5 | import android.view.Menu; | |
6 | import android.view.MenuItem; | |
7 | import android.widget.Toast; | |
8 | ||
9 | import rx.Observable; | |
10 | import rx.Subscription; | |
11 | import rx.android.schedulers.AndroidSchedulers; | |
12 | - | import rx.functions.Action0; |
12 | + | |
13 | import rx.functions.Func1; | |
14 | import rx.schedulers.Schedulers; | |
15 | ||
16 | ||
17 | public class MainActivity extends Activity { | |
18 | Subscription s; | |
19 | ||
20 | //this represents a long-running background action. shouldn't have a reference to the activity | |
21 | private static class myMapFunction implements Func1<Integer, Integer> { | |
22 | @Override | |
23 | public Integer call(Integer integer) { | |
24 | int x = 1; | |
25 | while (x == 1) { | |
26 | try { | |
27 | Thread.sleep(10000); | |
28 | } catch (InterruptedException e) { | |
29 | e.printStackTrace(); | |
30 | } | |
31 | } | |
32 | return 1; | |
33 | } | |
34 | } | |
35 | ||
36 | ||
37 | @Override | |
38 | protected void onCreate(Bundle savedInstanceState) { | |
39 | super.onCreate(savedInstanceState); | |
40 | setContentView(R.layout.activity_main); | |
41 | s = Observable.from(1).observeOn(Schedulers.io()).map(new myMapFunction()).observeOn(AndroidSchedulers.mainThread()).subscribe( | |
42 | new Action1<Integer>() { | |
43 | @Override | |
44 | public void call(Integer integer) { | |
45 | Toast.makeText(MainActivity.this, "lol", Toast.LENGTH_SHORT).show(); | |
46 | } | |
47 | } | |
48 | - | }, new Action1<Throwable>() { |
48 | + | |
49 | } | |
50 | - | public void call(Throwable throwable) { |
50 | + | |
51 | @Override | |
52 | protected void onStop() { | |
53 | - | }, new Action0() { |
53 | + | |
54 | if (s != null) | |
55 | - | public void call() { |
55 | + | |
56 | } | |
57 | ||
58 | @Override | |
59 | public boolean onCreateOptionsMenu(Menu menu) { | |
60 | // Inflate the menu; this adds items to the action bar if it is present. | |
61 | getMenuInflater().inflate(R.menu.main, menu); | |
62 | return true; | |
63 | } | |
64 | ||
65 | @Override | |
66 | public boolean onOptionsItemSelected(MenuItem item) { | |
67 | // Handle action bar item clicks here. The action bar will | |
68 | // automatically handle clicks on the Home/Up button, so long | |
69 | // as you specify a parent activity in AndroidManifest.xml. | |
70 | int id = item.getItemId(); | |
71 | if (id == R.id.action_settings) { | |
72 | return true; | |
73 | } | |
74 | return super.onOptionsItemSelected(item); | |
75 | } | |
76 | ||
77 | } |