(五)后台服务

本文详细介绍了四个Android后台服务的实验,包括显式启动服务、使用线程产生随机数、绑定方式使用服务以及创建跨进程服务。每个实验都涵盖了创建过程、代码实现和运行分析,同时探讨了Service的原理、用途以及跨进程服务中自定义数据类型的使用。
摘要由CSDN通过智能技术生成

一、实验内容

  1. 使用“SimpleRandomServiceDemo”程序显式启动服务在应用程序中建立Service?
  2. 使用“ThreadRandomServiceDemo”程序使用线程持续产生随机数?
  3. 使用“SimpleMathServiceDemo”程序使用绑定方式使用Service?
  4. 使用“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 
  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值