RK Android5.1 MTK SchedulePowerOnOff

 一.初解定时开关机

1.1.packages\apps\SchedulePowerOnOff\src\com\mediatek\schpwronoff\AlarmProvider.java

 建表 插入默认定时开关机时间  查询、修改、插入、删除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
private static class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "schpwrs.db";
    private static final int DATABASE_VERSION = 5;
 
    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
 
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE schpwrs (" + "_id INTEGER PRIMARY KEY," + "hour INTEGER, " + "minutes INTEGER, "
                + "daysofweek INTEGER, " + "alarmtime INTEGER, " + "enabled INTEGER, " + "vibrate INTEGER, "
                + "message TEXT, " + "alert TEXT);");
 
        // insert default alarms
        String insertMe = "INSERT INTO schpwrs "
                + "(hour, minutes, daysofweek, alarmtime, enabled, vibrate, message, alert) " + "VALUES ";
        db.execSQL(insertMe + "(9, 0, 127, 0, 0, 1, '', '');");
        db.execSQL(insertMe + "(23, 00, 127, 0, 0, 1, '', '');");
    }
 //在数据库的版本发生变化时会被调用 一般在软件升级时才需改变版本号 而数据库的版本是由程序员控制的 
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion) {
        Log.d("@M_" + TAG, "Upgrading schpwrs database from version " + oldVersion + " to " + currentVersion
                + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS schpwrs");
        onCreate(db);
    }
}

1.2.packages\apps\SchedulePowerOnOff\src\com\mediatek\schpwronoff\Alarm.java  数据库的工具类

1
2
3
4
5
public static class Columns implements BaseColumns {
    /**
     * The content:// style URL for this table
     */
    public static final Uri CONTENT_URI = Uri.parse("content://com.mediatek.schpwronoff/schpwr");

