Browse Source

增加消息提示

ChengJH 2 years ago
parent
commit
793408c03c

+ 125 - 0
app/src/main/java/com/uas/rd_equipment/activity/HomeActivity.java

@@ -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

+ 7 - 0
app/src/main/java/com/uas/rd_equipment/bean/CloseEventBusBean.java

@@ -0,0 +1,7 @@
+package com.uas.rd_equipment.bean;
+
+public class CloseEventBusBean {
+    public CloseEventBusBean(String s) {
+
+    }
+}

+ 4 - 4
app/src/main/java/com/uas/rd_equipment/fragment/IndexSettingFragment.java

@@ -38,6 +38,7 @@ import com.uas.rd_equipment.activity.FunctionActivity;
 import com.uas.rd_equipment.activity.MainActivity;
 import com.uas.rd_equipment.adapter.IpAddressAdapter;
 import com.uas.rd_equipment.application.PdaApplication;
+import com.uas.rd_equipment.bean.CloseEventBusBean;
 import com.uas.rd_equipment.bean.IPPORT;
 import com.uas.rd_equipment.bean.LoginDataBean;
 import com.uas.rd_equipment.bean.Master;
@@ -53,6 +54,7 @@ import com.uas.rd_equipment.util.FragmentUtils;
 import com.uas.rd_equipment.util.JsonTools;
 import com.uas.rd_equipment.util.LogUtil;
 
