Skip to content

Commit df468d4

Browse files
author
qianlq
committed
🍀 feat(进程内服务):提供进程内服务
1 parent e56ea57 commit df468d4

File tree

6 files changed

+231
-4
lines changed

6 files changed

+231
-4
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,14 @@
209209
</intent-filter>
210210
</activity>
211211

212+
<activity android:name=".chapter7.ServiceActivity">
213+
<intent-filter>
214+
<action android:name="android.intent.action.VIEW" />
215+
216+
<category android:name="android.intent.category.LAUNCHER" />
217+
</intent-filter>
218+
</activity>
219+
212220
<provider
213221
android:name=".chapter6.providers.PeopleProvider"
214222
android:authorities="com.coderqian.chapter6.providers.PeopleProvider"
@@ -219,6 +227,8 @@
219227
android:authorities="com.coderqian.chapter6.providers.StudentProvider"
220228
android:exported="true" />
221229

230+
<service android:name=".chapter7.service.CompareService" />
231+
222232
</application>
223233

224234
</manifest>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public class MainActivity extends EuclidActivity {
2020
"com.coderqian.chapter3.Tab2Activity,com.coderqian.chapter3.DialogActivity,com.coderqian.chapter3.ExpandableList2Activity,com.coderqian.chapter3.Launcher2Activity,com.coderqian.chapter3.List2Activity,com.coderqian.chapter3.Preference2Activity",
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",
23-
"com.coderqian.chapter6.SQLiteActivity,com.coderqian.chapter6.ResolverActivity,com.coderqian.chapter6.StudentActivity", "", "", ""};
23+
"com.coderqian.chapter6.SQLiteActivity,com.coderqian.chapter6.ResolverActivity,com.coderqian.chapter6.StudentActivity",
24+
"com.coderqian.chapter7.ServiceActivity", "", ""};
2425

