一、实验内容
- 使用“SimpleRandomServiceDemo”程序显式启动服务在应用程序中建立Service?
- 使用“ThreadRandomServiceDemo”程序使用线程持续产生随机数?
- 使用“SimpleMathServiceDemo”程序使用绑定方式使用Service?
- 使用“RemoteMathServiceDemo”程序,说明如何创建跨进程?
二、实验仪器、设备
硬件:PC 微型计算机、1G以上内存,40G以上硬盘
软件:Windows XP,Eclipse , JDK , Android SDK
三、实验步骤
1.使用“SimpleRandomServiceDemo”程序显式启动服务在应用程序中建立Service
1)创建SimpleRandomServiceDemo工程
打开Android Studio,点击Start a new Android Studio project;
勾选Phone and Tablet,点击next;
勾选Empty Activity,点击next;
在Activity Name中填写SimpleRandomServiceDemo,在Layout Name中添加main,点击finish。
2)程序代码:
MainActivity.java:
package com.example.simplerandomservicedemo;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startButton = (Button) findViewById(R.id.start);
Button stopButton = (Button) findViewById(R.id.stop);
final Intent serviceIntent = new Intent(this, RandomService.class);
startButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view) {
startService(serviceIntent);
}
});
stopButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view) {
stopService(serviceIntent);
}
});
}
}
RandomService.java:
package com.example.simplerandomservicedemo;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;
public class RandomService extends Service{
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "(1) 调用onCreate()",
Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Toast.makeText(this, "(2) 调用onStart()",
Toast.LENGTH_SHORT).show();
double randomDouble = Math.random();
String msg = "随机数:"+ String.valueOf(randomDouble);
Toast.makeText(this,msg, Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "(3) 调用onDestroy()",
Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.simplerandomservicedemo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".RandomService"/>
</application>
</manifest>
Strings.xml:
<resources>
<string name="hello">Hello World,SimpleRandomServiceDemo</string>
<string name="app_name">SimpleRandomServiceDemo</string>
</resources>
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动Service"/>
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止Service"/>
</LinearLayout>
3)运行截图分析:
在程序启动时点击“启动SERVICE”调用onCreate()、onStart()函数,并开时产生随机数,点击“停止SERVICE”按钮调用onDestory()函数,停止随机数的产生。
2.使用“ThreadRandomServiceDemo”程序使用线程持续产生随机数?
1)创建ThreadRandomServiceDemo工程
打开Android Studio,点击Start a new Android Studio project;
勾选Phone and Tablet,点击next;
勾选Empty Activity,点击next;
在Activity Name中填写ThreadRandomServiceDemo,在Layout Name中添加main,点击finish。
2)程序代码:
MainActivity.java:
package com.example.threadrandomservicedemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity