快速搭建直播平台,主页面添加一个常亮按钮
快速搭建直播平台,主页面添加一个常亮按钮实现的相关代码
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);
}
}
}
以上就是快速搭建直播平台,主页面添加一个常亮按钮实现的相关代码, 更多内容欢迎关注之后的文章