|
|
@@ -1,10 +1,18 @@
|
|
|
package com.uas.rd_equipment.activity;
|
|
|
|
|
|
+import android.app.Notification;
|
|
|
+import android.app.NotificationChannel;
|
|
|
+import android.app.NotificationManager;
|
|
|
+import android.content.Context;
|
|
|
import android.content.Intent;
|
|
|
import android.content.pm.PackageManager;
|
|
|
+import android.graphics.BitmapFactory;
|
|
|
import android.graphics.drawable.BitmapDrawable;
|
|
|
+import android.os.Build;
|
|
|
import android.os.Bundle;
|
|
|
+import android.os.Handler;
|
|
|
import android.support.annotation.NonNull;
|
|
|
+import android.support.v4.app.NotificationCompat;
|
|
|
import android.view.Gravity;
|
|
|
import android.view.MotionEvent;
|
|
|
import android.view.View;
|
|
|
@@ -15,17 +23,31 @@ import android.widget.PopupWindow;
|
|
|
import android.widget.SimpleAdapter;
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.android.volley.Request;
|
|
|
import com.uas.rd_equipment.R;
|
|
|
import com.uas.rd_equipment.application.PdaApplication;
|
|
|
+import com.uas.rd_equipment.bean.CloseEventBusBean;
|
|
|
import com.uas.rd_equipment.fragment.BaseFragment;
|
|
|
import com.uas.rd_equipment.global.GloableParams;
|
|
|
import com.uas.rd_equipment.tools.DataSourceManager;
|
|
|
+import com.uas.rd_equipment.tools.SharedPreUtil;
|
|
|
import com.uas.rd_equipment.tools.VolleyUtil;
|
|
|
import com.uas.rd_equipment.util.CommonUtil;
|
|
|
+import com.uas.rd_equipment.util.FastjsonUtil;
|
|
|
+import com.uas.rd_equipment.util.HttpCallback;
|
|
|
+import com.uas.rd_equipment.util.HttpParams;
|
|
|
import com.uas.rd_equipment.util.PermissionUtil;
|
|
|
+import com.uas.rd_equipment.util.StringUtil;
|
|
|
+import com.uas.rd_equipment.util.VolleyRequest;
|
|
|
+
|
|
|
+import org.greenrobot.eventbus.EventBus;
|
|
|
+import org.greenrobot.eventbus.Subscribe;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
+import java.util.Random;
|
|
|
|
|
|
public class HomeActivity extends BaseActivity implements AdapterView.OnItemClickListener {
|
|
|
private ArrayList<HashMap<String, Object>> gridItemList;
|
|
|
@@ -35,12 +57,108 @@ public class HomeActivity extends BaseActivity implements AdapterView.OnItemClic
|
|
|
private PopupWindow mExitPopupWindow;
|
|
|
private TextView mCancelTextView, mMinimizeTextView, mExitTextView;
|
|
|
public BaseFragment fragment;
|
|
|
+ private String homede_code;
|
|
|
+
|
|
|
@Override
|
|
|
protected void onDestroy() {
|
|
|
//清除全部App缓存
|
|
|
// SharedPreUtil.removeAll(getApplicationContext());
|
|
|
VolleyUtil.distoryVolley();
|
|
|
super.onDestroy();
|
|
|
+ EventBus.getDefault().unregister(this);
|
|
|
+ handler.removeCallbacks(runnable);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Subscribe
|
|
|
+ public void onMessageEvent(CloseEventBusBean evensst) {
|
|
|
+ handler.removeCallbacks(runnable);
|
|
|
+ }
|
|
|
+ Handler handler=new Handler();
|
|
|
+
|
|
|
+ Runnable runnable=new Runnable() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+// showNotification(HomeActivity.this, "通知内容:关注公众号,编程资料与技术共享学习");
|
|
|
+ getDatalistCodes();
|
|
|
+ handler.postDelayed(this, 2000);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ };
|
|
|
+
|
|
|
+ public void showNotification(Context context, String content) {
|
|
|
+ //1.创建通知管理器
|
|
|
+ NotificationManager notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
|
|
|
+ NotificationCompat.Builder builder;
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//Android 8.0版本适配
|
|
|
+ NotificationChannel channel = new NotificationChannel("default", "default", NotificationManager.IMPORTANCE_HIGH);
|
|
|
+ notificationManager.createNotificationChannel(channel);
|
|
|
+ builder = new NotificationCompat.Builder(context, "default");
|
|
|
+ } else {
|
|
|
+ builder = new NotificationCompat.Builder(context);
|
|
|
+ }
|
|
|
+ //用来进行跳转并传递消息的intent
|
|
|
+// Intent intent = new Intent(this, MainActivity.class);
|
|
|
+// intent.putExtra("content",content);
|
|
|
+ //2.创建通知实例
|
|
|
+ Notification notification = builder
|
|
|
+ .setContentTitle("通知信息")
|
|
|
+ .setContentText(content)
|
|
|
+ .setWhen(System.currentTimeMillis())
|
|
|
+ //smallIcon 通知栏显示小图标
|
|
|
+ //android5.0 之后通知栏图标都修改了,小图标不能含有RGB图层,也就是说图片不能带颜色,否则显示的就成白色方格了
|
|
|
+ //解决方法一:为图片带颜色,targetSdkVersion改为21以下
|
|
|
+ //解决方法二:只能用白色透明底的图片
|
|
|
+ .setSmallIcon(R.mipmap.icon)
|
|
|
+ //LargeIcon 下拉后显示的图标
|
|
|
+ .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.icon))
|
|
|
+ //收到通知时的效果,这里是默认声音
|
|
|
+ .setDefaults(Notification.DEFAULT_SOUND)
|
|
|
+ .setAutoCancel(true)
|
|
|
+// .setContentIntent(PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT))
|
|
|
+ .build();
|
|
|
+ //3.notify
|
|
|
+ //notifyId每次要不一致,不然下一次的通知会覆盖上一次
|
|
|
+ int notifyId = new Random().nextInt();
|
|
|
+ notificationManager.notify(notifyId, notification);
|
|
|
+ }
|
|
|
+ private void getDatalistCodes(){
|
|
|
+ homede_code = SharedPreUtil.getString(HomeActivity.this, "homede_code", null);
|
|
|
+ VolleyRequest.getInstance().stringRequest(new HttpParams.Builder()
|
|
|
+ .url(GloableParams.ASTATIONTHE_SELECT_DEVICEINFOPUSH)
|
|
|
+ .method(Request.Method.GET)
|
|
|
+ .tag("TAG" + "getindatalist")
|
|
|
+ .flag(0)
|
|
|
+ .build(), new HttpCallback() {
|
|
|
+ @Override
|
|
|
+ public void onSuccess(int flag, Object o) throws Exception {
|
|
|
+ JSONArray dataArray = FastjsonUtil.getJSONArray(o.toString(), "InfoPush");
|
|
|
+ if (dataArray == null|| dataArray.size() == 0){
|
|
|
+ }else {
|
|
|
+ for(Object index:dataArray){
|
|
|
+ JSONObject data = (JSONObject) index;
|
|
|
+ String de_code = data.getString("DE_CODE");
|
|
|
+ if (!StringUtil.isEmpty(de_code)){
|
|
|
+ if (!de_code.equals(homede_code)){
|
|
|
+ SharedPreUtil.saveString(HomeActivity.this,"homede_code", de_code);
|
|
|
+ showNotification(HomeActivity.this, de_code+"");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFail(int flag, String failStr) throws Exception {
|
|
|
+
|
|
|
+ CommonUtil.toastNoRepeat(HomeActivity.this, failStr);
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -76,6 +194,13 @@ public class HomeActivity extends BaseActivity implements AdapterView.OnItemClic
|
|
|
closeExitPopupWindow();
|
|
|
}
|
|
|
});
|
|
|
+
|
|
|
+ if (!EventBus.getDefault().isRegistered(this)) {
|
|
|
+ EventBus.getDefault().register(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ handler.postDelayed(runnable, 2000);
|
|
|
+
|
|
|
}
|
|
|
|
|
|
@Override
|