分类:
版权声明:本文为博主原创文章,未经博主允许不得转载。
这个函数注释:
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
这次项目中,遇到这个问题,一个activity在finish时候,还有收到广播,结果导致bug
在广播接收器中先判断一下比较保险。或者在onDestroy里面注销广播
{
@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; } }