Android常用代码-阿里云推送

集成依赖

1
implementation 'com.aliyun.ams:alicloud-android-push:3.1.2'

核心代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* 初始化云推送通道
*/
private void initCloudChannel(Context applicationContext) {

//8.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 通知渠道的id
String id = "1";
// 用户可以看到的通知渠道的名字.
CharSequence name = "测试渠道名";
// 用户可以看到的通知渠道的描述
String description = "测试渠道描述";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
mChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
// 配置通知渠道的属性
mChannel.setDescription(description);
// 设置通知出现时的闪灯(如果 android 设备支持的话)
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.setSound(Uri.parse("android.resource://" + BaseUtil.getPackageInfo(BaseUtil.getApp()).packageName + "/" + R.raw.hint_four), null);
// 设置通知出现时的震动(如果 android 设备支持的话)
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200 , 300, 400, 500, 400, 300, 200, 400});
//最后在notificationmanager中创建该通知渠道
if (mNotificationManager != null) {
mNotificationManager.createNotificationChannel(mChannel);
}
}

PushServiceFactory.init(applicationContext);
CloudPushService pushService = PushServiceFactory.getCloudPushService();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
pushService.setNotificationSoundFilePath(Uri.parse("android.resource://" + BaseUtil.getPackageInfo(BaseUtil.getApp()).packageName + "/" + R.raw.hint_four).toString());
}
pushService.register(applicationContext, new CommonCallback() {
@Override
public void onSuccess(String response) {
Log.d(TAG, "init cloudchannel success");
pushService.turnOnPushChannel(null);
}

@Override
public void onFailed(String errorCode, String errorMessage) {
Log.d(TAG, "init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
}
});

}

AndroidStudio技巧-发布library到jitpack并带注释

在project的build.gralde中添加classpath

1
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'

注意:如果没有加google(),必须加上这个,并且buildscript和allprojects都得加上。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}

Android疑难杂症-自定义RecyclerView的LayoutManager

禁用RecyclerView手动滑动功能;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class PlanDateRecyclerView extends RecyclerView {
public PlanDateRecyclerView(Context context) {
super(context);
init();
}

public PlanDateRecyclerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}

public PlanDateRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

private void init() {
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
}
}

Android疑难杂证-解决ViewPager和Fragment中水平滑动控件冲突问题

终极解决方案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import zhouzhuo810.me.zzandframe.ui.widget.ZzViewPager;

/**
* Created by zhouzhuo810 on 2018/4/11.
*/
public class NoConflictViewPager extends ZzViewPager {
public NoConflictViewPager(Context context) {
super(context);
}

public NoConflictViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
//这里添加冲突的控件类型
if (v instanceof PickerView) {
return true;
}
return super.canScroll(v, checkV, dx, x, y);
}
}

Android常用代码-再按一次退出App

核心代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Override
public void onBackPressed() {
if (isExit) {
finish();
} else {
isExit = true;
ToastUtils.showCustomBgToast(getString(R.string.press_angin_exit_text));
new Timer().schedule(new TimerTask() {
@Override
public void run() {
isExit = false;
}
}, 2000);
}
}

Android-8.0新特性-服务启动问题

问题描述

  • Android 8.0 启动后台service 出错 IllegalStateException: Not allowed to start service Intent

原因

  • 极光推送老版本sdk启动推送服务未兼容8.0才出现的问题;

Android O对应用在后台运行时可以执行的操作施加了限制,称为后台执行限制(Background Execution Limits),这可以大大减少应用的内存使用和耗电量,提高用户体验。后台执行限制分为两个部分:后台服务限制(Background Service Limitations)、广播限> 制(BroadcastLimitations)。