init
This commit is contained in:
379
app/src/main/java/com/info/sn/MainActivity.java
Normal file
379
app/src/main/java/com/info/sn/MainActivity.java
Normal file
@@ -0,0 +1,379 @@
|
||||
package com.info.sn;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Bitmap;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.text.TextUtils;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.hjq.permissions.OnPermission;
|
||||
import com.hjq.permissions.Permission;
|
||||
import com.hjq.permissions.XXPermissions;
|
||||
import com.info.sn.bean.UserInfo;
|
||||
import com.info.sn.jpush.ExampleUtil;
|
||||
import com.info.sn.jpush.LocalBroadcastManager;
|
||||
import com.info.sn.jpush.TagAliasOperatorHelper;
|
||||
import com.info.sn.network.api.HTTPInterface;
|
||||
import com.info.sn.service.MyDownloadService;
|
||||
import com.info.sn.utils.LogUtils;
|
||||
import com.info.sn.utils.SPUtils;
|
||||
import com.info.sn.utils.ToastUtil;
|
||||
import com.info.sn.utils.Utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import cn.jpush.android.api.JPushInterface;
|
||||
|
||||
import static com.info.sn.jpush.TagAliasOperatorHelper.ACTION_ADD;
|
||||
import static com.info.sn.jpush.TagAliasOperatorHelper.ACTION_CHECK;
|
||||
import static com.info.sn.jpush.TagAliasOperatorHelper.ACTION_CLEAN;
|
||||
import static com.info.sn.jpush.TagAliasOperatorHelper.ACTION_DELETE;
|
||||
import static com.info.sn.jpush.TagAliasOperatorHelper.ACTION_GET;
|
||||
import static com.info.sn.jpush.TagAliasOperatorHelper.ACTION_SET;
|
||||
import static com.info.sn.jpush.TagAliasOperatorHelper.TagAliasBean;
|
||||
import static com.info.sn.jpush.TagAliasOperatorHelper.sequence;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
public static boolean isForeground = false;
|
||||
private ImageView imageView;
|
||||
private TextView tv_note, tv_devsn, tv_username, tv_school, tv_grade, tv_version;
|
||||
private int DeviceInfo;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
|
||||
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
|
||||
}
|
||||
PackageManager pm = getPackageManager();
|
||||
//后台为0可能传过来null
|
||||
pm.setApplicationEnabledSetting("com.info.sn", PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 0);
|
||||
requestPermission();
|
||||
registerMessageReceiver(); // used for receive msg
|
||||
String rid = JPushInterface.getRegistrationID(getApplicationContext());
|
||||
if (!rid.isEmpty()) {
|
||||
ToastUtil.debugShow("RegId:" + rid);
|
||||
LogUtils.e("RegId", rid);
|
||||
onTagAliasAction(7);
|
||||
} else {
|
||||
// ToastUtil.show("Get registration fail, JPush init failed!");
|
||||
// Toast.makeText(this, "Get registration fail, JPush init failed!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
initView();
|
||||
initData();
|
||||
HTTPInterface.checkDevicesInfo(handler);
|
||||
startService(new Intent(MainActivity.this, MyDownloadService.class));
|
||||
|
||||
}
|
||||
|
||||
private void initView() {
|
||||
tv_note = (TextView) findViewById(R.id.tv_note);
|
||||
imageView = (ImageView) findViewById(R.id.imageView);
|
||||
tv_devsn = (TextView) findViewById(R.id.tv_devsn);
|
||||
tv_devsn.setText("设备SN:" + Utils.getSerial());
|
||||
tv_username = (TextView) findViewById(R.id.tv_username);
|
||||
tv_school = (TextView) findViewById(R.id.tv_school);
|
||||
tv_grade = (TextView) findViewById(R.id.tv_grade);
|
||||
tv_version = findViewById(R.id.version);
|
||||
tv_version.setText("版本:"+ BuildConfig.VERSION_NAME);
|
||||
}
|
||||
|
||||
private void initData() {
|
||||
DeviceInfo = (int) SPUtils.get(this, "isLogined", 2);
|
||||
switch (DeviceInfo) {
|
||||
case 0:
|
||||
setImageAndText(imageView, "设备未绑定");
|
||||
break;
|
||||
case 1:
|
||||
setImageAndText(imageView, "设备已绑定");
|
||||
break;
|
||||
case 2:
|
||||
setImageAndText(imageView, "未经验证的设备,请联系客服");
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void setImageAndText(ImageView imageView, String text) {
|
||||
Bitmap bitmap = Utils.createQRImage(Utils.getSn(), 250, 250);
|
||||
imageView.setImageBitmap(bitmap);
|
||||
tv_note.setText(text);
|
||||
}
|
||||
|
||||
private void setUserInfo(UserInfo userInfo) {
|
||||
String name = userInfo.getSn_name();
|
||||
String school = userInfo.getSn_school();
|
||||
String grade = userInfo.getSn_grade();
|
||||
if (name == null || name.equals("")) {
|
||||
tv_username.setText("用户姓名:未设置");
|
||||
} else {
|
||||
tv_username.setText("用户姓名:" + name);
|
||||
}
|
||||
if (school == null || school.equals("")) {
|
||||
tv_school.setText("学校:未设置");
|
||||
} else {
|
||||
tv_school.setText("学校:" + school);
|
||||
}
|
||||
if (grade == null || grade.equals("")) {
|
||||
tv_grade.setText("年级:未设置");
|
||||
} else {
|
||||
tv_grade.setText("年级:" + getGrade(grade));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private String getGrade(String grade) {
|
||||
String s;
|
||||
switch (grade) {
|
||||
case "1":
|
||||
s = "一年级";
|
||||
break;
|
||||
case "2":
|
||||
s = "二年级";
|
||||
break;
|
||||
case "3":
|
||||
s = "三年级";
|
||||
break;
|
||||
case "4":
|
||||
s = "四年级";
|
||||
break;
|
||||
case "5":
|
||||
s = "五年级";
|
||||
break;
|
||||
case "6":
|
||||
s = "六年级";
|
||||
break;
|
||||
case "7":
|
||||
s = "初一";
|
||||
break;
|
||||
case "8":
|
||||
s = "初二";
|
||||
break;
|
||||
case "9":
|
||||
s = "初三";
|
||||
break;
|
||||
case "10":
|
||||
s = "高一";
|
||||
break;
|
||||
case "11":
|
||||
s = "高二";
|
||||
break;
|
||||
case "12":
|
||||
s = "高三";
|
||||
break;
|
||||
default:
|
||||
s = "一年级";
|
||||
break;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
Handler handler = new Handler() {
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
super.handleMessage(msg);
|
||||
switch (msg.what) {
|
||||
case 0:
|
||||
SPUtils.put(MainActivity.this, "isLogined", 0);
|
||||
setImageAndText(imageView, "设备未绑定");
|
||||
break;
|
||||
case 1:
|
||||
UserInfo userInfo = (UserInfo) msg.obj;
|
||||
SPUtils.put(MainActivity.this, "isLogined", 1);
|
||||
SPUtils.put(MainActivity.this, "member_id", userInfo.getMember_id());
|
||||
SPUtils.put(MainActivity.this, "sn_id", userInfo.getId());
|
||||
setUserInfo((UserInfo) msg.obj);
|
||||
setImageAndText(imageView, "设备已绑定");
|
||||
break;
|
||||
case 2:
|
||||
SPUtils.put(MainActivity.this, "isLogined", 2);
|
||||
setImageAndText(imageView, "未经验证的设备,请联系客服");
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private String[] permission = new String[]{
|
||||
// Permission.SYSTEM_ALERT_WINDOW,
|
||||
// Permission.CAMERA,
|
||||
// Permission.READ_SMS,
|
||||
// Permission.RECEIVE_SMS,
|
||||
// Permission.SEND_SMS,
|
||||
Permission.REQUEST_INSTALL_PACKAGES,
|
||||
Permission.READ_EXTERNAL_STORAGE,
|
||||
Permission.WRITE_EXTERNAL_STORAGE,
|
||||
// Permission.READ_PHONE_STATE
|
||||
};
|
||||
|
||||
public void requestPermission() {
|
||||
XXPermissions.with(this)
|
||||
// 可设置被拒绝后继续申请,直到用户授权或者永久拒绝
|
||||
.constantRequest()
|
||||
// 支持请求6.0悬浮窗权限8.0请求安装权限
|
||||
//.permission(Permission.REQUEST_INSTALL_PACKAGES)
|
||||
// 不指定权限则自动获取清单中的危险权限
|
||||
.permission(permission)
|
||||
.request(new OnPermission() {
|
||||
|
||||
@Override
|
||||
public void hasPermission(List<String> granted, boolean isAll) {
|
||||
if (isAll) {
|
||||
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "POStemp";
|
||||
File file = new File(path);
|
||||
file.mkdirs();
|
||||
} else {
|
||||
ToastUtil.show("需要授予所有权限才能正常使用本程序!");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void noPermission(List<String> denied, boolean quick) {
|
||||
if (quick) {
|
||||
ToastUtil.show("被永久拒绝授权,请手动授予权限!");
|
||||
//如果是被永久拒绝就跳转到应用权限系统设置页面
|
||||
XXPermissions.gotoPermissionSettings(MainActivity.this);
|
||||
} else {
|
||||
ToastUtil.show("获取权限失败");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public void onTagAliasAction(int i) {
|
||||
Set<String> tags = null;
|
||||
String alias = null;
|
||||
int action = -1;
|
||||
boolean isAliasAction = false;
|
||||
switch (i) {
|
||||
//设置手机号码:
|
||||
case 0:
|
||||
// handleSetMobileNumber();
|
||||
return;
|
||||
//增加tag
|
||||
case 1:
|
||||
// tags = getInPutTags();
|
||||
if (tags == null) {
|
||||
return;
|
||||
}
|
||||
action = ACTION_ADD;
|
||||
break;
|
||||
//设置tag
|
||||
case 2:
|
||||
// tags = getInPutTags();
|
||||
if (tags == null) {
|
||||
return;
|
||||
}
|
||||
action = ACTION_SET;
|
||||
break;
|
||||
//删除tag
|
||||
case 3:
|
||||
// tags = getInPutTags();
|
||||
if (tags == null) {
|
||||
return;
|
||||
}
|
||||
action = ACTION_DELETE;
|
||||
break;
|
||||
//获取所有tag
|
||||
case 4:
|
||||
action = ACTION_GET;
|
||||
break;
|
||||
//清除所有tag
|
||||
case 5:
|
||||
action = ACTION_CLEAN;
|
||||
break;
|
||||
case 6:
|
||||
// tags = getInPutTags();
|
||||
if (tags == null) {
|
||||
return;
|
||||
}
|
||||
action = ACTION_CHECK;
|
||||
break;
|
||||
//设置alias
|
||||
case 7:
|
||||
// alias = getInPutAlias();
|
||||
alias = Utils.getSerial();
|
||||
if (TextUtils.isEmpty(alias)) {
|
||||
return;
|
||||
}
|
||||
isAliasAction = true;
|
||||
action = ACTION_SET;
|
||||
break;
|
||||
//获取alias
|
||||
case 8:
|
||||
isAliasAction = true;
|
||||
action = ACTION_GET;
|
||||
break;
|
||||
//删除alias
|
||||
case 9:
|
||||
isAliasAction = true;
|
||||
action = ACTION_DELETE;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
TagAliasBean tagAliasBean = new TagAliasBean();
|
||||
tagAliasBean.action = action;
|
||||
sequence++;
|
||||
if (isAliasAction) {
|
||||
tagAliasBean.alias = alias;
|
||||
} else {
|
||||
tagAliasBean.tags = tags;
|
||||
}
|
||||
tagAliasBean.isAliasAction = isAliasAction;
|
||||
TagAliasOperatorHelper.getInstance().handleAction(getApplicationContext(), sequence, tagAliasBean);
|
||||
|
||||
}
|
||||
|
||||
|
||||
//for receive customer msg from jpush server
|
||||
private MessageReceiver mMessageReceiver;
|
||||
public static final String MESSAGE_RECEIVED_ACTION = "com.example.jpushdemo.MESSAGE_RECEIVED_ACTION";
|
||||
public static final String KEY_TITLE = "title";
|
||||
public static final String KEY_MESSAGE = "message";
|
||||
public static final String KEY_EXTRAS = "extras";
|
||||
|
||||
public void registerMessageReceiver() {
|
||||
mMessageReceiver = new MessageReceiver();
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
|
||||
filter.addAction(MESSAGE_RECEIVED_ACTION);
|
||||
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, filter);
|
||||
}
|
||||
|
||||
public class MessageReceiver extends BroadcastReceiver {
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
try {
|
||||
if (MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {
|
||||
String messge = intent.getStringExtra(KEY_MESSAGE);
|
||||
String extras = intent.getStringExtra(KEY_EXTRAS);
|
||||
StringBuilder showMsg = new StringBuilder();
|
||||
showMsg.append(KEY_MESSAGE + " : " + messge + "\n");
|
||||
if (!ExampleUtil.isEmpty(extras)) {
|
||||
showMsg.append(KEY_EXTRAS + " : " + extras + "\n");
|
||||
}
|
||||
// setCostomMsg(showMsg.toString());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user