+import org.greenrobot.eventbus.EventBus;
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
@@ -398,15 +400,13 @@ public class IndexSettingFragment extends BaseFragment implements View.OnClickLi
                         .setPositiveButton(getString(R.string.confirm), new DialogInterface.OnClickListener() {
                             @Override
                             public void onClick(DialogInterface dialog, int which) {
+                                EventBus.getDefault().post(new CloseEventBusBean("关闭handler"));
                                 progressDialog.show();
                                 //传递Handler对象给Volley
                                 VolleyUtil.setVolleyHandler(mHandler);
                                 //连接服务器
                                 VolleyUtil.getVolleyUtil().requestConnectServer(getActivity(), GloableParams.ADDRESS_LOGOUT_APPLY, VolleyUtil.METHOD_GET, VolleyUtil.FRAGMENT_LOGOUT);
-                                SharedPreUtil.removeString(getContext(),"ss_name");
-                                SharedPreUtil.removeString(getContext(),"ss_code");
-                                SharedPreUtil.removeString(getContext(),"ss_brdip");
-                                SharedPreUtil.removeString(getContext(),"ss_brdport");
+                                SharedPreUtil.removeString(getContext(),"homede_code");
                                 /*PdaApplication.mRequestQueue.cancelAll(TAG + "logout");
 
                                 mStringRequest = new StringRequest(Request.Method.GET, GloableParams.ADDRESS_LOGOUT_APPLY,

+ 12 - 12
app/src/main/java/com/uas/rd_equipment/fragment/LoginFragment.java

@@ -88,18 +88,18 @@ public class LoginFragment extends BaseFragment implements AdapterView.OnItemSel
                         saveLoginCache();
                     }
 
-                    intent = new Intent(getActivity(), HomeActivity.class);
+                    intent = new Intent(mActivity, HomeActivity.class);
                     startActivity(intent);
                     //销毁MainActivity
-                    getActivity().finish();
+                    mActivity.finish();
                     break;
                 case VolleyUtil.SUCCESS_FAILED:
                     showNotice(getResources().getString(R.string.notice_login_error) + " " + tip);
-                    //   Toast.makeText(getActivity(),"登录失败:"+tip,Toast.LENGTH_SHORT).show();
+                    //   Toast.makeText(mActivity,"登录失败:"+tip,Toast.LENGTH_SHORT).show();
                     break;
                 case VolleyUtil.FAILED_FAILED:
                     showNotice(getResources().getString(R.string.notice_login_error));
-                    //  Toast.makeText(getActivity(),tip,Toast.LENGTH_SHORT).show();
+                    //  Toast.makeText(mActivity,tip,Toast.LENGTH_SHORT).show();
                     break;
             }
         }
@@ -184,11 +184,11 @@ public class LoginFragment extends BaseFragment implements AdapterView.OnItemSel
 
         //绑定Master下拉框数据
         masterFuncList = DataSourceManager.getDataSourceManager().getMasterFuncList();
-        adapter = new ArrayAdapter(getActivity(), R.layout.item_spinner, R.id.text_spinner, masterFuncList);
+        adapter = new ArrayAdapter(mActivity, R.layout.item_spinner, R.id.text_spinner, masterFuncList);
         masterSpinner.setAdapter(adapter);
 
-        maName = SharedPreUtil.getString(getActivity().getApplicationContext(), Constants.FLAG.ACCOUNT_NAME_CACHE, null);
-        maFunName = SharedPreUtil.getString(getActivity().getApplicationContext(), Constants.FLAG.ACCOUNT_FUNNAME_CACHE, null);
+        maName = SharedPreUtil.getString(mActivity.getApplicationContext(), Constants.FLAG.ACCOUNT_NAME_CACHE, null);
+        maFunName = SharedPreUtil.getString(mActivity.getApplicationContext(), Constants.FLAG.ACCOUNT_FUNNAME_CACHE, null);
         if (maFunName != null) {
             CommonUtil.setSpinnerItemSelectedByValue(masterSpinner, maFunName);
         }
@@ -236,7 +236,7 @@ public class LoginFragment extends BaseFragment implements AdapterView.OnItemSel
      */
     private void saveCache(String pUserName, String pPwd) {
         cacheInfo = DataSourceManager.getDataSourceManager().addInfoItem(pUserName, pPwd);
-        SharedPreUtil.saveString(getActivity(), keyUri, cacheInfo);
+        SharedPreUtil.saveString(mActivity, keyUri, cacheInfo);
     }
 
     /**
@@ -244,7 +244,7 @@ public class LoginFragment extends BaseFragment implements AdapterView.OnItemSel
      */
     private void loadCache() {
         //通过ipport获得登录名缓存
-        cacheInfo = SharedPreUtil.getString(getActivity(), keyUri, null);
+        cacheInfo = SharedPreUtil.getString(mActivity, keyUri, null);
         if (cacheInfo != null) {
             DataSourceManager.getDataSourceManager().setInfoList(cacheInfo);
             List<Map<String, String>> tmpList = DataSourceManager.getDataSourceManager().getInfoList();
@@ -339,7 +339,7 @@ public class LoginFragment extends BaseFragment implements AdapterView.OnItemSel
         //传递Handler
         VolleyUtil.setVolleyHandler(loginHandler);
         //Volley请求服务器
-        VolleyUtil.requestUserLogin(getActivity(), GloableParams.ADDRESS_LOGIN_APPLY, VolleyUtil.METHOD_POST,
+        VolleyUtil.requestUserLogin(mActivity, GloableParams.ADDRESS_LOGIN_APPLY, VolleyUtil.METHOD_POST,
                 pageType, maName, userName, password);
         SharedPreUtil.saveString(getContext(), Constants.FLAG.LOGINPASSOWRD, password);
 
@@ -352,8 +352,8 @@ public class LoginFragment extends BaseFragment implements AdapterView.OnItemSel
      */
     private void showNotice(String noticeMsg) {
         noticeTextView.setText(noticeMsg);
-        Animation noticeIn = AnimationUtils.loadAnimation(getActivity(), R.anim.notice_in);
-        final Animation noticeOut = AnimationUtils.loadAnimation(getActivity(), R.anim.notice_out);
+        Animation noticeIn = AnimationUtils.loadAnimation(mActivity, R.anim.notice_in);
+        final Animation noticeOut = AnimationUtils.loadAnimation(mActivity, R.anim.notice_out);
         //控件保存在动画结束的状态
         noticeIn.setFillAfter(true);
         noticeOut.setFillAfter(true);

+ 2 - 54
app/src/main/java/com/uas/rd_equipment/fragment/MaintenanceManagementListFragment.java

@@ -1,19 +1,11 @@
 package com.uas.rd_equipment.fragment;
 
 import android.app.Activity;
-import android.app.Notification;
-import android.app.NotificationChannel;
-import android.app.NotificationManager;
-import android.app.PendingIntent;
-import android.content.Context;
 import android.content.Intent;
-import android.graphics.BitmapFactory;
 import android.graphics.Color;
 import android.graphics.drawable.BitmapDrawable;
-import android.os.Build;
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
-import android.support.v4.app.NotificationCompat;
 import android.support.v4.content.ContextCompat;
 import android.view.KeyEvent;
 import android.view.View;
@@ -64,9 +56,6 @@ import org.greenrobot.eventbus.Subscribe;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
-import java.util.Random;
-
-import static android.content.Context.NOTIFICATION_SERVICE;
 
 /**
  * Created by cjh on 2022-10-15
@@ -103,53 +92,12 @@ public class MaintenanceManagementListFragment extends BaseFragment implements O
         mSmartTable = mActivity.findViewById(R.id.jlt_storage_in_filter_list_st);
         mScanImageView = (ImageView) mActivity.findViewById(R.id.storage_recharge_scan_iv);
         mMenuButton = (Button) mActivity.findViewById(R.id.btn_actionbar_more);
-        Button tishi = mActivity.findViewById(R.id.tishi);
-        tishi.setOnClickListener(new View.OnClickListener() {
-            @Override
-            public void onClick(View v) {
-                showNotification(mActivity, "通知内容:关注公众号,编程资料与技术共享学习");
-            }
-        });
 
     }
-    public void showNotification(Context context, String content) {
-        //1.创建通知管理器
-        NotificationManager notificationManager = (NotificationManager) mActivity.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(mActivity, FunctionActivity.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);
-    }
+
     @Override
     protected void initEvents() {
+
         storage_recharge_collect_et.requestFocus();
         storage_recharge_collect_et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
             @Override

+ 3 - 0
app/src/main/java/com/uas/rd_equipment/global/GloableParams.java

@@ -335,6 +335,7 @@ public class GloableParams {
     public static String ASTATIONTHE_SELECT_GETMAKERECORD;
     public static String ASTATIONTHE_SELECT_DELETEMAKERECORD;
     public static String ASTATIONTHE_SELECT_SAVEMAKERECORD;
+    public static String ASTATIONTHE_SELECT_DEVICEINFOPUSH;
 
     /**
      * 新版设备管理
@@ -367,6 +368,7 @@ public class GloableParams {
     public static String ADDRESSTAIL_GET_GETMAKERECORD="/oa/device/getMakerecord.action";
     public static String ADDRESSTAIL_GET_DELETEMAKERECORD="/oa/device/deleteMakerecord.action";
     public static String ADDRESSTAIL_GET_SAVEMAKERECORD="/oa/device/saveMakerecord.action";
+    public static String ADDRESSTAIL_GET_DEVICEINFOPUSH="/oa/device/deviceInfoPush.action";
 
     /**
      * 高登
@@ -1508,5 +1510,6 @@ public class GloableParams {
         GloableParams.ASTATIONTHE_SELECT_GETMAKERECORD = uriHead + GloableParams.ADDRESSTAIL_GET_GETMAKERECORD;
         GloableParams.ASTATIONTHE_SELECT_DELETEMAKERECORD = uriHead + GloableParams.ADDRESSTAIL_GET_DELETEMAKERECORD;
         GloableParams.ASTATIONTHE_SELECT_SAVEMAKERECORD = uriHead + GloableParams.ADDRESSTAIL_GET_SAVEMAKERECORD;
+        GloableParams.ASTATIONTHE_SELECT_DEVICEINFOPUSH = uriHead + GloableParams.ADDRESSTAIL_GET_DEVICEINFOPUSH;
     }
 }

+ 1 - 6
app/src/main/res/layout/maintenance_managementlsit_fragment.xml

@@ -30,12 +30,7 @@
             android:clickable="false"
             android:src="@drawable/ic_edittext_scan" />
     </LinearLayout>
-    <Button
-        android:id="@+id/tishi"
-        android:text="提示"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content">
-    </Button>
+
     <com.scwang.smartrefresh.layout.SmartRefreshLayout
         android:id="@+id/pda_wms_in_filter_list_srl"
         android:layout_width="match_parent"