Skip to content

Commit 1070b87

Browse files
author
qianlq
committed
🍀 feat(进程内服务):提供进程服务绑定启动
1 parent df468d4 commit 1070b87

File tree

5 files changed

+128
-2
lines changed

5 files changed

+128
-2
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,14 @@
217217
</intent-filter>
218218
</activity>
219219

220+
<activity android:name=".chapter7.BundleActivity">
221+
<intent-filter>
222+
<action android:name="android.intent.action.VIEW" />
223+
224+
<category android:name="android.intent.category.LAUNCHER" />
225+
</intent-filter>
226+
</activity>
227+
220228
<provider
221229
android:name=".chapter6.providers.PeopleProvider"
222230
android:authorities="com.coderqian.chapter6.providers.PeopleProvider"
@@ -229,6 +237,8 @@
229237

230238
<service android:name=".chapter7.service.CompareService" />
231239

240+
<service android:name=".chapter7.service.Compare2Service" />
241+
232242
</application>
233243

234244
</manifest>

app/src/main/java/com/coderqian/MainActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class MainActivity extends EuclidActivity {
2121
"com.coderqian.chapter4.FragmentActivity,com.coderqian.chapter4.Fragment2Activity,com.coderqian.chapter4.IntentActivity,com.coderqian.chapter4.AnimatorActivity",
2222
"com.coderqian.chapter5.TweenActivity,com.coderqian.chapter5.FrameActivity,com.coderqian.chapter5.LeafLoadingActivity",
2323
"com.coderqian.chapter6.SQLiteActivity,com.coderqian.chapter6.ResolverActivity,com.coderqian.chapter6.StudentActivity",
24-
"com.coderqian.chapter7.ServiceActivity", "", ""};
24+
"com.coderqian.chapter7.ServiceActivity,com.coderqian.chapter7.BundleActivity", "", ""};
2525

2626
@Override
2727
protected void onCreate(Bundle savedInstanceState) {
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.coderqian.chapter7;
2+
3+
import android.content.ComponentName;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
import android.content.ServiceConnection;
7+
import android.os.Bundle;
8+
import android.os.IBinder;
9+
import android.support.v7.app.AppCompatActivity;
10+
import android.view.View;
11+
import android.widget.Button;
12+
import android.widget.EditText;
13+
import android.widget.TextView;
14+
15+
import com.coderqian.R;
16+
import com.coderqian.chapter7.service.Compare2Service;
17+
18+
/**
19+
* @author qianliqing
20+
* @since 2018/11/7 11:25 AM
21+
22+
*/
23+
24+
public class BundleActivity extends AppCompatActivity {
25+
26+
private boolean isBundle = false;
27+
28+
private Compare2Service compare2Service;
29+
30+
int a, b = 0;
31+
32+
@Override
33+
protected void onCreate(Bundle savedInstanceState) {
34+
super.onCreate(savedInstanceState);
35+
setContentView(R.layout.activity_service);
36+
37+
final TextView result = (TextView) findViewById(R.id.result);
38+
Button compare = (Button) findViewById(R.id.compare);
39+
final Button reset = (Button) findViewById(R.id.reset);
40+
final EditText one = (EditText) findViewById(R.id.one);
41+
final EditText two = (EditText) findViewById(R.id.two);
42+
43+
if (!isBundle) {
44+
Intent intent = new Intent(BundleActivity.this, Compare2Service.class);
45+
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
46+
}
47+
48+
compare.setOnClickListener(new View.OnClickListener() {
49+
@Override
50+
public void onClick(View view) {
51+
String c1 = one.getText().toString();
52+
String c2 = two.getText().toString();
53+
if (!c1.equals("") && !c2.equals("")) {
54+
a = Integer.parseInt(c1);
55+
b = Integer.parseInt(c2);
56+
}
57+
result.setText(String.valueOf(compare2Service.IntCompare(a, b)));
58+
}
59+
});
60+
61+
reset.setOnClickListener(new View.OnClickListener() {
62+
@Override
63+
public void onClick(View view) {
64+
one.setText(null);
65+
two.setText(null);
66+
result.setText(null);
67+
}
68+
});
69+
}
70+
71+
private ServiceConnection serviceConnection = new ServiceConnection() {
72+
@Override
73+
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
74+
compare2Service = ((Compare2Service.LocalBinder) iBinder).getService();
75+
}
76+
77+
@Override
78+
public void onServiceDisconnected(ComponentName componentName) {
79+
compare2Service = null;
80+
}
81+
};
82+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.coderqian.chapter7.service;
2+
3+
import android.app.Service;
4+
import android.content.Intent;
5+
import android.os.Binder;
6+
import android.os.IBinder;
7+
import android.support.annotation.Nullable;
8+
9+
/**
10+
* @author qianliqing
11+
* @since 2018/11/7 11:32 AM
12+
13+
*/
14+
15+
public class Compare2Service extends Service {
16+
17+
private final IBinder iBinder = new LocalBinder();
18+
19+
public class LocalBinder extends Binder {
20+
public Compare2Service getService() {
21+
return Compare2Service.this;
22+
}
23+
}
24+
25+
@Nullable
26+
@Override
27+
public IBinder onBind(Intent intent) {
28+
return iBinder;
29+
}
30+
31+
public int IntCompare(int a, int b) {
32+
return a >= b ? a : b;
33+
}
34+
}

app/src/main/java/com/coderqian/chapter7/service/CompareService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
/**
1212
* @author qianliqing
13-
* @date 2018/11/7 10:21 AM
13+
* @since 2018/11/7 10:21 AM
1414
1515
*/
1616

0 commit comments

Comments
 (0)