1.使用通知以及PendingIntent
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { Context context=getApplicationContext(); Intent intent = new Intent(context, notification.class); PendingIntent pendingIntent = PendingIntent.getActivity( context,1,intent,PendingIntent.FLAG_UPDATE_CURRENT ); String channelId = "ChannelId"; NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); Notification notification = new Notification.Builder(context) .setChannelId(channelId) .setContentTitle("This is content title") .setContentText("This is content text") .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)) .setContentIntent(pendingIntent) .setAutoCancel(true) .setVibrate(new long[]{0,1000,1000,1000}) .build(); NotificationChannel channel = new NotificationChannel(channelId,"渠道名称",NotificationManager.IMPORTANCE_DEFAULT); manager.createNotificationChannel(channel); manager.notify(1,notification); }
2.使用前台服务
要加上权限
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
同时通知部分的代码也要修改
//android 8.0以后新增 String CHANNEL_ONE_ID="com.example.servicetest"; String CHANNEL_ONE_NAME = "Channel One"; NotificationChannel notificationChannel = null; if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_HIGH); notificationChannel.enableLights(true); notificationChannel.setLightColor(Color.RED); notificationChannel.setShowBadge(true); notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); manager.createNotificationChannel(notificationChannel); //创建前台服务 Intent intent = new Intent(this,MainActivity.class); PendingIntent pi = PendingIntent.getActivity(this,0,intent,0); Notification notification = new Notification.Builder(this) .setChannelId(CHANNEL_ONE_ID)//新增 .setContentTitle("This is content title") .setContentText("This is content title") .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setContentIntent(pi) .build(); notification.flags |= Notification.FLAG_NO_CLEAR;//新增 startForeground(1,notification); }
3.okhttp
注册文件里新加
android:usesCleartextTraffic="true"
android:networkSecurityConfig="@xml/network_security_config"
在res下新建xml目录,新建文件 network_security_config.xml
<?xml version="1.0" encoding="utf-8"?> <network-security-config > <base-config cleartextTrafficPermitted="true" /> </network-security-config>
同时gradle文件里两处新加
android 包里
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
dependencies 包里
implementation 'com.squareup.okhttp3:okhttp:3.14.2'
4.新建项目时build问题
切换镜像源,使用vpn,在gradle所有
repositories
里添加
maven{ url 'http://maven.aliyun.com/nexus/content/repositories/central/'}
5.support.v4,v7,v13里的控件(toolbar,reycleview),现在都可以在androidx里面找了,直接导入对应的androidx.包即可
6.使用NavigationView
在build里添加
implementation 'de.hdodenhof:circleimageview:2.1.0' implementation 'com.android.support:design:29.1.1'
使用的时候是
<com.google.android.material.navigation.NavigationView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/nav_view" app:menu="@menu/nav_menu" android:layout_gravity="start" app:headerLayout="@layout/nav_header"/>
7. 使用litepal
首先在gradle里加上
implementation 'org.litepal.android:java:3.0.0'
然后在main下新建asserts目录,目录下新建 litepal.xml
<?xml version="1.0" encoding="utf-8" ?> <litepal> <dbname value="BookStore" ></dbname> <version value="2" ></version> <list> <mapping class="com.example.litepal.Book"></mapping> <mapping class="com.example.litepal.Category"></mapping> </list> </litepal>
注册文件里再加
android:name="org.litepal.LitePalApplication"
litepal 2.0以后有了新的api
首先DataSupport -> LitepalSupport
8.使用下拉刷新
androidx.swiperefreshlayout.widget.SwipeRefreshLayout
要在build里导入一个design包
implementation 'com.android.support:design:29.1.1'