最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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

實驗步驟:使用BoundService方式開啟服務(wù)

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

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

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

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

5、服務(wù)的綁定需要創(chuàng)建ServiceConnection對象并實現(xiàn)相應(yīng)的方法,然后在重寫的onServiceConnected方法中獲取后臺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="倒計時"
 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(); //必須實現(xiàn)的方法,用于活動與服務(wù)之間的綁定
 }
 public class MyBinder extends Binder {
 MyHandler handler;
 public MyService getMyService() {
 return MyService.this;
 }
 public MyHandler getHandler() {
 handler=new MyHandler();//初始化一個消息對象
 return handler; //返回該消息對象
 }
 }
 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() { //開啟一個線程
  nums=str1.split(":"); //將獲取到的字符串拆分成數(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)對對時間進行循環(huán)
  if(time==0){ //如果時間倒計時到0,則顯示(時間到)字樣
  MainActivity.textView.setText("時間到!");
  }
  try { //將time秒重新轉(zhuǎn)換成時間字符串
  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); //及時更新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)建啟動Service的Intent對象
  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ù)進行通信
 myBinder = (MyService.MyBinder) service; //獲取服務(wù)中的MyBinder對象
 Message message = new Message(); //創(chuàng)建消息對象
 message.obj = str; //傳遞參數(shù),str是獲取到的值
 MyService.MyHandler handler = myBinder.getHandler(); //獲取MyService中的Handler對象
 handler.sendMessage(message); //通過Handler對象發(fā)送消息
 }

 @Override
 public void onServiceDisconnected(ComponentName name) {

 }
 };
}

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

相關(guān)文章

  • Flutter如何輕松實現(xiàn)動態(tài)更新ListView淺析

    Flutter如何輕松實現(xiàn)動態(tài)更新ListView淺析

    在Android中通常都會用到listview.那么flutter里面怎么用呢?下面這篇文章主要給大家介紹了關(guān)于Flutter如何輕松實現(xiàn)動態(tài)更新ListView的相關(guān)資料,需要的朋友可以參考下
    2022-02-02
  • Android使用Kotlin API實踐WorkManager

    Android使用Kotlin API實踐WorkManager

    這篇文章主要介紹了Android使用Kotlin API實踐WorkManager的步驟,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
    2021-04-04
  • android的編譯和運行過程深入分析

    android的編譯和運行過程深入分析

    首先來看一下使用Java語言編寫的Android應(yīng)用程序從源碼到安裝包的整個過程,此過程對了解android的編譯和運行過程有很大的幫助
    2012-12-12
  • Flutter中g(shù)o_router路由管理的使用指南

    Flutter中g(shù)o_router路由管理的使用指南

    go_router?是一個?Flutter?的第三方路由插件,相比?Flutter?自帶的路由,go_router?更加靈活,而且簡單易用,下面小編就來和大家聊聊go_router的使用吧
    2023-08-08
  • 在啟動欄制作android studio啟動圖標(biāo)

    在啟動欄制作android studio啟動圖標(biāo)

    這篇文章主要介紹了在啟動欄制作android studio啟動圖標(biāo)的相關(guān)知識,需要的朋友可以參考下
    2018-03-03
  • Android藍(lán)牙服務(wù)啟動流程分析探索

    Android藍(lán)牙服務(wù)啟動流程分析探索

    這篇文章主要介紹了Android藍(lán)牙服務(wù)啟動流程,了解內(nèi)部原理是為了幫助我們做擴展,同時也是驗證了一個人的學(xué)習(xí)能力,如果你想讓自己的職業(yè)道路更上一層樓,這些底層的東西你是必須要會的
    2023-01-01
  • Android開發(fā)自學(xué)筆記(一):Hello,world!

    Android開發(fā)自學(xué)筆記(一):Hello,world!

    這篇文章主要介紹了Android開發(fā)自學(xué)筆記(一):Hello,world!本文講解了創(chuàng)建HelloWorld工程、編寫代碼、啟動模擬器等步驟,需要的朋友可以參考下
    2015-04-04
  • android  點擊EditText始終不彈出軟件鍵盤實現(xiàn)代碼

    android 點擊EditText始終不彈出軟件鍵盤實現(xiàn)代碼

    這篇文章主要介紹了android 點擊EditText始終不彈出軟件鍵盤實現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下
    2016-11-11
  • Android 5.0以上Toast不顯示的解決方法

    Android 5.0以上Toast不顯示的解決方法

    最近在開發(fā)中我們經(jīng)常會在適配5.0以后的機型遇到各種各樣的問題,其中有一個不大不小的問題就是:Toast不顯示問題,這篇文章就給大家總結(jié)了Android 5.0以上Toast不顯示的原因與解決方法,有需要的朋友們可以參考借鑒,下面來一起看看吧。
    2016-11-11
  • Flutter實現(xiàn)可循環(huán)輪播圖效果

    Flutter實現(xiàn)可循環(huán)輪播圖效果

    這篇文章主要介紹了Flutter實現(xiàn)可循環(huán)輪播圖效果,本文圖文并茂通過實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2019-07-07

最新評論

景谷| 泽州县| 安阳县| 册亨县| 镇赉县| 天全县| 广平县| 汝南县| 贺州市| 承德县| 青冈县| 农安县| 徐州市| 玉山县| 溧水县| 封丘县| 拜泉县| 吉林省| 普格县| 宁明县| 忻州市| 开鲁县| 尼木县| 荆门市| 古交市| 灵武市| 绥宁县| 灵石县| 建水县| 微山县| 满洲里市| 克什克腾旗| 通州市| 昂仁县| 建瓯市| 芒康县| 安图县| 始兴县| 贵德县| 兴和县| 高淳县|