Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
只猪会上树
耳机防沉迷(记录佩戴耳机时长)
提交
1f1abefa
耳
耳机防沉迷(记录佩戴耳机时长)
项目概览
只猪会上树
/
耳机防沉迷(记录佩戴耳机时长)
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
代码片段
项目成员
Pages
耳
耳机防沉迷(记录佩戴耳机时长)
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
代码片段
代码片段
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
1f1abefa
编写于
7月 13, 2021
作者:
只猪会上树
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add new file
上级
632c1ee7
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
196 addition
and
0 deletion
+196
-0
app/src/main/java/com/example/myfirstapp/RecordTimeService.java
...c/main/java/com/example/myfirstapp/RecordTimeService.java
+196
-0
未找到文件。
app/src/main/java/com/example/myfirstapp/RecordTimeService.java
0 → 100644
浏览文件 @
1f1abefa
package
example.com.save
;
import
android.app.Activity
;
import
android.app.AlarmManager
;
import
android.app.Notification
;
import
android.app.PendingIntent
;
import
android.app.Service
;
import
android.bluetooth.BluetoothHeadset
;
import
android.content.Context
;
import
android.content.Intent
;
import
android.content.IntentFilter
;
import
android.media.AudioDeviceInfo
;
import
android.media.AudioManager
;
import
android.media.MediaPlayer
;
import
android.os.Build
;
import
android.os.IBinder
;
import
android.os.SystemClock
;
import
android.util.Log
;
import
android.widget.Toast
;
import
android.support.annotation.RequiresApi
;
public
class
RecordTimeService
extends
Service
{
// 时钟广播接收器
private
static
TIME_TICK_Receiver
time_tick_receiver
;
// 耳机广播接收器
private
HeadsetReceiver
HeadsetReceiver
;
// 长时间计时任务AlarmManager
private
AlarmManager
manager
;
private
PendingIntent
pi
;
// 音频控制
private
AudioManager
audioManager
;
private
MediaPlayer
mediaPlayer
;
public
RecordTimeService
()
{
}
@Override
public
IBinder
onBind
(
Intent
intent
)
{
return
null
;
}
@RequiresApi
(
api
=
Build
.
VERSION_CODES
.
JELLY_BEAN
)
public
void
onCreate
(){
super
.
onCreate
();
register_time_receiver
();
registerHeadsetReceiver
(
this
);
Intent
intent
=
new
Intent
(
this
,
MainActivity
.
class
);
intent
.
setFlags
(
Intent
.
FLAG_ACTIVITY_NEW_TASK
);
PendingIntent
pd
=
PendingIntent
.
getActivity
(
this
,
0
,
intent
,
PendingIntent
.
FLAG_UPDATE_CURRENT
);
Notification
notify
=
new
Notification
.
Builder
(
this
)
.
setOngoing
(
true
)
.
setPriority
(
Notification
.
PRIORITY_MAX
)
.
setContentIntent
(
pd
)
.
setContentTitle
(
"监测中"
)
.
setContentText
(
"亲,注意佩戴耳机时长呦"
)
//.setSmallIcon(R.drawable.headplug)
.
build
();
startForeground
(
1
,
notify
);
}
@RequiresApi
(
api
=
Build
.
VERSION_CODES
.
KITKAT
)
public
int
onStartCommand
(
Intent
intent
,
int
flags
,
int
startId
){
//Log.i("service", "启动");
manager
=
(
AlarmManager
)
getSystemService
(
ALARM_SERVICE
);
int
music_time
=
7
*
1000
;
long
triggerAtTime
=
SystemClock
.
elapsedRealtime
()
+
music_time
;
Intent
i
=
new
Intent
(
this
,
AlarmReceiver
.
class
);
pi
=
PendingIntent
.
getBroadcast
(
this
,
0
,
i
,
0
);
manager
.
setExact
(
AlarmManager
.
ELAPSED_REALTIME_WAKEUP
,
triggerAtTime
,
pi
);
//aaa.txt中为1则耳机连接,为0则耳机断开
if
(
FileProcess
.
readInternalData
(
getApplicationContext
(),
"aaa.txt"
)
==
1
)
{
//这里开辟一条线程,用来执行具体的逻辑操作:
new
Thread
(
new
Runnable
()
{
@Override
public
void
run
()
{
//Log.i("Service", "线程");
int
time2
=(
int
)(
SystemClock
.
elapsedRealtime
()/
1000
)
-
FileProcess
.
readInternalData
(
getApplicationContext
(),
"lasttime.txt"
);
// 持续佩戴耳机时间
FileProcess
.
writeInternalData
(
getApplicationContext
(),
"time2.txt"
,
time2
);
//总共时间
int
newTime
=
FileProcess
.
readInternalData
(
getApplicationContext
(),
"time.txt"
)
+
FileProcess
.
readInternalData
(
getApplicationContext
(),
"time2.txt"
);
//判断时长是否超过上限
if
(
newTime
>=
FileProcess
.
readInternalData
(
getApplicationContext
(),
"limit.txt"
)
)
{
mediaPlayer
=
MediaPlayer
.
create
(
getApplicationContext
(),
R
.
raw
.
red
);
//语音提示
try
{
if
(
mediaPlayer
.
getCurrentPosition
()
>
0
)
mediaPlayer
.
seekTo
(
0
);
mediaPlayer
.
start
();
}
catch
(
IllegalStateException
e
)
{
e
.
printStackTrace
();
}
}
}
}).
start
();
}
return
super
.
onStartCommand
(
intent
,
flags
,
startId
);
//系统kill服务自动重启
// return Service.START_STICKY;
}
/**
* 注册时钟广播接收器
*/
private
void
register_time_receiver
(){
time_tick_receiver
=
new
TIME_TICK_Receiver
();
IntentFilter
filter
=
new
IntentFilter
();
filter
.
addAction
(
Intent
.
ACTION_TIME_TICK
);
registerReceiver
(
time_tick_receiver
,
filter
);
}
/**
* 注册耳机断开与连接广播接收器
* 判定耳机设备连接状态
*/
public
void
registerHeadsetReceiver
(
Service
service
)
{
HeadsetReceiver
=
new
HeadsetReceiver
();
IntentFilter
intentFilter
=
new
IntentFilter
();
intentFilter
.
addAction
(
Intent
.
ACTION_HEADSET_PLUG
);
IntentFilter
intentFilter1
=
new
IntentFilter
();
intentFilter1
.
addAction
(
BluetoothHeadset
.
ACTION_CONNECTION_STATE_CHANGED
);
service
.
registerReceiver
(
HeadsetReceiver
,
intentFilter
);
service
.
registerReceiver
(
HeadsetReceiver
,
intentFilter1
);
HeadsetReceiver
.
setOnHeadsetListener
(
new
OnHeadsetListener
()
{
@Override
public
void
isHeadsetConnected
(
boolean
connected
)
{
if
(
connected
)
{
//Log.i("receiver","耳机已连接");
FileProcess
.
writeInternalData
(
getApplicationContext
(),
"aaa.txt"
,
1
);
int
lasttime
=(
int
)(
SystemClock
.
elapsedRealtime
()/
1000
);
FileProcess
.
writeInternalData
(
getApplicationContext
(),
"lasttime.txt"
,
lasttime
);
}
else
{
//Log.i("receiver","耳机断开");
FileProcess
.
writeInternalData
(
getApplicationContext
(),
"aaa.txt"
,
0
);
int
newTime
=
FileProcess
.
readInternalData
(
getApplicationContext
(),
"time.txt"
)
+
FileProcess
.
readInternalData
(
getApplicationContext
(),
"time2.txt"
);
FileProcess
.
writeInternalData
(
getApplicationContext
(),
"time.txt"
,
newTime
);
FileProcess
.
writeInternalData
(
getApplicationContext
(),
"time2.txt"
,
0
);
}
}
});
}
// public void registerNoisyreceiver(Context context){
// noisyReceiver=new NoisyReceiver();
// IntentFilter intentFilter=new IntentFilter();
// intentFilter.addAction(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
// context.registerReceiver(noisyReceiver,intentFilter);
// noisyReceiver.SpeakerChangeListener(new NoisyReceiver() {
// @Override
// public void isSpeaker(boolean speaker) {
// if(speaker){
// mediaPlayer.release();
// }
// }
// });
//
// }
public
void
onDestroy
(){
super
.
onDestroy
();
//Toast.makeText(getApplicationContext(),"onDestroy",Toast.LENGTH_SHORT).show();
//Log.i("service","onDestroy");
}
//判断设备连接的另一种方法
private
boolean
checkIsWired
()
{
audioManager
=
(
AudioManager
)
getSystemService
(
Context
.
AUDIO_SERVICE
);
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
M
)
{
AudioDeviceInfo
[]
devices
=
audioManager
.
getDevices
(
AudioManager
.
GET_DEVICES_OUTPUTS
);
for
(
AudioDeviceInfo
device
:
devices
)
{
int
deviceType
=
device
.
getType
();
if
(
deviceType
==
AudioDeviceInfo
.
TYPE_WIRED_HEADSET
||
deviceType
==
AudioDeviceInfo
.
TYPE_WIRED_HEADPHONES
||
deviceType
==
AudioDeviceInfo
.
TYPE_BLUETOOTH_A2DP
||
deviceType
==
AudioDeviceInfo
.
TYPE_BLUETOOTH_SCO
)
{
return
true
;
}
}
}
else
{
return
audioManager
.
isWiredHeadsetOn
()
||
audioManager
.
isBluetoothScoOn
()
||
audioManager
.
isBluetoothA2dpOn
();
}
return
false
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录