Java tutorial
/* * Copyright (C) 2012 CyberAgent * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jp.co.cyberagent.android.gpuimage.sample.activity; import android.Manifest; import android.app.Activity; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Bundle; import android.support.v4.app.ActivityCompat; import android.support.v4.content.PermissionChecker; import android.view.View; import android.view.View.OnClickListener; import com.yml.testapp.R; public class ActivityMain extends Activity implements OnClickListener { @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_gpuimage); findViewById(R.id.button_gallery).setOnClickListener(this); findViewById(R.id.button_camera).setOnClickListener(this); } @Override public void onClick(final View v) { if (PermissionChecker.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_DENIED) { ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA }, v.getId()); } else if (PermissionChecker.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) { ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, v.getId()); } else { startActivity(v.getId()); } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { if (grantResults.length != 1 || grantResults[0] == PackageManager.PERMISSION_GRANTED) { startActivity(requestCode); } else { super.onRequestPermissionsResult(requestCode, permissions, grantResults); } } private void startActivity(int id) { switch (id) { case R.id.button_gallery: startActivity(new Intent(this, ActivityGallery.class)); break; case R.id.button_camera: startActivity(new Intent(this, ActivityCamera.class)); break; default: break; } } }