Screen Shots:
Note : Please add android-support-v4.jar file in Project libs folder.
1. MainActivity
public class MainActivity extends Activity implements
OnRequestPermissionsResultCallback
{
private static int REQUEST_CODE = 1;
private TextView tv;
private RelativeLayout layout;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// getting references
layout = (RelativeLayout)
findViewById(R.id.layout);
tv = (TextView)
findViewById(R.id.tv);
// checking weather the permission is already granted
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_CONTACTS) ==
PackageManager.PERMISSION_GRANTED) {
// permission is already granted
Toast.makeText(getApplicationContext(),
"Permission already
granted",
Toast.LENGTH_LONG).show();
// setting background color and text
layout.setBackgroundColor(Color.BLUE);
tv.setText("Permission already
granted");
} else {
// permission is not granted yet
// Asking for permission
ActivityCompat.requestPermissions(this,
new String[] {
Manifest.permission.READ_CONTACTS },
REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions, @NonNull int[]
grantResults)
{
if (requestCode == 1) {
//
checking if the permission is granted
if (grantResults.length > 0
&&
grantResults[0] ==
PackageManager.PERMISSION_GRANTED)
{
// permission is granted,do your operation
Toast.makeText(getApplicationContext(),
"Permission
granted",
Toast.LENGTH_SHORT).show();
// setting background color and text
layout.setBackgroundColor(Color.BLUE);
tv.setText("Permission granted");
}
else {
// permission not granted
// Display your message to customer
Toast.makeText(this, "You denied the permission",
Toast.LENGTH_LONG).show();
// setting background color and text
layout.setBackgroundColor(Color.RED);
tv.setText("You denied the permission");
}
}
}
}
2. activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="@string/hello_world"
android:textColor="#ffffff"
android:textSize="20dp"
android:textStyle="bold"
/>
</RelativeLayout>
3. AndroidManifest.xml
<?xml
version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.venkat.runtimepermissionsrequestdemo"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="23"
/>
<uses-permission
android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
>
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Note : Please add android-support-v4.jar file in Project libs folder.
************************************
************************************
No comments:
Post a Comment