Android基于BroadcastReceiver和Service、SoundPool开发的防过充助手app
来源:互联网 发布:超次元矩阵 编辑:程序博客网 时间:2024/05/22 05:08
前段时间换了一个小米4C手机,可是发现它的充电充满没有提醒,上一个手机换了就是因为不爱惜电池,让它过充的次数多了,虽然听别人说小米4c手机充电器是智能充电器,有保护作用,但我自己还是不放心,于是就亲手写了一个防过充小应用,已经在使用,可以达到想要的效果。
下面就来说一下该应用的大致思路,主要通过广播类BroadcastReceiver来实时监控手机的电量变化,当手机电量充电到达100%时,就会通过广播类来启动服务类Service.服务类启动在它内部的SoundPool类播放声音,提示用户拔去充电器。看到博主的想法后,你们可能会有各种想法,会认为可以只用广播类或者服务类啊,播放音乐可以用Mediaplayer类呀,当然这些都是可以去尝试的,博主在这里只是分享自己的想法,个人爱好不同。
下面就是开始贴代码了
MyBroadcastReceiver类:
package com.example.batteryhelper;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentSender.SendIntentException;import android.graphics.Color;import android.os.Bundle;import android.widget.TextView;import android.widget.Toast;public class MyBroadcastReceiver extends BroadcastReceiver { /** * 广播负责循环监听,实时做出响应 */@Overridepublic void onReceive(Context context, Intent intent) {if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {Bundle bundle = intent.getExtras(); // 获得通知里面的信息// 获取当前电量int current = bundle.getInt("level");// 获取总电量int total = bundle.getInt("scale");if ((current * 100 / total) >= 100) { //当电量不小于100%时,即满// 广播里启动服务Intent service = new Intent(context, MyService.class); context.startService(service);//确保充满时只有停止按钮可以使用MainActivity.btnMyServiceStart.setEnabled(false);MainActivity.btnMyServiceStop.setEnabled(true);MainActivity.txtMyTextView.setText("当前状态:已启动");MainActivity.txtMyTextView.setTextColor(Color.RED);}}}}
MyService类:
package com.example.batteryhelper;import java.util.HashMap;import android.app.Service;import android.content.Intent;import android.media.AudioManager;import android.media.SoundPool;import android.media.SoundPool.OnLoadCompleteListener;import android.os.IBinder;import android.text.InputFilter.LengthFilter;import android.util.Log;import android.widget.TextView;import android.widget.Toast;/** * MyService类负责启动播放器播放背景音乐 * @author Administrator * */public class MyService extends Service {private SoundPool mySound; //定义一个音乐池,用来播放提示用户的声音private HashMap<Integer, Integer> mySoundMap; //定义HashMap用来存放音乐资源的映射@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate(); /** * onCreate()周期完成对音乐池mySound,哈希集合mySoundMap的初始化工作 */mySound = new SoundPool(10, AudioManager.STREAM_SYSTEM, 5);mySoundMap = new HashMap<Integer, Integer>();mySoundMap.put(1, mySound.load(this, R.raw.fulled, 1));}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {//由于OnCreate到onStartCommand周期间隔时间很短,//实现OnLoadCompleteListener 确保加载完成来播放,值得注意mySound.setOnLoadCompleteListener(new OnLoadCompleteListener() {public void onLoadComplete(SoundPool soundPool, int sampleId,int status) {// TODO Auto-generated method stubsoundPool.play(mySoundMap.get(1), // 声音资源id1, // 左声道音量1, // 右声道音量0, // 优先级-1, // 循环次数 -1带表永远循环1);}});return super.onStartCommand(intent, flags, startId);}@Overridepublic IBinder onBind(Intent arg0) {return null;}@Overridepublic void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy(); //在onDestroy周期里释放mySound的加载的资源mySound.release();}}
MainActivity类:
package com.example.batteryhelper;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.content.IntentFilter;import android.graphics.Color;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity {//定义静态共有的Button控件btnMyServiceStart,用于启动广播//也便于其他类对其状态的修改public static Button btnMyServiceStart; //定义静态共有的Button控件btnMyServiceStop,用于停止广播public static Button btnMyServiceStop;//定义静态共有的TextView控件txtMyTextView,用于显示当前应用启用的状态public static TextView txtMyTextView;//定义广播类mMyBroadcastReceiverprivate MyBroadcastReceiver mMyBroadcastReceiver;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获取页面布局里的控件btnMyServiceStart = (Button) findViewById(R.id.btnStartMyService);btnMyServiceStop = (Button) findViewById(R.id.btnStopMyService);txtMyTextView = (TextView) findViewById(R.id.txtMyText);btnMyServiceStart.setOnClickListener(new OnClickListener() { /** * "启动"按钮对应的事件 */@Overridepublic void onClick(View v) {// TODO Auto-generated method stub//实例化过滤器itFilter,过滤电池电量改变BATTERY_CHANGED事件,//对其进行监听IntentFilter itFilter = new IntentFilter("android.intent.action.BATTERY_CHANGED");//实例化广播mMyBroadcastReceiver = new MyBroadcastReceiver();//动态注册广播registerReceiver(mMyBroadcastReceiver, itFilter); //将启用按钮禁用btnMyServiceStart.setEnabled(false);//将停止按钮启用btnMyServiceStop.setEnabled(true); //设置文本控件的内容txtMyTextView.setText("当前状态:已启动");txtMyTextView.setTextColor(Color.RED);}});btnMyServiceStop.setOnClickListener(new OnClickListener() { /** * "停止"按钮对应的事件 */@Overridepublic void onClick(View v) {// TODO Auto-generated method stubunregisterReceiver(mMyBroadcastReceiver); //注销广播Intent it = new Intent(MainActivity.this, MyService.class);stopService(it); //关闭服务btnMyServiceStart.setEnabled(true);btnMyServiceStop.setEnabled(false);txtMyTextView.setText("当前状态:已关闭");txtMyTextView.setTextColor(Color.GRAY);}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
主页面布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/btnStartMyService" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="启动充满提醒" /> <Button android:id="@+id/btnStopMyService" android:layout_width="fill_parent" android:layout_height="wrap_content" android:enabled="false" android:text="停止充满提醒" /> <TextView android:id="@+id/txtMyText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="说明:\n 1.本应用是当手机电量充满时会发出响声提示用户\n 2.使用时请将手机调为非静音状态,关掉手机自动清理内存功能,并保证本应用处于运行状态\n 3.本应用目前为试用版,不作商业用途\n 4.若有任何疑问请联系467406162@qq.com " android:textColor="#808080" /> </RelativeLayout></LinearLayout>主页面布局效果图:
AndroidManifest.xml文件:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.batteryhelper" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="14" /> <application android:allowBackup="true" android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.batteryhelper.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".MyBroadcastReceiver" > <intent-filter > <action android:name="android.intent.action.BATTERY_CHANGED"/> </intent-filter> </receiver> <service android:name=".MyService" /> </application> <uses-permission android:name="android.permission.VIBRATE" /></manifest>以上就是与该应用有关的。
0 0
- Android基于BroadcastReceiver和Service、SoundPool开发的防过充助手app
- android--service和broadcastReceiver基础
- 浅谈android Service和BroadCastReceiver
- 安卓开发的Service、BroadcastReceiver和Webview
- 开发APP的那些组件---->>>>------Android中BroadcastReceiver的应用
- Android入门之Service和BroadcastReceiver
- Android笔记----Service应用和BroadcastReceiver组件
- Android面试(三)BroadCastReceiver和Service
- 基于Service的播放器,Activity,Service,BroadcastReceiver综合使用
- Android中SoundPool和MediaPlayer的区别
- 关于BroadcastReceiver 和 Service的那些事
- 开源基于百度地图SDK的Android交通助手App
- Android Service BroadcastReceiver
- Android之Service&BroadCastReceiver
- Android之Service&BroadCastReceiver
- Android (Service、BroadcastReceiver)
- Android~Service+BroadcastReceiver使用
- Android的Service与BroadcastReceiver之四 BroadcastReceiver 简介、开发与配置;发送广播、有序广播;使用BroadcastReceiver接受系统广播
- ThinkPHP3.2.3自定义函数库
- 乔布斯的创新
- ALAssetsLibrary读取所有照片
- get_magic_quotes_gpc() 对$_GET $_COOKIE $_POST转义处理
- 宏元编程——关系计算——not_equal
- Android基于BroadcastReceiver和Service、SoundPool开发的防过充助手app
- android 测试
- c++第二次实验1-2
- UIBezierPath+画板,签名档
- maven中使用spring提示applicationContext.xml找不到
- UVA10779 Collectors Problem(最大流)
- VS平台下 OpenGL glReadPixels、glDrawPixels、glCopyPixels函数
- 简单oracle存储过程测试
- 简单国际化操作(本地化)