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

Android實(shí)現(xiàn)定時(shí)自動(dòng)靜音小助手

 更新時(shí)間:2017年06月21日 10:10:31   作者:liu_roy  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)定時(shí)自動(dòng)靜音小助手,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

定時(shí)靜音助手的實(shí)現(xiàn)方法,供大家參考,具體內(nèi)容如下

背景

突發(fā)奇想,剛好這學(xué)期剛上安卓課程,想設(shè)計(jì)一個(gè)時(shí)間助手。工作、學(xué)習(xí)中經(jīng)常會(huì)被突如其來的電話所打擾,在上班,上課時(shí)這突如其來的鈴聲會(huì)惹來別人的反感,而只靠人們的記性是很難在準(zhǔn)確的時(shí)間記得靜音。如果一直靜音,那么在休息時(shí)間又有可能漏接重要的電話。基于這種考慮,設(shè)計(jì)了這樣一自動(dòng)靜音小助手,來幫助人們在忙碌的生活中定時(shí)靜音,定時(shí)開啟正常模式,簡單方便。

界面設(shè)計(jì)

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

 <Button
  android:id="@+id/btnAddAlarm1"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="添加靜音開始時(shí)間" />
 <TextView
  android:id="@+id/tvAlarmRecord1"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:textSize="16dp" />
 
  <Button
   android:id="@+id/btnAddAlarm2"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="添加靜音停止時(shí)間" />
  <TextView
  android:id="@+id/tvAlarmRecord2"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:textSize="16dp" /
</LinearLayout>

點(diǎn)擊完按鈕的會(huì)出現(xiàn)一個(gè)時(shí)間點(diǎn)設(shè)置的對話框 代碼如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <TimePicker android:id="@+id/timepicker1"
  android:layout_width="fill_parent" android:layout_height="wrap_content" />
 
</LinearLayout>

效果圖

功能設(shè)計(jì)

原理介紹

先簡單介紹一下工作原理。在添加時(shí)間點(diǎn)之后,需要將所添加的時(shí)間點(diǎn)保存在文件或者數(shù)據(jù)庫中,我使用了SharedPrefences來保存時(shí)間點(diǎn),key和value都是時(shí)間點(diǎn),然后用到AlarmManager每隔一分鐘掃描一次,在掃描過程中從文件獲取當(dāng)前時(shí)間(時(shí):分)的value,如果成功獲得value就說明當(dāng)前時(shí)間為時(shí)間點(diǎn),此時(shí)調(diào)用audioManager ,當(dāng)掃描掉button1設(shè)置的文件信息,就調(diào)用AudioManager.RINGER_MODE_SILENT,如果掃描到button2設(shè)置的文件信息,就調(diào)用AudioManager.RINGER_MODE_NORMAL,時(shí)期出去正常模式。

此程序包含兩個(gè)java文件,分別是MainActivity.java和TimeReceiver.java,TimeReceiver主要是判斷是否到達(dá)時(shí)間點(diǎn),MainActivity 主要是整體的框架和邏輯。

MainActivity代碼如下:

package com.example.timesilent;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;

public class MainActivity extends Activity implements OnClickListener 
{

 
 private SharedPreferences sharedPreferences1;
 private SharedPreferences sharedPreferences2;
 private TextView tvAlarmRecord1;
 private TextView tvAlarmRecord2;
 @Override
 public void onClick(View v) {
  View view =getLayoutInflater().inflate(R.layout.time,null);
  final TimePicker timePicker1=(TimePicker)view.findViewById(R.id.timepicker1);
  
  timePicker1.setIs24HourView(true);
  
  switch(v.getId())
  {
    case R.id.btnAddAlarm1:
    {
     new AlertDialog.Builder(this).setTitle("設(shè)置靜音開始時(shí)間").setView(view).setPositiveButton("確定",new DialogInterface.OnClickListener(){
     

     @Override
    public void onClick(DialogInterface dialog,int which)
      {
       String timeStr=String.valueOf(timePicker1.getCurrentHour())+":"+String.valueOf(timePicker1.getCurrentMinute());
      
      tvAlarmRecord1.setText(tvAlarmRecord1.getText().toString()+"\n"+timeStr);
       sharedPreferences1.edit().putString(timeStr,timeStr).commit();
      }
     }).setNegativeButton("取消",null).show();
     break;
    }
    case R.id.btnAddAlarm2:
    {
     new AlertDialog.Builder(this).setTitle("設(shè)置靜音結(jié)束時(shí)間").setView(view).setPositiveButton("確定",new DialogInterface.OnClickListener(){
      @Override
      public void onClick(DialogInterface dialog,int which)
      {
       String timeStr=String.valueOf(timePicker1.getCurrentHour())+":"+String.valueOf(timePicker1.getCurrentMinute());
      
      tvAlarmRecord2.setText(tvAlarmRecord2.getText().toString()+"\n"+timeStr);
       sharedPreferences2.edit().putString(timeStr,timeStr).commit();
      }
     }).setNegativeButton("取消",null).show();
     break;
    }
  }
 
  
 }
 @Override
 public void onCreate(Bundle savedInstanceState)
  {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   Button btnAddAlarm1 = (Button) findViewById(R.id.btnAddAlarm1);
   Button btnAddAlarm2 = (Button) findViewById(R.id.btnAddAlarm2);
   tvAlarmRecord1 = (TextView) findViewById(R.id.tvAlarmRecord1);
   tvAlarmRecord2 = (TextView) findViewById(R.id.tvAlarmRecord2);
   btnAddAlarm1.setOnClickListener(this);
   btnAddAlarm2.setOnClickListener(this);
   
   sharedPreferences1 = getSharedPreferences("alarm_record1",
     Activity.MODE_PRIVATE);
   sharedPreferences2 = getSharedPreferences("alarm_record2",
     Activity.MODE_PRIVATE);

   AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
   Intent intent = new Intent(this, TimeReceiver.class);
   PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
     intent, 0);
   alarmManager.setRepeating(AlarmManager.RTC, 0, 60 * 1000, pendingIntent);

  }
 
}

