Android兼容之摄像头权限关闭后应用生命周期和单例数据变化-创新互联

摄像头权限关闭后异常

描述:当app摄像头权限打开后,运行中的app和单例中的数据正常;切换到设置页面关闭摄像头权限后;再切换到app发生的变化
结果: activity会重新绘制,activity历史栈会保留,但是回退到历史栈的activity会重新从onCreate(***)开始;且单例中的数据被重置,例如:int 数据被重置为0,对象被重置为null

创新互联网站建设公司,提供网站制作、成都网站建设,网页设计,建网站,PHP网站建设等专业做网站服务;可快速的进行网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,是专业的做网站团队,希望更多企业前来合作!示例:

单例代码

public class CountManager {private CountManager() {}

    public static CountManager getInstance() {return SingleHolder.instance;
    }

    private static class SingleHolder {private static final CountManager instance = new CountManager();
    }

    private int mCount = 0;

    public void addCount() {mCount++;
    }

    public int getCount() {return mCount;
    }
}

第一个activity

public class MainActivity extends AppCompatActivity {private static final String TAG = "MainActivity-1";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        CountManager.getInstance().addCount(); //单例中数据增加
        findViewById(R.id.btn_second_click).setOnClickListener(new View.OnClickListener() {@Override
            public void onClick(View v) {startActivity(new Intent(MainActivity.this, SecondActivity.class));
            }
        });
        Log.d(TAG, "onCreate: ");
    }

    @Override
    protected void onResume() {super.onResume();
        Log.d(TAG, "onResume: " + CountManager.getInstance().getCount());
    }

    @Override
    protected void onPause() {super.onPause();
        Log.d(TAG, "onPause: ");
    }

    @Override
    protected void onDestroy() {super.onDestroy();
        Log.d(TAG, "onDestroy: ");
    }
}

第二个activity

public class SecondActivity extends AppCompatActivity {private static final String TAG = "MainActivity-Second";

    @Override
    protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CountManager.getInstance().addCount();
         if (savedInstanceState == null) {getSupportFragmentManager().beginTransaction()
                    .replace(R.id.container, MainFragment.newInstance())
                    .commitNow();
        }
        Log.d(TAG, "onCreate: ");
    }

    @Override
    protected void onResume() {super.onResume();
        Log.d(TAG, "onResume: "+ CountManager.getInstance().getCount());
    }

    @Override
    protected void onPause() {super.onPause();
        Log.d(TAG, "onPause: ");
    }

    @Override
    protected void onStop() {super.onStop();
        Log.d(TAG, "onStop: ");
    }

    @Override
    protected void onDestroy() {super.onDestroy();
        Log.d(TAG, "onDestroy: ");
    }
}

备注:记得app增加摄像头权限

运行结果

1、启动app后,在MainActivity,点击跳转到SecondActivity

D  onCreate: 
15:28:09.177 com.xiaoma.testc                     D  onResume: 1

15:28:11.663 com.xiaoma.testc                     D  onPause: 
15:28:11.708 com.xiaoma.testc                     D  onCreate: 
15:28:11.751 com.xiaoma.testc                     D  onResume: 2

2、切换到桌面,摄像头权限进行关闭操作或者打开后再关闭

15:31:44.956 com.xiaoma.testc                     D  onPause: 
15:31:44.989 com.xiaoma.testc                     D  onStop: 
---------------------------- PROCESS ENDED (31143) for package com.xiaoma.testc ----------------------------

可以看到进程关闭了的信息
3、再次点击app,查看里面的数据,然后进行回退历史栈

---------------------------- PROCESS STARTED (32348) for package com.xiaoma.testc ----------------------------
15:32:57.135 com.xiaoma.testc                     D  onCreate: 
15:32:57.170 com.xiaoma.testc                     D  onResume: 1
15:33:13.688 com.xiaoma.testc                     D  onPause: 
//返回操作后
15:33:13.751 com.xiaoma.testc                     D  onCreate: 
15:33:13.755 com.xiaoma.testc                     D  onResume: 2
15:33:14.162 com.xiaoma.testc                     D  onStop: 
15:33:14.167 com.xiaoma.testc                     D  onDestroy:

可以看到重启启动了进程,且历史栈存在

总结

对于开发SDK的,由于需要宿主传递接口回调,会导致回调为null,所以在页面需要进行回调判断,进行相关的业务处理。

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


网页题目:Android兼容之摄像头权限关闭后应用生命周期和单例数据变化-创新互联
当前地址:http://hbruida.cn/article/djoegc.html