亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android服務(wù)應(yīng)用ClockService實(shí)現(xiàn)鬧鐘功能

 更新時(shí)間:2020年11月08日 15:57:20   作者:渣渣林  
這篇文章主要為大家詳細(xì)介紹了Android服務(wù)應(yīng)用ClockService實(shí)現(xiàn)鬧鐘功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

ClockService安卓服務(wù)應(yīng)用實(shí)現(xiàn)鬧鐘,供大家參考,具體內(nèi)容如下

創(chuàng)建ClockActivity,可輸入一個(gè)時(shí)間(使用Time文本框),再創(chuàng)建一個(gè)ClockService在用于計(jì)時(shí),到時(shí)間后,以在Activity中發(fā)出通知(在下方的TextView中顯示“時(shí)間到”)。

注意:這里涉及到了Service操作Activity

實(shí)驗(yàn)步驟:使用BoundService方式開啟服務(wù)

1、首先定義布局文件,這里不做過多贅述

3、 定義一個(gè)Service服務(wù)類,然后在類里面定義一個(gè)MyBinder的內(nèi)部類,用于獲取Service對(duì)象與Service對(duì)象狀態(tài)。在內(nèi)部類中必須要實(shí)現(xiàn)的方法onBind方法返回MyBinder服務(wù)對(duì)象。在內(nèi)部類中定義一個(gè)getHandler方法獲取Handler對(duì)象用于MainActivity和MyService之間的消息傳遞。

Handler消息傳遞關(guān)鍵代碼如下:

4、 創(chuàng)建MainActivity中的單擊事件

5、服務(wù)的綁定需要?jiǎng)?chuàng)建ServiceConnection對(duì)象并實(shí)現(xiàn)相應(yīng)的方法,然后在重寫的onServiceConnected方法中獲取后臺(tái)Service,代碼如下:

- Activity_main.xml代碼:

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

 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="110dp"
 android:layout_marginHorizontal="20dp"
 android:orientation="horizontal">

 <TextView
 android:layout_width="150dp"
 android:layout_height="80dp"
 android:layout_marginTop="15dp"
 android:background="@drawable/shape"
 android:gravity="center_horizontal"
 android:text="鬧鐘"
 android:textAlignment="center"
 android:textSize="50sp"></TextView>

 <EditText
 android:autofillHints="true"
 android:hint="10:10:10"
 android:id="@+id/num"
 android:layout_width="match_parent"
 android:layout_height="80dp"
 android:layout_marginLeft="15dp"
 android:layout_marginTop="5dp"
 android:background="@drawable/shape"
 android:gravity="center"
 android:inputType="time"
 android:textSize="35sp"></EditText>
 </LinearLayout>

 <Button
 android:id="@+id/btnStart"
 android:layout_width="match_parent"
 android:layout_height="80dp"
 android:layout_marginHorizontal="20dp"
 android:layout_marginTop="15dp"
 android:background="@drawable/shape"
 android:text="開始"
 android:textSize="50sp"></Button>

 <TextView
 android:id="@+id/text1"
 android:layout_width="match_parent"
 android:layout_height="300dp"
 android:layout_margin="20dp"
 android:background="@drawable/shape"
 android:gravity="center"
 android:text="倒計(jì)時(shí)"
 android:textSize="100sp"></TextView>
</LinearLayout>

- MyService.java代碼

package com.example.clock;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.EditText;
public class MyService extends Service {
 public MyService() {
 }
 @Override
 public IBinder onBind(Intent intent) {
 return new MyBinder(); //必須實(shí)現(xiàn)的方法,用于活動(dòng)與服務(wù)之間的綁定
 }
 public class MyBinder extends Binder {
 MyHandler handler;
 public MyService getMyService() {
 return MyService.this;
 }
 public MyHandler getHandler() {
 handler=new MyHandler();//初始化一個(gè)消息對(duì)象
 return handler; //返回該消息對(duì)象
 }
 }
 public class MyHandler extends Handler {
 public String[] nums;
 public String str;
 public String str1;
 public void handleMessage(Message msg) {
 str1= String.valueOf(msg.obj); //獲取MainActivity中傳遞的消息
 Log.d("渣", str1);
 new Thread(new Runnable() {
 @Override
 public void run() { //開啟一個(gè)線程
  nums=str1.split(":"); //將獲取到的字符串拆分成數(shù)組
  //將字符串中的時(shí)間轉(zhuǎn)換成秒
  int time1=Integer.parseInt(nums[2])+60*60*Integer.parseInt(nums[1])+60*Integer.parseInt(nums[1]);
  for(int time = time1;time>=0;time--){ //通過for循環(huán)對(duì)對(duì)時(shí)間進(jìn)行循環(huán)
  if(time==0){ //如果時(shí)間倒計(jì)時(shí)到0,則顯示(時(shí)間到)字樣
  MainActivity.textView.setText("時(shí)間到!");
  }
  try { //將time秒重新轉(zhuǎn)換成時(shí)間字符串
  int hour = 0;
  int minutes = 0;
  int sencond = 0;
  int temp = time % 3600;
  if (time > 3600) {
  hour = time / 3600;
  if (temp != 0) {
   if (temp > 60) {
   minutes = temp / 60;
   if (temp % 60 != 0) {
   sencond = temp % 60;
   }
   } else {
   sencond = temp;
   }
  }
  } else {
  minutes = time / 60;
  if (time % 60 != 0) {
   sencond = time % 60;
  }
  }
  str=(hour<10?("0"+hour):hour) + ":" + (minutes<10?("0"+minutes):minutes)
   + ":" + (sencond<10?("0"+sencond):sencond);
  MainActivity.num.setText(str); //及時(shí)更新EditText的值
  Thread.sleep(1000); //線程休眠1秒
  } catch (Exception e) {
  e.printStackTrace();
  }
  }
 }
 }).start();
 }
 }