TimeReceiver的代碼如下:

package com.example.timesilent;

import java.util.Calendar;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.content.SharedPreferences;


public class TimeReceiver extends BroadcastReceiver
{

 @Override
 public void onReceive(Context context, Intent intent)
 {
  
  SharedPreferences sharedPreferences1 = context.getSharedPreferences(
    "alarm_record1", Activity.MODE_PRIVATE);
  SharedPreferences sharedPreferences2 = context.getSharedPreferences(
    "alarm_record2", Activity.MODE_PRIVATE);
  String hour = String.valueOf(Calendar.getInstance().get(
    Calendar.HOUR_OF_DAY));
  String minute = String.valueOf(Calendar.getInstance().get(
    Calendar.MINUTE));
  String time1 = sharedPreferences1.getString(hour + ":" + minute, null);
  String time2 = sharedPreferences2.getString(hour + ":" + minute, null);
  AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
  if (time1!= null)
  { 
   audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
  }
  if (time2!= null)
  { 
   
   audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
  }
  
 }

 
 
}

程序運(yùn)行效果

初始狀態(tài)

開始靜音狀態(tài)

恢復(fù)正常狀態(tài)

源碼地址

github

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

相關(guān)文章

  • Android通用索引欄實(shí)現(xiàn)代碼

    Android通用索引欄實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了Android通用索引欄實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Android自定義控件實(shí)現(xiàn)溫度旋轉(zhuǎn)按鈕效果

    Android自定義控件實(shí)現(xiàn)溫度旋轉(zhuǎn)按鈕效果

    這篇文章主要給大家介紹了關(guān)于Android如何通過自定義控件實(shí)現(xiàn)溫度旋轉(zhuǎn)按鈕的效果,文中通過思路與方法一步步介紹的很詳細(xì),相信對大家的理解和學(xué)習(xí)具有一定的參考借鑒價(jià)值,有需要的朋友們下面來一起看看吧。
    2016-12-12
  • Android性能優(yōu)化全局異常處理詳情

    Android性能優(yōu)化全局異常處理詳情

    這篇文章主要介紹了Android性能優(yōu)化全局異常處理詳情,文章圍繞主題展開詳細(xì)的內(nèi)容協(xié)商,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下
    2022-08-08
  • Android仿網(wǎng)易新聞圖片詳情下滑隱藏效果示例代碼

    Android仿網(wǎng)易新聞圖片詳情下滑隱藏效果示例代碼

    這篇文章主要給大家介紹了關(guān)于利用Android如何仿網(wǎng)易新聞圖片詳情下滑隱藏效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-07-07
  • Android?Studio調(diào)試Gradle插件詳情

    Android?Studio調(diào)試Gradle插件詳情

    這篇文章主要介紹了Android?Studio調(diào)試Gradle插件詳情,文章圍繞主題展開詳細(xì)的內(nèi)容戒殺,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • 安卓(android)怎么實(shí)現(xiàn)下拉刷新

    安卓(android)怎么實(shí)現(xiàn)下拉刷新

    這里我們將采取的方案是使用組合View的方式,先自定義一個(gè)布局繼承自LinearLayout,然后在這個(gè)布局中加入下拉頭和ListView這兩個(gè)子元素,并讓這兩個(gè)子元素縱向排列。對安卓(android)怎么實(shí)現(xiàn)下拉刷新的相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧
    2016-04-04
  • Android使用FontMetrics對象計(jì)算位置坐標(biāo)

    Android使用FontMetrics對象計(jì)算位置坐標(biāo)

    這篇文章主要為大家詳細(xì)介紹了Android使用FontMetrics對象計(jì)算位置坐標(biāo),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • Android開發(fā)之圖片壓縮實(shí)現(xiàn)方法分析

    Android開發(fā)之圖片壓縮實(shí)現(xiàn)方法分析

    這篇文章主要介紹了Android開發(fā)之圖片壓縮實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android圖片壓縮的原理、實(shí)現(xiàn)方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2019-03-03
  • Android入門之Fragment嵌套Fragment的用法詳解

    Android入門之Fragment嵌套Fragment的用法詳解

    這篇文章主要為大家詳細(xì)介紹了Android中如何實(shí)現(xiàn)Fragment嵌套Fragment的相關(guān)資料,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下
    2023-02-02
  • Android中實(shí)現(xiàn)滑動(dòng)的七種方式總結(jié)

    Android中實(shí)現(xiàn)滑動(dòng)的七種方式總結(jié)

    這篇文章主要介紹了Android中實(shí)現(xiàn)滑動(dòng)的七種方式總結(jié),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-02-02

最新評論

寻甸| 桐乡市| 南投市| 普宁市| 三原县| 灵丘县| 林芝县| 安龙县| 古交市| 伊宁县| 陈巴尔虎旗| 志丹县| 铁力市| 那曲县| 淄博市| 呼和浩特市| 将乐县| 余庆县| 灵山县| 额济纳旗| 曲松县| 涡阳县| 台东市| 五河县| 广饶县| 揭东县| 海门市| 青冈县| 庆元县| 隆子县| 四川省| 东源县| 镇平县| 确山县| 麻阳| 建始县| 抚顺市| 通渭县| 措勤县| 东山县| 延津县|