Android在service中实现随机数产生

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="TextView" />
    <Button
        android:id="@+id/start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="startService"
        android:text="startService"
        />

    <Button
        android: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;

    @Override
    public 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() {
        @Override
        public 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 {

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "服务已经启动", Toast.LENGTH_LONG).show();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "服务已经停止", Toast.LENGTH_LONG).show();
    }



}
  • 7
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用以下代码在 Android Studio 创建一个 Service,该 Service 会生成一个随机数并将其返回: ``` public class RandomNumberService extends Service { private final IBinder mBinder = new RandomNumberBinder(); private final Random mGenerator = new Random(); public class RandomNumberBinder extends Binder { RandomNumberService getService() { return RandomNumberService.this; } } @Override public IBinder onBind(Intent intent) { return mBinder; } public int getRandomNumber() { return mGenerator.nextInt(); } } ``` 在您的 Activity ,您可以使用以下代码来绑定 Service 并获取随机数: ``` private boolean mBound = false; private RandomNumberService mService; private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName className, IBinder service) { RandomNumberBinder binder = (RandomNumberBinder) service; mService = binder.getService(); mBound = true; } @Override public void onServiceDisconnected(ComponentName arg0) { mBound = false; } }; @Override protected void onStart() { super.onStart(); Intent intent = new Intent(this, RandomNumberService.class); bindService(intent, mConnection, Context.BIND_AUTO_CREATE); } @Override protected void onStop() { super.onStop(); if (mBound) { unbindService(mConnection); mBound = false; } } public void generateRandomNumber() { if (mBound) { int randomNumber = mService.getRandomNumber(); // 在此处使用生成的随机数 } } ``` 请注意,您需要在 AndroidManifest.xml 文件声明您的 Service,如下所示: ``` <service android:name=".RandomNumberService" /> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值