手表开发的isFinishing

 

 

boolean android.app.Activity.isFinishing()函数使用

标签: actionstring
  11487人阅读  评论(0)  收藏  举报
  分类:
 

这个函数注释:

Check to see whether this activity is in the process of finishing, either because you called finish on it or someone else has requested that it finished. This is often used in onPause to determine whether the activity is simply pausing or completely finishing.

Returns:
If the activity is finishing, returns true; else returns false.
See Also:
finish
判断this Activity是否finish,自己调用或者某种因素请求finish,经常用在onPause函数里面


这次项目中,遇到这个问题,一个activity在finish时候,还有收到广播,结果导致bug

在广播接收器中先判断一下比较保险。或者在onDestroy里面注销广播

private BroadcastReceiver mIntentReceiver = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            Log.d(TAG, intent.toString());    
            final String action = intent.getAction();
            
            if(isFinishing())
            {

                  。。。。。。。              

                return;

            }

}


还有遇到一个问题,在activity  finish时候,这时候handler.sendMessage,这时候也会导致bug

这时候最好在finish函数里面removeAllCallbackAndMessage(this)



package com.netease.mobimail.activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.Builder;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.wearable.Node;
import com.google.android.gms.wearable.NodeApi;
import com.google.android.gms.wearable.NodeApi.GetConnectedNodesResult;
import com.google.android.gms.wearable.NodeApi.NodeListener;
import com.google.android.gms.wearable.Wearable;
import com.netease.mobimail.WearMailApplication;
import com.netease.mobimail.command.BaseCommand;
import com.netease.mobimail.storage.ConfigSharedManager;
import java.util.Iterator;
import java.util.List;
import ows.phoneAndWear.storage.entity.DuwearMail;

public abstract class BaseActivity
        extends Activity
        implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener
{
    public String TAG = "";
    public int mHeight;
    NodeApi.NodeListener mNodeListener = new NodeApi.NodeListener()
    {
        public void onPeerConnected(Node paramAnonymousNode)
        {
            ConfigSharedManager.getConfigSharedManager(BaseActivity.this).upDatePhoneConnectState(true);
            BaseActivity.this.onPeerConnectSuccess();
        }

        public void onPeerDisconnected(Node paramAnonymousNode)
        {
            if (BaseActivity.this.getTaskId() == -1) {
                return;
            }
            ConfigSharedManager.getConfigSharedManager(BaseActivity.this).upDatePhoneConnectState(false);
            BaseActivity.this.onPeerDisconnectFail();
        }
    };
    private GoogleApiClient mOwsApiClient;
    public int mWitdh;

    private void initOwsApiClient()
    {
        this.mOwsApiClient = new GoogleApiClient.Builder(this).addApi(Wearable.API).addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
    }

    protected void detectPeerNode()
    {
        Log.i(this.TAG, "start detect peer phone");
        Wearable.NodeApi.getConnectedNodes(this.mOwsApiClient).setResultCallback(new ResultCallback()
        {
            public void onResult(NodeApi.GetConnectedNodesResult paramAnonymousGetConnectedNodesResult)
            {
                if (paramAnonymousGetConnectedNodesResult.getNodes().size() == 0)
                {
                    Log.i(BaseActivity.this.TAG, "no peer phone");
                    BaseActivity.this.onNoPeerConnect();
                    ConfigSharedManager.getConfigSharedManager(BaseActivity.this).upDatePhoneConnectState(false);
                }
                for (;;)
                {
                    return;
                    Iterator localIterator = paramAnonymousGetConnectedNodesResult.getNodes().iterator();
                    while (localIterator.hasNext())
                    {
                        Node localNode = (Node)localIterator.next();
                        BaseActivity.this.printNodeInfo(localNode);
                        ConfigSharedManager.getConfigSharedManager(BaseActivity.this).upDatePhoneConnectState(true);
                    }
                }
            }
        });
    }

    protected abstract void doAfterClientConnectFail();

    protected abstract void doAfterClientConnectSuccess();

    public GoogleApiClient getAvailableOwsApiClient()
    {
        if ((this.mOwsApiClient == null) || (!this.mOwsApiClient.isConnected())) {
            return null;
        }
        return this.mOwsApiClient;
    }

    public boolean isNeedConnectCallback()
    {
        return (WearMailApplication.getCurrentActivity() != null) && (WearMailApplication.getCurrentActivity() == this);
    }

    public void onConnected(Bundle paramBundle)
    {
        ConfigSharedManager.getConfigSharedManager(this).updateApiClientAvailable(true);
        Wearable.NodeApi.addListener(this.mOwsApiClient, this.mNodeListener);
        detectPeerNode();
        BaseCommand.reStartAllRequest();
        doAfterClientConnectSuccess();
    }

    public void onConnectionFailed(ConnectionResult paramConnectionResult)
    {
        Log.i(this.TAG, "onConnectionFailed");
        ConfigSharedManager.getConfigSharedManager(this).updateApiClientAvailable(false);
        doAfterClientConnectFail();
        BaseCommand.cancalAllCommands();
    }

    public void onConnectionSuspended(int paramInt)
    {
        Log.i(this.TAG, "onConnectionSuspended");
    }

    protected void onCreate(Bundle paramBundle)
    {
        super.onCreate(paramBundle);
        this.TAG = getClass().getSimpleName();
        WearMailApplication.addActivity(this);
        WearMailApplication.setCurrentActivity(this);
        initOwsApiClient();
        if (!isFinishing())
        {
            this.mHeight = getWindowManager().getDefaultDisplay().getHeight();
            this.mWitdh = getWindowManager().getDefaultDisplay().getWidth();
        }
    }

    public abstract void onGetMessage(int paramInt, DuwearMail paramDuwearMail);

    protected abstract void onNoPeerConnect();

    protected abstract void onPeerConnectSuccess();

    protected abstract void onPeerDisconnectFail();

    public void onStart()
    {
        super.onStart();
        this.mOwsApiClient.connect();
    }

    public void onStop()
    {
        super.onStop();
        BaseCommand.pauseAllRequest();
        if ((this.mOwsApiClient != null) && (this.mOwsApiClient.isConnected()))
        {
            Wearable.NodeApi.removeListener(this.mOwsApiClient, this.mNodeListener);
            this.mOwsApiClient.disconnect();
        }
    }

    public void printNodeInfo(Node paramNode) {}

    public GoogleApiClient requestOwsApiClient()
    {
        if (this.mOwsApiClient == null) {
            this.mOwsApiClient = new GoogleApiClient.Builder(WearMailApplication.getCurrentActivity()).addApi(Wearable.API).addConnectionCallbacks(WearMailApplication.getCurrentActivity()).addOnConnectionFailedListener(WearMailApplication.getCurrentActivity()).build();
        }
        this.mOwsApiClient.blockingConnect();
        if (!this.mOwsApiClient.isConnected()) {
            return null;
        }
        return this.mOwsApiClient;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值