irpas技术客

Android 8.1 第三方apk通过数据库调用系统定时开关机功能_EpiphanyCode_android 实现定时开关机

irpas 1536

由于客户需要我们提供API给到他们实现定时开关机功能

做的是展锐的方案,简单看下原厂如何实现该功能

1、展锐创建了一个sqlite数据库用来存放定时开关机相关数据

vendor\sprd\platform\packages\apps\Settings\src\com\sprd\settings\timerpower\AlarmProvider.java

?private SQLiteOpenHelper mOpenHelper;

? ? private static final int ALARMS = 1; ? ? private static final int ALARMS_ID = 2; ? ? private static final UriMatcher sURLMatcher = new UriMatcher( ? ? ? ? ? ? UriMatcher.NO_MATCH);

? ? static { ? ? ? ? sURLMatcher.addURI("com.android.settings.alarm", "timerpower", ALARMS); // uri 表名 ? ? ? ? sURLMatcher.addURI("com.android.settings.alarm", "timerpower/#", ALARMS_ID); ? ? }

? ? public static class DatabaseHelper extends SQLiteOpenHelper { ? ? ? ? private static final String DATABASE_NAME = "timerpower.db";//数据库名字 ? ? ? ? private static final int DATABASE_VERSION = 6;

? ? ? ? public DatabaseHelper(Context context) { ? ? ? ? ? ? super(context, DATABASE_NAME, null, DATABASE_VERSION); ? ? ? ? ? ? Log.v("AlarmProvider DatabaseHelper"); ? ? ? ? }

? ? ? ? @Override ? ? ? ? public void onCreate(SQLiteDatabase db) { ? ? ? ? ? ? Log.v("AlarmProvider DB ?OnCreate"); ? ? ? ? ? ? db.execSQL("CREATE TABLE timerpowers (" + ? ? ? ? ? ? ? ? ? ? ? ?"_id INTEGER PRIMARY KEY," + ? ? ? ? ? ? ? ? ? ? ? ?"hour INTEGER, " + ??? ??? ??? ?//小时 ? ? ? ? ? ? ? ? ? ? ? ?"minutes INTEGER, " +?? ??? ?//分钟 ? ? ? ? ? ? ? ? ? ? ? ?"daysofweek INTEGER, " +?? ??? ?//重复周期 ? ? ? ? ? ? ? ? ? ? ? ?"alarmtime INTEGER, " + ? ? ?//时间 ? ? ? ? ? ? ? ? ? ? ? ?"enabled INTEGER, " +?? ??? ?//开关 ? ? ? ? ? ? ? ? ? ? ? ?"vibrate INTEGER, " +?? ??? ?//震动 ? ? ? ? ? ? ? ? ? ? ? ?"message TEXT, " +?? ??? ??? ?//定时开机or关机 ? ? ? ? ? ? ? ? ? ? ? ?"alert TEXT);");?? ??? ??? ??? ?//提示语

? ? ? ? ? ? // insert default alarms ? ? ? ? ? ? String insertMe = "INSERT INTO timerpowers " + ? ? ? ? ? ? ? ? ? ? "(hour, minutes, daysofweek, alarmtime, enabled, vibrate, message, alert) " + ? ? ? ? ? ? ? ? ? ? "VALUES "; ? ? ? ? ? ? db.execSQL(insertMe + "(8, 30, 31, 0, 0, 0, 'on', '');");//默认定时开机时间 重复星期 ? ? ? ? ? ? db.execSQL(insertMe + "(9, 00, 96, 0, 0, 0, 'off', '');");//默认定时关机时间 重复星期 ? ? ? ? }

2、使用sqlite数据库打开db文件

通过sqlite我们可以清晰的查看各个表单的数据

该数据库文件存放在该目录下 data/user_de/0/com.android.settings/databases/timerpower.db

3、研究原厂实现的过程通过SQLiteDatabase进行数据更新

第三方发送广播

?Intent intent = new Intent("android.intent.action.TIMER_POWER"); ? intent.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); ? intent.putExtra("hour", "11"); ?//小时 ? intent.putExtra("minutes", "11"); //分钟 ? intent.putExtra("enabled", "0或1");//0表示关闭 1表示打开? ? intent.putExtra("daysofweek", "0或127");//0表示不重复 127表示每天重复 ? intent.putExtra("message", "on或off");//on 表示定时开机 off 表示定时关机 ? context.sendBroadcast(intent);

vendor\sprd\platform\packages\apps\Settings\src\com\sprd\settings\timerpower\AlarmInitReceiver.java

? ?@Override ? ? public void onReceive(Context context, Intent intent) { ?? ??? ?if(intent.getAction().equals("android.intent.action.TIMER_POWER")){ ?? ??? ??? ?AlarmProvider.DatabaseHelper datebaseHelper=new ?????????????AlarmProvider.DatabaseHelper(context); ?? ??? ??? ?SQLiteDatabase db=datebaseHelper.getWritableDatabase(); ?? ??? ??? ?ContentValues contentValues=new ContentValues(); ?? ??? ??? ?contentValues.put("hour",11);//小时 ?? ??? ??? ?contentValues.put("minutes",12);//分钟 ?? ??? ??? ?contentValues.put("enabled",1);//是否打开 ?? ??? ??? ?contentValues.put("daysofweek",0);//每天重复 ?? ??? ??? ?db.update("timerpowers",contentValues,"message=?",new String[]{"off"});//on 代表定时开机 off 代表定时关机? ?? ??? ??? ?db.close(); ?? ??? ?}

}

注册action

packages\apps\Settings\AndroidManifest.xml

? <receiver android:name="com.sprd.settings.timerpower.AlarmInitReceiver" > ? ? ? ? ? ? <intent-filter> ? ? ? ? ? ? ? ? <action android:name="android.intent.action.BOOT_COMPLETED" /> ? ? ? ? ? ? ? ? <action android:name="android.intent.action.TIME_SET" /> ? ? ? ? ? ? ? ? <action android:name="android.intent.action.TIMER_POWER" /> //add ? ? ? ? ? ? ? ? <action android:name="android.intent.action.TIMEZONE_CHANGED" /> ? ? ? ? ? ? ? ? <action android:name="android.intent.action.LOCALE_CHANGED" /> ? ? ? ? ? ? </intent-filter> ? ? ? ? </receiver>


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #Android #实现定时开关机 #mOpenHelper #private #static