关闭

安卓心跳

标签: android心跳安卓心跳安卓实现心跳安卓心跳机制
238人阅读 评论(0) 收藏 举报
private boolean runHeartFlag = true;
    private boolean currentMain = true;

    private String first = "FIRST";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initFrame();
        UmengUpdateAgent.update(this);

        setHeartBeat();
    }

    private ExecutorService mExecutorService;
    private Handler mHeartHandler = new Handler(new Handler.Callback() {

        @Override
        public boolean handleMessage(Message msg) {
            // 无权限,则弹出对话框,退出
            exitDialog();
            return true;
        }
    });

    /**
     * 设置心跳服务
     */
    private void setHeartBeat() {
        mExecutorService = Executors.newFixedThreadPool(4);
        imei = ((TelephonyManager) getSystemService(TELEPHONY_SERVICE)).getDeviceId();
        mUserName = PreferencesUtils.getInstance(this).getString("userName");
        mUserService = new UserService();

        mExecutorService.execute(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                while (runHeartFlag) {
                    if (currentMain) {
                        if (HttpTool.isNetworkConnected(mActivity)) {
                            String result = mUserService.loginStatus(mUserName, imei, first);
                            first = "NOT_FIRST";
                            if (!TextUtils.isEmpty(result) && TextUtils.equals(result, "FAIL")) {
                                // 解析,判断状态,如果是无权限,则弹出对话框,退出
                                runHeartFlag = false;
                                mHeartHandler.sendEmptyMessage(0);
                            }
                        }
                        try {
                            Thread.sleep(10000);
                        } catch (Exception e) {
                            // TODO: handle exception
                            e.printStackTrace();
                        }
                    } 
                }
            }
        });
    }

    /**
     * 弹出对话框,告诉用户当前登录已失效
     * @param packetPath
     */
    private void exitDialog() {
        final AlertDialog dialog = new AlertDialog.Builder(this).create();
        dialog.show();
        dialog.setContentView(R.layout.dialog_relogin);
        dialog.setCancelable(false);

        Button btn_got_it = (Button) dialog.findViewById(R.id.btn_got_it);
        btn_got_it.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.cancel();
                // TODO Auto-generated method stub
                mActivity.finish();
                Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }
        });
    }


    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        currentMain = true;
        if (mPacketFragment != null) {
            mPacketFragment.onActivityResume();
        }
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        currentMain = false;
        if (mPacketFragment != null) {
            mPacketFragment.onActivityPause();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        runHeartFlag = false;
    }
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:297037次
    • 积分:4298
    • 等级:
    • 排名:第7097名
    • 原创:161篇
    • 转载:146篇
    • 译文:0篇
    • 评论:30条
    文章分类
    最新评论