Android开发过程中的小细节
1.通过Service创建一个Presentation在虚拟屏中展示,5秒后会ANR
原因:Android 8.0 的应用尝试在不允许其创建后台服务的情况下使用 startService() 函数,则该函数将引发一个 IllegalStateException
解决:https://blog.csdn.net/sinat_20059415/article/details/80584487
在服务启动后5s内发送一个通知
startForeground(1,notification)
private fun startNotification() {
var mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// 通知渠道的id
var id = "info_flow_01";
// 用户可以看到的通知渠道的名字.
var name = "渠道名称";
// 用户可以看到的通知渠道的描述
var description = "渠道描述";
var importance = NotificationManager.IMPORTANCE_HIGH;
var mChannel = NotificationChannel(id, name, importance);
// 配置通知渠道的属性
mChannel.description = description;
// 设置通知出现时的闪灯(如果 android 设备支持的话)
mChannel.enableLights(true); mChannel.setLightColor(Color.RED);
// 设置通知出现时的震动(如果 android 设备支持的话)
mChannel.enableVibration(true);
mChannel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400);
// 最后在notificationmanager中创建该通知渠道 //
mNotificationManager.createNotificationChannel(mChannel);
// 为该通知设置一个id
var notifyID = 1;
// 通知渠道的id
var CHANNEL_ID = "info_flow_01";
// Create a notification and set the notification channel.
var notification = Notification.Builder(this)
.setContentTitle("New Message") .setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setChannelId(CHANNEL_ID)
.build();
startForeground(1,notification);
}
2.发送广播无反应
private fun sendBroadcast(displayId: Int, cardType: Int?, cardSizeType: Int) {
val intent = Intent()
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
val packageName = "com.noboauto.app1" //另一个app的包名
val className = "com.nb.a3app1.CardFlowReceiver" //另一个app要启动的组件的全路径名
val actionName = "com.nobo.auto.info.flow.card"//启动指定action的组件
val cardBean = CardBean(displayId,cardType,cardSizeType,packageName,actionName);
intent.putExtra("data", Json.encodeToString(cardBean))
intent.action = actionName
sendBroadcast(intent)
}
原因:是Android 8.0的问题,看了这个博客就解决了。
https://blog.csdn.net/kongqwesd12/article/details/78998151
解决:加上这句
intent.component = ComponentName(packageName,className)
private fun sendBroadcast(displayId: Int, cardType: Int?, cardSizeType: Int) {
val intent = Intent()
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
val packageName = "com.noboauto.app1" //另一个app的包名
val className = "com.nb.a3app1.CardFlowReceiver" //另一个app要启动的组件的全路径名
val actionName = "com.nobo.auto.info.flow.card"//启动指定action的组件
val cardBean = CardBean(displayId,cardType,cardSizeType,packageName,actionName);
intent.putExtra("data", Json.encodeToString(cardBean))
intent.action = actionName
intent.component = ComponentName(packageName,className)
sendBroadcast(intent)
}
3.当你构建的项目因为各种原因下不下来时,SDKManager不显示未安装的sdk时的解决办法
引用:https://www.jianshu.com/p/01354d90e199
原因:国内各种各样的原因限制了google的域名访问
解决:通过配置host文件访问域名对应的ip
1.通过爱网站链接,ping出dl.google.com域名成功的ip,可找出响应时间最短的ip
命令行中输入Ping IP 确认能连接
记事本打开hosts文件。路径:C:\Windows\System32\drivers\etc
追加host文件:
203.208.43.70 dl.google.com
203.208.43.70 dl.l.google.com
203.208.43.70 dl-ssl.google.com
保存成功,重启as就OK了