A. 如何修改安卓手机的通知栏时间、日期和通知颜色
安卓手机通知栏个性化教程对于追求个性化界面的android手机用户,修改通知栏时间、日期和通知颜色可以为手机增添新鲜感。以下是详细的步骤:
一、修改时间颜色
二、修改日期颜色
三、修改通知字体颜色
B. android8.0之悬浮窗和通知栏
悬浮窗:
使用场景:例如微信在视频的时候,点击Home键,视频小窗口仍然会在屏幕上显示;
注意事项:
1、一般需要在后台进行操作的时候才需要悬浮窗,这样悬浮窗才有意义;
2、API Level >= 23的时候,需要在AndroidManefest.xml文件中声明权限SYSTEM_ALERT_WINDOW才能在其他应用上绘制控件。
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />;除了这个权限外,我们还需要在系统设置里面对本应用进行设置悬浮窗权限。该权限在应用中需要启动Settings.ACTION_MANAGE_OVERLAY_PERMISSION来让用户手动设置权限:startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())), REQUEST_CODE);
3、LayoutParam设置:LayoutParam里的type变量。这个变量是用来指定窗口类型的。在设置这个变量时,需要注意一个坑,那就是需要对不同版本的Android系统进行适配。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
};在Android 8.0之前,悬浮窗口设置可以为TYPE_PHONE,这种类型是用于提供用户交互操作的非应用窗口。
而Android 8.0对系统和API行为做了修改,包括使用SYSTEM_ALERT_WINDOW权限的应用无法再使用一下窗口类型来在其他应用和窗口上方显示提醒窗口:
- TYPE_PHONE
- TYPE_PRIORITY_PHONE
- TYPE_SYSTEM_ALERT
- TYPE_SYSTEM_OVERLAY
- TYPE_SYSTEM_ERROR
如果需要实现在其他应用和窗口上方显示提醒窗口,那么必须该为TYPE_APPLICATION_OVERLAY的新类型;
如果在Android 8.0以上版本仍然使用TYPE_PHONE类型的悬浮窗口,则会出现如下异常信息:
android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@f8ec928 -- permission denied for window type 2002;
具体实现:
1、Activity:
public void startFloatingService(View view) {
...
if (!Settings.canDrawOverlays(this)) {
Toast.makeText(this, "当前无权限,请授权", Toast.LENGTH_SHORT);
startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())), 0);
} else {
startService(new Intent(MainActivity.this, FloatingService.class));
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (!Settings.canDrawOverlays(this)) {
Toast.makeText(this, "授权失败", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "授权成功", Toast.LENGTH_SHORT).show();
startService(new Intent(MainActivity.this, FloatingService.class));
}
}
}
2、service:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
showFloatingWindow();
return super.onStartCommand(intent, flags, startId);
}
private void showFloatingWindow() {
if (Settings.canDrawOverlays(this)) {
// 获取WindowManager服务
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
// 新建悬浮窗控件
Button button = new Button(getApplicationContext());
button.setText("Floating Window");
button.setBackgroundColor(Color.BLUE);
// 设置LayoutParam
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
}
layoutParams.format = PixelFormat.RGBA_8888;
layoutParams.width = 500;
layoutParams.height = 100;
layoutParams.x = 300;
layoutParams.y = 300;
// 将悬浮窗控件添加到WindowManager
windowManager.addView(button, layoutParams);
}
}
效果展示:
C. android 4.2 怎样禁止通知栏下拉呢
1.在AndroidManifest.xml中添加权限
<uses-permission android:name="android.permission.EXPAND_STATUS_BAR"/>
<uses-permission android:name="android.permission.STATUS_BAR"/>
2.在相应的activity中添加
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
try {
Object service = getSystemService("statusbar");
Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
Method test = statusbarManager.getMethod("collapse");
test.invoke(service);
} catch (Exception ex) {
ex.printStackTrace();
}
}
D. android通知栏怎么添加控件
Notification的自定义布局是RemoteViews,和其他RemoteViews一样,在自定义视图布局文件中,仅支持FrameLayout、LinearLayout、RelativeLayout三种布局控件和AnalogClock、Chronometer、Button、ImageButton、ImageView、ProgressBar、TextView、ViewFlipper、ListView、GridView、StackView和AdapterViewFlipper这些显示控件,不支持这些类的子类或Android提供的其他控件。否则会引起ClassNotFoundException异常
步骤如下:
1)创建自定义视图
2)获取远程视图对象(注:Notification的contentView不能为空)
3)设置PendingIntent(来响应各种事件)
4)发起Notification
大体4步骤这里就不详细说了,下面就把DEMO中的列子拿出来说下
样式:
1.自定义带按钮通知栏(如下样式)
正在进行的
“正在进行的”通知使用户了解正在运行的后台进程。例如,音乐播放器可以显示正在播放的音乐。也可以用来显示需要长时间处理的操作,例如下载或编码视频。“正在进行的”通知不能被手动删除。
实现方法如下:
java">实现方法如下:
/**
*带按钮的通知栏
*/
publicvoidshowButtonNotify(){
NotificationCompat.BuildermBuilder=newBuilder(this);
RemoteViewsmRemoteViews=newRemoteViews(getPackageName(),R.layout.view_custom_button);
mRemoteViews.setImageViewResource(R.id.custom_song_icon,R.drawable.sing_icon);
//API3.0以上的时候显示按钮,否则消失
mRemoteViews.setTextViewText(R.id.tv_custom_song_singer,"周杰伦");
mRemoteViews.setTextViewText(R.id.tv_custom_song_name,"七里香");
//如果版本号低于(3。0),那么不显示按钮
if(BaseTools.getSystemVersion()<=9){
mRemoteViews.setViewVisibility(R.id.ll_custom_button,View.GONE);
}else{
mRemoteViews.setViewVisibility(R.id.ll_custom_button,View.VISIBLE);
}
//
if(isPlay){
mRemoteViews.setImageViewResource(R.id.btn_custom_play,R.drawable.btn_pause);
}else{
mRemoteViews.setImageViewResource(R.id.btn_custom_play,R.drawable.btn_play);
}
//点击的事件处理
IntentbuttonIntent=newIntent(ACTION_BUTTON);
/*上一首按钮*/
buttonIntent.putExtra(INTENT_BUTTONID_TAG,BUTTON_PREV_ID);
//这里加了广播,所及INTENT的必须用getBroadcast方法
PendingIntentintent_prev=PendingIntent.getBroadcast(this,1,buttonIntent,PendingIntent.FLAG_UPDATE_CURRENT);
mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_prev,intent_prev);
/*播放/暂停按钮*/
buttonIntent.putExtra(INTENT_BUTTONID_TAG,BUTTON_PALY_ID);
PendingIntentintent_paly=PendingIntent.getBroadcast(this,2,buttonIntent,PendingIntent.FLAG_UPDATE_CURRENT);
mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_play,intent_paly);
/*下一首按钮*/
buttonIntent.putExtra(INTENT_BUTTONID_TAG,BUTTON_NEXT_ID);
PendingIntentintent_next=PendingIntent.getBroadcast(this,3,buttonIntent,PendingIntent.FLAG_UPDATE_CURRENT);
mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_next,intent_next);
mBuilder.setContent(mRemoteViews)
.setContentIntent(getDefalutIntent(Notification.FLAG_ONGOING_EVENT))
.setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示
.setTicker("正在播放")
.setPriority(Notification.PRIORITY_DEFAULT)//设置该通知优先级
.setOngoing(true)
.setSmallIcon(R.drawable.sing_icon);
Notificationnotify=mBuilder.build();
notify.flags=Notification.FLAG_ONGOING_EVENT;
mNotificationManager.notify(notifyId,notify);
}
如果您对回答满意,请关注一下俺的微博