1、在service中实现随机数产生;
2、实现Service中的各个生命周期函数,并理解其功能;
2、在Activity界面实现随机数的显示,每2秒更新一次;
3、采用启动式完成service的启动;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:id="@+id/txt"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="TextView" /><Buttonandroid:id="@+id/start"android:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="startService"android:text="startService"/><Buttonandroid:id="@+id/stop"android:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="stopService"android:text="stopService"/></LinearLayout>

MainActivity

package com.example.myapplication22;import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.content.Intent;
import android.os.IBinder;
import android.view.View;
import android.widget.TextView;public class MainActivity extends Activity {private TextView txt;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);txt = findViewById(R.id.txt);}private Handler mHandler = new Handler();Runnable runnable = new Runnable() {@Overridepublic void run() {txt.setText(String.valueOf(Math.random()*100));mHandler.postDelayed(this, 2000);}};Thread thread = new Thread(runnable);public void startService(View view) {startService(new Intent(getBaseContext(), MyServiceToStart.class));thread.start();}public void stopService(View view) {mHandler.removeCallbacks(runnable);stopService(new Intent(getBaseContext(), MyServiceToStart.class));}}

MyServiceToStart

package com.example.myapplication22;import android.app.Service;
import android.content.Intent;
import android.os.IBinder;import android.widget.Toast;public class MyServiceToStart extends Service {@Overridepublic IBinder onBind(Intent arg0) {return null;}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Toast.makeText(this, "服务已经启动", Toast.LENGTH_LONG).show();return START_STICKY;}@Overridepublic void onDestroy() {super.onDestroy();Toast.makeText(this, "服务已经停止", Toast.LENGTH_LONG).show();}}

Android在service中实现随机数产生相关推荐

  1. Android在Service中显示Dialog

    在Service中弹出一个Dialog对话框 第1步:在应用的AndroidManifest.xml中需要添加权限.没有无法显示. <uses-permission android:name=& ...

  2. Android在Service中注册动态广播接收者

    Android广播分为动态.静态广播. 广播接收器注册一共有两种形式 : 静态注册和动态注册. 两者及其接收广播的区别: 1.动态注册的广播 永远要快于 静态注册的广播,不管静态注册的优先级设置的多高 ...

  3. Android 在Service中使用bindService

    前面已经对Service的startServer方式启动一个服务了解过了,现在来看一下Service的另一种启动方式→bindServer bindServer使用场景 1.在同个app之间调用(即是 ...

  4. android init.rc中启动的service 默认是disable的,后续如何启动此服务

    如果 android init.rc中启动的service 默认是disable的,如何才能启动此服务呢? init.rc中可以直接启动service 附带的参数决定启动程序的状态,例如数据业务中配置 ...

  5. android service中显示一个dialog

    转自:http://blog.csdn.net/huxueyan521/article/details/8954844 dialog是依附于activity存在的.但是app中经常需要使用以下的情况, ...

  6. android Service中Thread.sleep不精确

    平台 RK3288 + Android 7.1 问题 在测试Thread.sleep过程中发现, 当App进入后台后, 服务中的Thread.sleep会有不同程度的精确度丢失. 测试sleep 2m ...

  7. Service中的绑定服务总结

    绑定服务是客户端服务器接口中的服务器,绑定服务可以让组件绑定到服务.发送请求.接收响应,甚至执行进程间通信IPC,绑定服务通常只在为其他应用组件服务时处于活动状态,不会无限期在后台运行. 绑定服务是S ...

  8. Android Studio掷骰子生成随机数(Java)

    Android Studio掷骰子生成随机数(Java) .xml <?xml version="1.0" encoding="utf-8"?> & ...

  9. Android Studio掷骰子生成随机数(图片版)(Java)

    Android Studio掷骰子生成随机数(图片版)(Java) .xml <?xml version="1.0" encoding="utf-8"?& ...

最新文章

  1. Redis在CentOS 6.8中的安装方法,JAVA初级使用Redis连接池
  2. 软件开发环境-工具集
  3. eclipce如何配置mysql_如何在eclipse配置mysql数据库
  4. 延长端粒续命有风险,科学家警告:端粒过长反而容易患癌
  5. 如何模拟将CPU、IO打满?
  6. java hashmap 去重复_为什么我在Java HashMap中得到重复的键?
  7. 多媒体(1):MCI接口编程
  8. C语言 va_start / va_end / va_arg 自定义 printf 函数 - C语言零基础入门教程
  9. CentOS中nginx负载均衡和反向代理的搭建
  10. call和calling的用法_call的用法及短语例句
  11. LOGO与BASIC语言编程入门pdf
  12. c语言程序中TMOD,keil 中用c 语言写的代码 error C231: 'TMOD': redefinition
  13. 那些年,我们一起做过的 Java 课后练习题(71 - 75)
  14. java源文件组成_java源文件由什么组成?,java源文件组成
  15. Google Play In-app Billing API version is less than 3.
  16. 想买个吉他英雄3的正版
  17. 访问任何dns都超时_如何使用动态DNS从任何地方轻松访问您的家庭网络
  18. 计算机桌面颜色比较暗,电脑显示器颜色偏暗怎么回事
  19. c语言程序设计 学籍,c语言学籍信息管理系统设计
  20. Photoshop滤镜给城市夜空添加满天星光

热门文章

  1. PLC/DCS系统常见的干扰现象及判断方法
  2. 快速容易地处理Windows、Mac 和Linux系统中文件路径问题
  3. 怎么判断机械表上满弦_如何判断机械表是否上满弦?
  4. 浅析游戏AI的原理与实现
  5. JVM:内存与垃圾回收篇
  6. 行云管家荣获CFS第十二届财经峰会 “2023产品科技创新奖”
  7. Proe Creo 二次开发之计算两个模型之间的干涉信息
  8. U8v13.0使用医院行业使用政府会计制度.财务预算单凭证,总账和明细账,多辅助账和明细账不平错误.
  9. 【软考系统架构设计师】2021年下系统架构师综合知识历年真题
  10. 第十五届“中国电机工程学会杯”数学建模竞赛