2526
@Override
2627
protected void onCreate(Bundle savedInstanceState) {
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.coderqian.chapter7;
2+
3+
import android.content.Intent;
4+
import android.os.Bundle;
5+
import android.os.Handler;
6+
import android.support.v7.app.AppCompatActivity;
7+
import android.view.View;
8+
import android.widget.Button;
9+
import android.widget.EditText;
10+
import android.widget.TextView;
11+
12+
import com.coderqian.R;
13+
import com.coderqian.chapter7.service.CompareService;
14+
15+
/**
16+
* @author qianliqing
17+
* @date 2018/11/7 9:16 AM
18+
19+
*/
20+
21+
public class ServiceActivity extends AppCompatActivity {
22+
23+
public static int maxNum;
24+
25+
public static Handler handler = new Handler();
26+
27+
private static TextView result;
28+
29+
private Button compare;
30+
private Button reset;
31+
32+
private EditText one;
33+
private EditText two;
34+
35+
public static void updateGui(int refreshDouble) {
36+
maxNum = refreshDouble;
37+
handler.post(RefreshLable);
38+
}
39+
40+
public static Runnable RefreshLable = new Runnable() {
41+
public void run() {
42+
result.setText(String.valueOf(maxNum));
43+
}
44+
};
45+
46+
@Override
47+
protected void onCreate(Bundle savedInstanceState) {
48+
final Bundle bundle = new Bundle();
49+
super.onCreate(savedInstanceState);
50+
setContentView(R.layout.activity_service);
51+
52+
final Intent intent = new Intent(ServiceActivity.this, CompareService.class);
53+
54+
result = (TextView) findViewById(R.id.result);
55+
compare = (Button) findViewById(R.id.compare);
56+
reset = (Button) findViewById(R.id.reset);
57+
one = (EditText) findViewById(R.id.one);
58+
two = (EditText) findViewById(R.id.two);
59+
60+
compare.setOnClickListener(new View.OnClickListener() {
61+
@Override
62+
public void onClick(View view) {
63+
bundle.putString("one", one.getText().toString());
64+
bundle.putString("two", two.getText().toString());
65+
intent.putExtras(bundle);
66+
startService(intent);
67+
}
68+
});
69+
70+
reset.setOnClickListener(new View.OnClickListener() {
71+
@Override
72+
public void onClick(View view) {
73+
one.setText(null);
74+
two.setText(null);
75+
result.setText(null);
76+
}
77+
});
78+
}
79+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.coderqian.chapter7.service;
2+
3+
import android.app.Service;
4+
import android.content.Intent;
5+
import android.os.Bundle;
6+
import android.os.IBinder;
7+
import android.support.annotation.Nullable;
8+
9+
import com.coderqian.chapter7.ServiceActivity;
10+
11+
/**
12+
* @author qianliqing
13+
* @date 2018/11/7 10:21 AM
14+
15+
*/
16+
17+
public class CompareService extends Service {
18+
private Thread thread;
19+
Bundle bundle;
20+
int one = 0, two = 0;
21+
22+
@Nullable
23+
@Override
24+
public IBinder onBind(Intent intent) {
25+
return null;
26+
}
27+
28+
public void onCreate() {
29+
super.onCreate();
30+
thread = new Thread(null, backgroundWork, "workThread");
31+
}
32+
33+
public void onStart(Intent intent, int startId) {
34+
super.onStart(intent, startId);
35+
bundle = intent.getExtras();
36+
String c1 = bundle.getString("one");
37+
String c2 = bundle.getString("two");
38+
if (!c1.equals("") && !c2.equals("")) {
39+
one = Integer.parseInt(c1);
40+
two = Integer.parseInt(c2);
41+
}
42+
if (!thread.isAlive()) {
43+
thread.start();
44+
}
45+
}
46+
47+
private Runnable backgroundWork = new Runnable() {
48+
@Override
49+
public void run() {
50+
int randomDouble = IntCompare(one, two);
51+
ServiceActivity.updateGui(randomDouble);
52+
stopSelf();
53+
}
54+
};
55+
56+
int IntCompare(int a, int b) {
57+
return a >= b ? a : b;
58+
}
59+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:orientation="vertical">
6+
7+
<LinearLayout
8+
android:layout_width="match_parent"
9+
android:layout_height="wrap_content"
10+
android:orientation="horizontal">
11+
12+
<TextView
13+
android:id="@+id/text1"
14+
android:layout_width="wrap_content"
15+
android:layout_height="wrap_content"
16+
android:text="第一个整数 " />
17+
18+
<EditText
19+
android:id="@+id/one"
20+
android:layout_width="wrap_content"
21+
android:layout_height="wrap_content"
22+
android:hint="请输入第一个数"
23+
android:selectAllOnFocus="true" />
24+
</LinearLayout>
25+
26+
<LinearLayout
27+
android:layout_width="match_parent"
28+
android:layout_height="wrap_content"
29+
android:orientation="horizontal">
30+
31+
<TextView
32+
android:layout_width="wrap_content"
33+
android:layout_height="wrap_content"
34+
android:text="第二个整数 " />
35+
36+
<EditText
37+
android:id="@+id/two"
38+
android:layout_width="wrap_content"
39+
android:layout_height="wrap_content"
40+
android:hint="请输入第二个数"
41+
android:selectAllOnFocus="true" />
42+
</LinearLayout>
43+
44+
<LinearLayout
45+
android:layout_width="match_parent"
46+
android:layout_height="wrap_content"
47+
android:orientation="horizontal">
48+
49+
<TextView
50+
android:layout_width="wrap_content"
51+
android:layout_height="wrap_content"
52+
android:text="最大的整数 " />
53+
54+
<TextView
55+
android:id="@+id/result"
56+
android:layout_width="wrap_content"
57+
android:layout_height="wrap_content" />
58+
</LinearLayout>
59+
60+
<LinearLayout
61+
android:layout_width="match_parent"
62+
android:layout_height="wrap_content"
63+
android:orientation="horizontal">
64+
65+
<Button
66+
android:id="@+id/compare"
67+
android:layout_width="wrap_content"
68+
android:layout_height="wrap_content"
69+
android:text="比较" />
70+
71+
<Button
72+
android:id="@+id/reset"
73+
android:layout_width="wrap_content"
74+
android:layout_height="wrap_content"
75+
android:text="复原" />
76+
</LinearLayout>
77+
78+
</LinearLayout>

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<item>练习Fragment、Intent和各种资源的使用</item>
2424
<item>实现几种绘图和动画效果</item>
2525
<item>建立SQLite数据库和表,封装访问方法</item>
26-
<item>暂无</item>
26+
<item>简易网络聊天室应用</item>
2727
<item>暂无</item>
2828
<item>暂无</item>
2929
</string-array>
@@ -35,7 +35,7 @@
3535
<item>新建一个项目,练习Fragment、Intent和各种资源的使用</item>
3636
<item>新建一个项目,练习各种资源的使用,并实现几种绘图和动画效果</item>
3737
<item>建立一个ContentProvider对该数据库的访问进行封装,并提供访问接口</item>
38-
<item>暂无</item>
38+
<item>建立一个进程内服务,实现基于Socket编程的简易网络聊天室应用</item>
3939
<item>暂无</item>
4040
<item>暂无</item>
4141
</string-array>
@@ -50,7 +50,7 @@
5050
<item>1、参照chapter4、chapter5、chapter6实验手册,完成上机实验。\n2、新建一个项目,练习Fragment、Intent和各种资源的使用。</item>
5151
<item>1、参照chapter6、chapter7实验手册,完成上机实验。\n2、新建一个项目,练习各种资源的使用,并实现几种绘图和动画效果。</item>
5252
<item>1、参照chapter8、chapter9实验手册,完成上机实验。\n2、建立一个SQLite数据库,并建立至少一个表,向表内添加一些数据。建立一个ContentProvider对该数据库的访问进行封装,并提供访问接口。开发一个测试应用,能够通过ContentResolver对前一应用的数据库进行增删改查。</item>
53-
<item>暂无</item>
53+
<item>1、参照chapter10、chapter11、chapter12、chapter13实验手册,完成上机实验。\n2、编程建立一个进程内服务,服务提供int compare(int, int)函数,输入两个整数,输出较大的整数,添加一个Activity,与该Service绑定,并能够调用该Service中的compare函数,显示输出结果。\n3、基于Socket编程实现一个简易网络聊天室应用(包括服务器端和Android客户端)。</item>
5454
<item>暂无</item>
5555
<item>暂无</item>
5656
</string-array>

0 commit comments

Comments
 (0)