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);
}
});

}

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常用代码-自定义View和ViewGroup

一、自定义View

自定义属性

  • 以ZzHorizontalProgressBar为例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//attrs.xml
<declare-styleable name="ZzHorizontalProgressBar">
<!--大小-->
<attr name="zpb_padding" format="dimension" />
<!--颜色-->
<attr name="zpb_bg_color" format="color|reference" />
<attr name="zpb_pb_color" format="color|reference" />
<attr name="zpb_second_pb_color" format="color|reference" />
<!--整数-->
<attr name="zpb_max" format="integer" />
<attr name="zpb_progress" format="integer" />
<attr name="zpb_second_progress" format="integer" />
<!--布尔-->
<attr name="zpb_show_second_progress" format="boolean" />
<!--枚举-->
<attr name="zpb_show_second_point_shape" format="enum">
<enum name="point" value="0"/>
<enum name="line" value="1"/>
</attr>
</declare-styleable>

Android常用代码-横竖屏切换

自动切换

  • Android Manifest在需要切换的Activity中添加
1
2
android:configChanges="orientation|keyboardHidden|layoutDirection|screenSize|screenLayout"
android:screenOrientation="sensor"
  • res添加layout-land和layout-port文件夹,分别放两套布局。

  • Activity代码中实现

1
2
3
4
5
6
7
8
9
10
11
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
//切换到竖屏
setContentView(R.layout.activity_main);
}else{
//切换到横屏
setContentView(R.layout.activity_main);
}
}

Android常用代码-EditText各种样式背景

底部变色直线

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
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:bottom="1px" android:state_focused="true">
<layer-list>
<item>
<shape>
<solid android:color="@color/colorPrimary" />
</shape>
</item>
<item android:bottom="1px">
<shape>
<solid android:color="@android:color/white" />
</shape>
</item>
</layer-list>
</item>
<item android:bottom="1px" android:state_pressed="false">
<layer-list>
<item>
<shape>
<solid android:color="#ddd" />
</shape>
</item>
<item android:bottom="1px">
<shape>
<solid android:color="@android:color/white" />
</shape>
</item>
</layer-list>
</item>
</selector>

Android常用代码-发送验证码倒计时

使用RxJava优雅的实现

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
btnVerifyCode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnVerifyCode.setEnabled(false);
Observable.interval(0, 1, TimeUnit.SECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(AndroidSchedulers.mainThread())
.limit(31)
.map(new Func1<Long, Long>() {
@Override
public Long call(Long aLong) {
return 30 - aLong;
}
})
.doOnSubscribe(new Action0() {
@Override
public void call() {
btnVerifyCode.setEnabled(false);
}
})
.doOnCompleted(new Action0() {
@Override
public void call() {
btnVerifyCode.setEnabled(true);
btnVerifyCode.setText("重新发送");
}
})
.subscribe(new Action1<Long>() {
@Override
public void call(Long aLong) {
btnVerifyCode.setText("重新发送("+aLong + " s )");
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
ToastUtils.showCustomBgToast("获取失败,错误信息:" + throwable.getMessage() + "");
}
});
}
});