UI高级_Service获取随机值、获取本地时间

MainActivity

package com.example.demo01_service;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.demo01_service.service.Service;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Service.Mybind bind;
    ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            bind = (Service.Mybind) iBinder;
          
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //跳转
        Intent intent = new Intent(MainActivity.this,Service.class);
        //开启service
        //注册service
        bindService(intent,connection,Service.BIND_AUTO_CREATE);
        //获取控件
        Button button = findViewById(R.id.butt_dj);
        textView = findViewById(R.id.textview);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.butt_dj:
                //获取本地时间
                String date = bind.getDate();
                textView.setText(""+date);
                //吐丝随机数
                Toast.makeText(MainActivity.this,""+bind.getRandom(),Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

Service

package com.example.demo01_service.service;

import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @Auther: 努力
 * @Date: 2019/1/2 10:43:${卢文杰}
 * @Description:
 */
public class Service extends android.app.Service {
    //内部类
    public class Mybind extends Binder{
        //随机值
        public String getRandom() {
            Random random = new Random();
            return random.nextInt(9) + "";
        }

        // 获取本地时间
        public String getDate() {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                    "yyyy年MM月dd日  HH:mm:ss");
            Date date = new Date(System.currentTimeMillis());
            String timeDate = simpleDateFormat.format(date);
            return timeDate;
        }

    }
    //实例化
    private Mybind mybind = new Mybind();

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mybind;
    }
}

切记一定要在清单文件中注册

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.demo01_service">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".service.Service"/>
    </application>

</manifest>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值