 @Override
 public void onDestroy() {
 super.onDestroy();
 }
}

MainAcivity.java

package com.example.clock;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
 MyService.MyBinder myBinder;
 public static EditText num;
 int flag = 0;
 String str;
 Intent intent;
 public static TextView textView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 textView=findViewById(R.id.text1);
 final Button btnStart = (Button) findViewById(R.id.btnStart);
 num = (EditText) findViewById(R.id.num);
 btnStart.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 if (flag == 0) {
  if (num.getText().length() < 1) { //如果未輸入數(shù)值,則獲取默認(rèn)填充值(Hint)
  str = String.valueOf(num.getHint());
  }else {
  str=num.getText().toString(); //獲取輸入的值
  }
  flag = 1; //用于判斷按鈕狀態(tài)
  btnStart.setText("暫停");
  num.setEnabled(false); //將EditText設(shè)置為不可編輯
  intent = new Intent(MainActivity.this, MyService.class); //創(chuàng)建啟動(dòng)Service的Intent對(duì)象
  bindService(intent, conn, BIND_AUTO_CREATE); //綁定指定Service
  Log.d("time", String.valueOf(str));
 } else {
  flag = 0;
  btnStart.setText("開始");
  myBinder.getMyService().onDestroy();
 }
 }
 });
 }
 ServiceConnection conn = new ServiceConnection() {
 @Override
 public void onServiceConnected(ComponentName name, IBinder service) {//設(shè)置與服務(wù)進(jìn)行通信
 myBinder = (MyService.MyBinder) service; //獲取服務(wù)中的MyBinder對(duì)象
 Message message = new Message(); //創(chuàng)建消息對(duì)象
 message.obj = str; //傳遞參數(shù),str是獲取到的值
 MyService.MyHandler handler = myBinder.getHandler(); //獲取MyService中的Handler對(duì)象
 handler.sendMessage(message); //通過Handler對(duì)象發(fā)送消息
 }

 @Override
 public void onServiceDisconnected(ComponentName name) {

 }
 };
}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android UI開發(fā)中所遇到的各種坑

    Android UI開發(fā)中所遇到的各種坑

    今天小編就為大家分享一篇關(guān)于Android UI開發(fā)中所遇到的各種坑,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • Android選擇與上傳圖片之ImagePicker教程

    Android選擇與上傳圖片之ImagePicker教程

    這篇文章主要介紹了在Android中對(duì)于圖片的選擇與上傳方法,本文介紹了ImagePicker的相關(guān)使用教程,學(xué)習(xí)Android的同學(xué)進(jìn)來看看吧
    2021-08-08
  • Android中監(jiān)聽軟鍵盤輸入的使用方式

    Android中監(jiān)聽軟鍵盤輸入的使用方式

    今天我們來討論一下Android中監(jiān)聽軟鍵盤輸入的使用方式,它允許用戶輸入文本和執(zhí)行其他操作,但是,有時(shí)候我們需要在用戶輸入文本時(shí)進(jìn)行一些特殊的處理,比如實(shí)時(shí)驗(yàn)證輸入內(nèi)容、限制輸入字符的類型等,因此,了解如何監(jiān)聽軟鍵盤輸入是非常重要的
    2023-10-10
  • Android判斷程序是否第一次啟動(dòng)

    Android判斷程序是否第一次啟動(dòng)

    這篇文章主要為大家詳細(xì)介紹了Android判斷程序是否第一次啟動(dòng)的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Android仿淘寶商品詳情頁

    Android仿淘寶商品詳情頁

    這篇文章主要為大家詳細(xì)介紹了Android仿淘寶商品詳情頁,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • Andriod Studio實(shí)現(xiàn)保存QQ密碼功能(案例代碼詳解)

    Andriod Studio實(shí)現(xiàn)保存QQ密碼功能(案例代碼詳解)

    這篇文章主要介紹了Andriod Studio實(shí)現(xiàn)保存QQ密碼功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Android在自定義類中實(shí)現(xiàn)自定義監(jiān)聽器方式

    Android在自定義類中實(shí)現(xiàn)自定義監(jiān)聽器方式

    這篇文章主要介紹了Android在自定義類中實(shí)現(xiàn)自定義監(jiān)聽器方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Android組件WebView編寫有道詞典小案例分享

    Android組件WebView編寫有道詞典小案例分享

    這篇文章主要為大家分享了Android組件WebView編寫有道詞典小案例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-05-05
  • 玩轉(zhuǎn)Android之Drawable的使用

    玩轉(zhuǎn)Android之Drawable的使用

    這篇文章主要為大家詳細(xì)介紹了Android之Drawable的使用方法,幫助大家系統(tǒng)的學(xué)習(xí)一下Drawable的使用,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Android APP性能優(yōu)化分析

    Android APP性能優(yōu)化分析

    這篇文章主要給大家講了Android APP性能優(yōu)化相關(guān)的思考以及優(yōu)化重點(diǎn)分析,需要的朋友參考下吧。
    2017-12-12

最新評(píng)論