android
android studio连接手机
1. 启用开发者模式
2. 启用usb调试

2. 安卓语音播报:Text to Speech
3. 小米推送
a. 注册成为企业开发者
b. 在页面提供包名,com.*.*.*
c. 从页面获取app_id, app_key以及密码
d. 下载android包,并按上面连接配置
e. 下载java端代码,并配置
private static final String APP_SECRET_KEY = "*****";
private static final String MY_PACKAGE_NAME = "com.*.*.*";
private static Sender sender = new Sender(APP_SECRET_KEY);
private static String DEFAULT_TOPIC = "TOPIC";
public static String messagePayload = null;
public MiPush() {
super();
}
public static void sendBroadcast(String title, String description, String topic) throws Exception {
Constants.useOfficial();
if(topic == null) topic = DEFAULT_TOPIC;
Message message = new Message.Builder()
.title(title)
.description(description).payload(messagePayload)
.restrictedPackageName(MY_PACKAGE_NAME)
.notifyType(1)
.build();
Result result = sender.broadcast(message, topic, 3);
ErrorCode errorCode = result.getErrorCode();
System.out.println("[Mi Push][errorCode=" + errorCode.getName() + "," + errorCode.getValue() + "][reason=" + result.getReason() + "]");
}
f. 注册topic,安卓端:
MiPushClient.subscribe(getApplicationContext(), MI_TOPIC, null);
4. 跳转activity
Intent intent = new Intent(context, ***Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);
FLAG_ACTIVITY_NEW_TASK :开始新的activity
FLAG_ACTIVITY_CLEAR_TASK:清除历史activity
5. 跳转login,然后再回来:
a. 当前activity:
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra(***Utils.GOTO_TARGET_FLAG, "true");
startActivityForResult(intent, 0);
b. login Activity:
String gotoTargetFlag = getIntent().getStringExtra(***Utils.GOTO_TARGET_FLAG);
if(gotoTargetFlag != null) {
***AppProperties.getInstance().setUserbean(userBean);
finish();//关闭当前activity
}
c. 当前activity:startActivityForResult的回调方法:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
...
} else {
showDiag("您没有足够的权限!");
}
}
d. 控制返回:
@Override
public void onBackPressed() {
if(***Utils.isLogin() && isTaskRoot()) {
startActivity(new Intent(getApplicationContext(), ***Activity.class));
} else {
super.onBackPressed();
}
}
6. 等待加载
ProgressDialog progressDialog = new ProgressDialog(***Activity.this);
progressDialog.setIndeterminate(false);//循环滚动
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage(msg);
progressDialog.setCancelable(false);//false不能取消显示,true可以取消显示
progressDialog.show()/progressDialog.dimiss()
7. android Fragment
静态fragment
<fragment
android:name="com.littlejie.demo.modules.base.fragment.create.TestFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
动态Fragment
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
注意红色部分区别
8. android adapter
9. android 图片缩放组件:
有一个例子可以参考:
图片居中全屏
<com.github.chrisbanes.photoview.PhotoView android:id="@+id/photo_view" android:layout_width="match_parent" android:layout_height="match_parent" android:adjustViewBounds="true" android:layout_gravity="center_vertical"/>
10. 另一个程序正在使用此文件,进程无法访问
11. 设置状态栏颜色、透明
implementation 'com.blankj:utilcodex:1.31.0'
代码加入:
BarUtils.transparentStatusBar(activity);
BarUtils.setStatusBarLightMode(activity, true);

12. tools:text [坑]
tools命名空间比较特别,该命名空间可以覆盖组件的任何属性,以便在ide预览中进行不同的展示,
它只有在编辑预览的时候有效,运行的时候是根本就不会打入apk中
13. android读写文件
FileInputStream fis = context.openFileInput(filename)
传入private.txt文件名,openFileInput最终打开的文件路径:
/data/data/<java文件的包名>/files
如果使用FileInputStream,则要传入全路径:/data/data/<java文件的包名>/files/private.txt