  Alarm 的构造方法  游标来取值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
 * Alarm object constructor
 * @param c Cursor
 */
public Alarm(Cursor c) {
    mId = c.getInt(Columns.ALARM_ID_INDEX);
    mEnabled = c.getInt(Columns.ALARM_ENABLED_INDEX) == 1;
    mHour = c.getInt(Columns.ALARM_HOUR_INDEX);
    mMinutes = c.getInt(Columns.ALARM_MINUTES_INDEX);
    mDaysOfWeek = new DaysOfWeek(c.getInt(Columns.ALARM_DAYS_OF_WEEK_INDEX));
    mTime = c.getLong(Columns.ALARM_TIME_INDEX);
    mVibrate = c.getInt(Columns.ALARM_VIBRATE_INDEX) == 1;
    mLabel = c.getString(Columns.ALARM_MESSAGE_INDEX);
    String alertString = c.getString(Columns.ALARM_ALERT_INDEX);
    if (Alarms.ALARM_ALERT_SILENT.equals(alertString)) {
        Log.d("@M_" + TAG, "Alarm is marked as silent");
        mSilent = true;
    } else {
        if (alertString != null && alertString.length() != 0) {
            mAlert = Uri.parse(alertString);
        }
 
        // If the database alert is null or it failed to parse, use the
        // default alert.
        if (mAlert == null) {
            mAlert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        }
    }
}

1.3. packages\apps\SchedulePowerOnOff\src\com\mediatek\schpwronoff\Alarms.java

设置定时开关机时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public static void setAlarm(Context context, int id, boolean enabled, int hour, int minutes,
        Alarm.DaysOfWeek daysOfWeek, boolean vibrate, String message, String alert) {
    final int initSize = 8;
    ContentValues values = new ContentValues(initSize);
    ContentResolver resolver = context.getContentResolver();
    // Set the alarm_time value if this alarm does not repeat. This will be
    // used later to disable expired alarms.
    long time = 0;
    if (!daysOfWeek.isRepeatSet()) {
        time = calculateAlarm(hour, minutes, daysOfWeek).getTimeInMillis();
    }
 
    Log.d("@M_" + TAG, "**  setAlarm * idx " + id + " hour " + hour + " minutes " + minutes
            + " enabled " + enabled + " time " + time);
 
    values.put(Alarm.Columns.ENABLED, enabled ? 1 : 0);
    values.put(Alarm.Columns.HOUR, hour);
    values.put(Alarm.Columns.MINUTES, minutes);
    values.put(Alarm.Columns.ALARM_TIME, time);
    values.put(Alarm.Columns.DAYS_OF_WEEK, daysOfWeek.getCoded());
    values.put(Alarm.Columns.VIBRATE, vibrate);
    values.put(Alarm.Columns.MESSAGE, message);
    values.put(Alarm.Columns.ALERT, alert);
    resolver.update(ContentUris.withAppendedId(Alarm.Columns.CONTENT_URI, id), values, null,
            null);
 
    if (id == 1) {
        // power on
        setNextAlertPowerOn(context);
    } else if (id == 2) {
        // power off
        setNextAlertPowerOff(context);
    }
}

setNextAlertPowerOff -> enableAlert(context, alarm, alarm.mTime);

am.setExact(AlarmManager.RTC_WAKEUP, atTimeInMillis, sender);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
private static void enableAlert(Context context, final Alarm alarm, final long atTimeInMillis) {
    if (alarm == null) {
        return;
    }
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Log.d("@M_" + TAG, "** enableAlertPowerOff id " + alarm.mId + " atTime " + atTimeInMillis);
    Intent intent = new Intent(context, com.mediatek.schpwronoff.SchPwrOffReceiver.class);
    Parcel out = Parcel.obtain();
    alarm.writeToParcel(out, 0);
    out.setDataPosition(0);
    intent.putExtra(ALARM_RAW_DATA, out.marshall());
    PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    am.setExact(AlarmManager.RTC_WAKEUP, atTimeInMillis, sender);
    Calendar c = Calendar.getInstance();
    c.setTime(new java.util.Date(atTimeInMillis));
 
    String poweroffdate=getFormatedDateTime("yy-MM-dd HH:mm:ss",atTimeInMillis);   
     Log.d("@M_" + TAG, "Alarms.enableAlertPowerOff(): setAlert id " + alarm.mId + " poweroffdate "
            + poweroffdate);           
}
 
private static void disableAlert(Context context) {
    Intent intent = new Intent(context, com.mediatek.schpwronoff.SchPwrOffReceiver.class);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    am.cancel(sender);
    Log.d("@M_" + TAG, "Alarms.disableAlertPowerOff(): disableForPowerOff");
}

1.4.packages\apps\SchedulePowerOnOff\src\com\mediatek\schpwronoff\AlarmInitReceiver.java

调起AlarmReceiverService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class AlarmInitReceiver extends BroadcastReceiver {
    private static final String TAG = "AlarmInitReceiver";
 
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.d("@M_" + TAG, "AlarmInitReceiver" + action);
        if (context.getContentResolver() == null) {
            Log.e("@M_" + TAG, "AlarmInitReceiver: FAILURE unable to get content resolver.  Alarms inactive.");
            return;
        }
        AlarmReceiverService.processBroadcastIntent(context, intent);
    }
}

 收到开机广播 开始设置下次定时开关机时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@Override
protected void onHandleIntent(Intent intent) {
 
    final String action = intent.getAction();
    if (!ACTION_BROADCAST.equals(action)) {
        return;
    }
            if(!isFromPowerOn())
            {
                return;
            }
    final Intent broadcastIntent = intent
            .getParcelableExtra(Intent.EXTRA_INTENT);
    final String broadcastAction = broadcastIntent.getAction();
 
    if (Intent.ACTION_BOOT_COMPLETED.equals(broadcastAction)) {
        // ALPS00448092.
        Log.i("@M_" + TAG, "action= " + broadcastAction);
        boolean b = copyDbFileFromDataPath();
        Log.w("@M_" + TAG, "copy db file result " + b);
        Alarms.saveSnoozeAlert(this, -1, -1);
        Alarms.disableExpiredAlarms(this);
        Alarms.setNextAlert(this);
    } else {
        //Alarms.setNextAlert(this);
    }
}

  

  

 

posted @   CrushGirl  阅读(348)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
历史上的今天:
2020-12-13 Java环境、Eclipse配置
点击右上角即可分享
微信分享提示