TileService
AndroidManifest.xml
<service android:name=".systemui.KeepScreenOnTileService" android:icon="@drawable/toggleon" android:label="屏幕常亮" android:permission="android.permission.BIND_QUICK_SETTINGS_TILE" > <intent-filter> <action android:name="android.service.quicksettings.action.QS_TILE" /> </intent-filter> </service>
KeepScreenOnTileService.java
public class KeepScreenOnTileService extends TileService {
private String TAG = "KeepScreenOnTileService";
private RemoteViews mRemoteViews;
private SharedPreferences mSharedPreferences;
private Intent mServiceIntent;
private static final String ACTION_KEEP_SCREEN_ON_DISABLED = "com.samsung.systemui.action.KEEP_SCREEN_ON_DISABLED";
private NotificationManager mNotificationManager;
private static final int KEEPSCREEN_ON_NOTIFICATION = 0x1234;
@Override
public void onCreate() {
super.onCreate();
mSharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
mServiceIntent = new Intent();
mServiceIntent.setClassName("com.zw.myandroid", "com.zw.myandroid.systemui.KeepScreenOnService");
mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_KEEP_SCREEN_ON_DISABLED);
registerReceiver(mReceiver, filter, null, null);
}
// 當用戶從Edit欄添加到快速設定中調用
@Override
public void onTileAdded() {
}
// 當用戶從快速設定欄中移除的時候調用
@Override
public void onTileRemoved() {
}
// 點擊圖標的時候
@Override
public void onClick() {
handleStatusChanged();
boolean isKeepScreenOn = mSharedPreferences.getBoolean("keep_screen_on", false);
mServiceIntent.putExtra("keep_screen_on", isKeepScreenOn);
startService(mServiceIntent);
}
// 打開下拉菜單的時候調用,當快速設置按鈕並沒有在編輯欄拖到設置欄中不會調用
// 在onTileAdded之后會調用一次
@Override
public void onStartListening() {
Tile tile = getQsTile();
if (tile == null) {
return;
}
boolean isKeepScreenOn = mSharedPreferences.getBoolean("keep_screen_on", false);
Log.d(TAG, "isKeepScreenOn:" + isKeepScreenOn);
tile.setState(isKeepScreenOn ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE);
tile.updateTile();
}
// 關閉下拉菜單的時候調用,當快速設置按鈕並沒有在編輯欄拖到設置欄中不會調用
// 在onTileRemoved移除之前也會調用移除
@Override
public void onStopListening() {
Log.d(TAG, "onStopListening");
}
// 詳細信息頁面-RemoteViews
@Override
public RemoteViews semGetDetailView() {
Log.d(TAG, "semGetDetailView");
mRemoteViews = new RemoteViews(this.getPackageName(), R.layout.tile_customer);
boolean isKeepScreenOn = mSharedPreferences.getBoolean("keep_screen_on", false);
mRemoteViews.setTextViewText(R.id.textView, "開關狀態" + isKeepScreenOn);
mRemoteViews.setTextColor(R.id.textView, Color.BLACK); //需要設置顏色,不然文字看不清
return mRemoteViews;
}
// 更改狀態,刷新快捷按鈕和詳細信息
private void handleStatusChanged() {
Tile tile = getQsTile();
if (tile == null) {
return;
}
boolean isKeepScreenOn = mSharedPreferences.getBoolean("keep_screen_on", false);
updateNotification(!isKeepScreenOn);
Editor editor = mSharedPreferences.edit();
editor.putBoolean("keep_screen_on", !isKeepScreenOn);
editor.commit();
tile.setState(isKeepScreenOn ? Tile.STATE_INACTIVE : Tile.STATE_ACTIVE);
tile.updateTile();
semUpdateDetailView();
}
@Override
public CharSequence semGetDetailViewTitle() {
return "屏幕常亮";
}
@Override
public Intent semGetSettingsIntent() {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
return intent;
}
//詳細信息是否有開關
@Override
public boolean semIsToggleButtonExists() {
return true;
}
//詳細信息開關點擊事件
@Override
public void semSetToggleButtonChecked(boolean checked) {
super.semSetToggleButtonChecked(checked);
handleStatusChanged();
semUpdateDetailView();
}
//詳細信息開關初始化
@Override
public boolean semIsToggleButtonChecked() {
boolean isKeepScreenOn = mSharedPreferences.getBoolean("keep_screen_on", false);
return isKeepScreenOn;
}
@Override
public CharSequence semGetDetailViewSettingButtonName() {
return null;
}
//啟用禁用自定義快捷方式,不會自動刷新
public static void setTilesEnabled(Context context, boolean enable) {
final PackageManager pm = context.getPackageManager();
String ACTIVITY_NAME = "com.zw.myandroid.systemui.KeepScreenOnTileService";
String PKG_NAME = "com.zw.myandroid";
ComponentName cn = new ComponentName(PKG_NAME, ACTIVITY_NAME);
pm.setComponentEnabledSetting(cn, enable ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
: PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_KEEP_SCREEN_ON_DISABLED.equals(action)) {
handleStatusChanged();
}
}
};
//發送或者刪除通知
private void updateNotification(boolean enabled) {
if (enabled) {
Intent intent = new Intent();
intent.setAction(ACTION_KEEP_SCREEN_ON_DISABLED);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.drawable.toggleon).setVisibility(Notification.VISIBILITY_PUBLIC)
.setContentTitle("保持屏幕常亮").setShowWhen(false)
.setOngoing(true).addAction(0, "關閉", pendingIntent);
mNotificationManager.notify(KEEPSCREEN_ON_NOTIFICATION, builder.build());
} else {
Log.d(TAG, "cancelNotification!!!");
mNotificationManager.cancel(KEEPSCREEN_ON_NOTIFICATION);
}
}
}
KeepScreenOnService.java
public class KeepScreenOnService extends Service {
private PowerManager mPowerManager;
private PowerManager.WakeLock mCpuWakeLock;
SharedPreferences mSharedPreferences;
@Override
public void onCreate() {
super.onCreate();
mSharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
mCpuWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP|PowerManager.ON_AFTER_RELEASE, "wake_up");
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
boolean isKeepScreenOn = mSharedPreferences.getBoolean("keep_screen_on", false);
if (isKeepScreenOn) {
mCpuWakeLock.acquire();
} else {
if (mCpuWakeLock.isHeld()) {
mCpuWakeLock.release();
}
}
return START_STICKY;
}
@Override
public void onDestroy() {
if (mCpuWakeLock.isHeld()) {
mCpuWakeLock.release();
}
}
}
Keep screen on
1.在Activity中添加
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
2.界面布局文件中直接設置
android:keepScreenOn="true"
3.使用WAKE_LOCK
<uses-permission android:name="android.permission.WAKE_LOCK" /> PowerManager.WakeLock mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE); mCpuWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP|PowerManager.ON_AFTER_RELEASE, "wake_up"); // mCpuWakeLock.setReferenceCounted(false); //是否需計算鎖的數量 mCpuWakeLock.acquire(); mCpuWakeLock.release(); /* PARTIAL_WAKE_LOCK: 保持CPU 運轉,屏幕和鍵盤燈可以關閉。 SCREEN_DIM_WAKE_LOCK: 保持CPU 運轉,保持屏幕顯示,但可以變暗,允許鍵盤燈關閉。 SCREEN_BRIGHT_WAKE_LOCK:保持CPU 運轉,允許保持屏幕高亮顯示,允許鍵盤燈關閉。 FULL_WAKE_LOCK: 保持CPU 運轉,保持屏幕和鍵盤燈都高亮顯示。 ACQUIRE_CAUSES_WAKEUP: 當獲取鎖后,立刻亮屏,典型地使用在通知中,以讓用戶立刻查看。 ON_AFTER_RELEASE: 在釋放鎖(release())后,手機屏幕仍會繼續亮一會兒。 */
