您的位置:首页 > 其它

原生短信添加一个自动亮屏的功能(不包括彩信)

2017-11-25 21:24 302 查看
最近做了伊朗项目,要求添加一个来短信(原生)之后自动亮屏的功能,在jira上搜索没人做过这个需求,于是尝试自己来完成;

(1)第一步:添加设置项,并存储设置值。

在xml文件增加设置项

<CheckBoxPreference
android:defaultValue="@bool/notification_wake_screen_default"
android:dependency="@string/notifications_enabled_pref_key"
android:key="@string/notification_wake_screen_pref_key"
android:persistent="true"
android:title="@string/notification_wake_screen_pref_title"
android:summary="@string/pref_summary_wake_screen_notification" />

android:dependency:由于这个依赖于上一个“通知”选项,所以设置依赖的选项。
android:defaultValue:根据客户的要求,这个默认开启,获取配置文件中的默认值--【1】
android:title:设置标题
android:summary:设置详细信息
android:persistent="true":是否保存在sharepreference,他用来保存这个设置项。--【2】

其实为了方便操作,我没有使用perfernce自带的存储设置的功能,【1】【2】无效设置


设置值
public static void enableNotificationsWakeupScreen(boolean enabled, Context context) {
// Store the value of notifications wakeup screen in SharedPreferences
SharedPreferences.Editor editor
= PreferenceManager.getDefaultSharedPreferences(context).edit();
editor.putBoolean(NOTIFICATION_WAKEUP_SCREEN_ENABLED, enabled);
editor.apply();
}

获取设置项中的值:
public static boolean isNotificationWakeupScreenEnable() {
SharedPreferences prefs
= PreferenceManager.getDefaultSharedPreferences(mContext);
boolean enable = prefs.getBoolean(NOTIFICATION_WAKEUP_SCREEN_ENABLED,
true);
return enable;
}

如果找不到这个值,那么说明这是第一次查询这个设置,所以根据客户要求默认获取为true
同时持久化这个值,下面这个操作是在初始化设置项进行的。
if(mNotificationsWakeupScreenPreference != null){
mNotificationsWakeupScreenPreference.setChecked(isNotificationWakeupScreenEnable());
}


第二步:短信来了,根据设置判断是否亮屏

短信会通过广播传递到短信应用,在AndroidMainfest.xml,找到对应的广播,根据日志,短信来了之后会到下面这个函数,他负责分发短信。

public static void deliverSmsMessages(final Context context, final int subId,

final int errorCode, final android.telephony.SmsMessage[] messages) ;

短信来了之后判断设置的值是否为true,如果为true就亮屏,否则不管他

public static void deliverSmsMessages(final Context context, final int subId,
final int errorCode, final android.telephony.SmsMessage[] messages)
{
......
if(NotificationPreferenceActivity.isNotificationWakeupScreenEnable()){
Log.d(TAG, "==== wakeup screen ====");
wakeUpScreen(context);
}

}

public static void wakeUpScreen(Context context){
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
final PowerManager.WakeLock wl =pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "bright");
wl.acquire();
wl.release();
}


如果是彩信的话,就不能这么做了,因为彩信在接收之后要下载彩信信息,这样的话亮屏之后不会显示彩信信息。特别是彩信内部比较大的情况。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