问题一: in layout has no declaration in the base layout folder
在Android Studio的 File 里有一个 Invalidate Caches / Restart 点击它再从弹出窗里选择 Invalidate and Restart.
Android Studio重启后正常。问题解决。
问题二:解决组件化开发butterknife 在 library中使用的坑
https://blog.csdn.net/zyw0101/article/details/80399225 组件化开发butterknife 在 library中使用
java.lang.BootstrapMethodError: Exception from call site #0 bootstrap method
ButterKnife requires that you enable Java 8 in your builds to function as of version 9.0.0 and newer. You can learn more about how to enable this at https://developer.android.com/studio/write/java8-support.
Butterknife 9.0.0以后的版本需要Java8的支持。
在build.gradle中加入:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
问题三:android icon 放在mipmap中显示不出来
直接删除选中的文件夹,有相关报错直接删除对应文件
问题四:android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
解决方法:加入通知
public class DataGetterService extends Service {
private static final int ROLL_MESSAGE = 1001;
private static final long DELAY_TIME = 10000;
PowerManager.WakeLock wl;
private MainPresenter mPresenter ;
private Handler rollHandler = new Handler() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if (msg.what == ROLL_MESSAGE) {
mPresenter.getData();
rollHandler.sendEmptyMessageDelayed(ROLL_MESSAGE,DELAY_TIME);
}
}
};
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel cn = new NotificationChannel("40", "datagetterservice", NotificationManager.IMPORTANCE_DEFAULT);
cn.enableLights(false);
cn.enableVibration(false);
cn.setSound(null, null);
nm.createNotificationChannel(cn);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "40");
builder.setVibrate(new long[]{0}).setSound(null);
startForeground(2, builder.build());
mPresenter = new MainPresenter();
rollHandler.sendEmptyMessage(ROLL_MESSAGE);
wl = ((PowerManager) getSystemService(Context.POWER_SERVICE)).newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "getdata");
wl.acquire();
Log.v("dawn","oncreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v("dawn","onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
if (rollHandler != null) {
rollHandler.removeMessages(ROLL_MESSAGE);
}
if (mPresenter!=null) {
mPresenter.onUnsubscribe();
}
stopForeground(true);
Log.i("dawn", "onDestroy: 服务关闭");
if (wl != null) {
wl.release();
}
super.onDestroy();
}
}