定時靜音助手


定時靜音助手

背景

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

界面設計

<?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="添加靜音開始時間" />
    <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="添加靜音停止時間" />
       <TextView
        android:id="@+id/tvAlarmRecord2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="16dp" /
</LinearLayout>

點擊完按鈕的會出現一個時間點設置的對話框 代碼如下

<?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>

效果圖

這里寫圖片描述

功能設計

原理介紹

先簡單介紹一下工作原理。在添加時間點之后,需要將所添加的時間點保存在文件或者數據庫中,我使用了SharedPrefences來保存時間點,key和value都是時間點,然后用到AlarmManager每隔一分鍾掃描一次,在掃描過程中從文件獲取當前時間(時:分)的value,如果成功獲得value就說明當前時間為時間點,此時調用audioManager ,當掃描掉button1設置的文件信息,就調用AudioManager.RINGER_MODE_SILENT,如果掃描到button2設置的文件信息,就調用AudioManager.RINGER_MODE_NORMAL,時期出去正常模式。
此程序包含兩個java文件,分別是MainActivity.java和TimeReceiver.java,TimeReceiver主要是判斷是否到達時間點,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("設置靜音開始時間").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("設置靜音結束時間").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);
		}
		
	}

	
	
}

程序運行效果

初始狀態

這里寫圖片描述

開始靜音狀態

這里寫圖片描述

恢復正常狀態

這里寫圖片描述

源碼地址

github


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM