-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathMainActivity.java
129 lines (98 loc) · 3.28 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.example.ndkbinderclient;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.ConditionVariable;
import android.os.IBinder;
import android.util.Log;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.Constants;
public class MainActivity extends AppCompatActivity implements ServiceConnection
{
// Used to load the 'native-lib' library on application startup.
static
{
System.loadLibrary("native-lib");
}
private volatile boolean mIsServiceConnected = false;
private final ConditionVariable mServiceConnectionWaitLock = new ConditionVariable();
private TextView mTV = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTV = findViewById(R.id.sample_text);
}
@Override
protected void onResume()
{
super.onResume();
Intent intent = new Intent();
intent.setClassName("com.example.javabinderservice",
"com.example.javabinderservice.MyService");
Log.d(Constants.LOG_TAG, "[App] [java] bindService");
bindService(intent, this, BIND_AUTO_CREATE);
new Thread(new Runnable()
{
@Override
public void run()
{
runOnUiThread(new SetTextRunnable("Waiting to talk to IMyService..."));
// Not connected to service yet?
while(!mIsServiceConnected)
{
mServiceConnectionWaitLock.block(); // waits for service connection
}
String returnedString = talkToService();
runOnUiThread(new SetTextRunnable("Talked to IMyService. Returned : " + returnedString));
}
}).start();
}
@Override
protected void onPause()
{
unbindService(this);
mIsServiceConnected = false;
onServiceDisconnected();
Log.d(Constants.LOG_TAG, "[App] [java] unbindService");
super.onPause();
}
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder)
{
Log.d(Constants.LOG_TAG, "[App] [java] onServiceConnected");
onServiceConnected(iBinder);
mIsServiceConnected = true;
mServiceConnectionWaitLock.open(); // breaks service connection waits
}
@Override
public void onServiceDisconnected(ComponentName componentName)
{
mIsServiceConnected = false;
onServiceDisconnected();
Log.d(Constants.LOG_TAG, "[App] [java] onServiceDisconnected");
}
private class SetTextRunnable implements Runnable
{
final String mText;
SetTextRunnable(String text)
{
mText = text;
}
@Override
public void run()
{
mTV.setText(mText);
}
}
/**
* A native methods that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native void onServiceConnected(IBinder binder);
public native void onServiceDisconnected();
public native String talkToService();
}