Cocos2d-x : Android M 6.0 - Create Request Permission
Test On:
- Mac OS X El Capitan
- Eclipse
- Android SDK
- Android NDK
- Python 2.7.11
- Cocos2d-x 2.2.3
- Lenovo A7000 + Android 6.0
[ Free sample code]
Apk file: http://sh.st/IFhC2
Source code: http://sh.st/IFh5G
[ Pre-require ]
You need to install and setup your environment first that not shown on this tutorial, so you can find from official site of cocos2dx here: http://www.cocos2d-x.org/ or read from my post http://gamedevtodied.blogspot.com/2014/09/cocos2d-x-v2x-create-c-lua-and.html after you've success created project without error then follow me next step.
[ Import and setting ]
Import your project into Eclipse by goto File > Import...
Then import wizard will open, choose Android > Existing Android Code Into Workspace and click "Next >".
"Browse..." to your cocos2dx folder (e.g. cocos2d-x-2.2.3) then check only two project the one is cocos lib for android (e.g. cocos2dx/platform/android/java) and other one is your project (e.g. projects/HelloWorld/proj.android), click "Finish".
Hint
[/] cocos2dx/platform/android/java
[/] projects/HelloWorld/proj.android
Now you should have 2 projects in left workspace.
Right-click on your project (e.g. HelloWorld) goto Properties > Resource > Linked Resources. Click on tab "Path Variables" and click "New..." add name and value of variable to your cocos2d-x-version folder and click "OK".
And your setting should be like this.
Next, you're still in Properties goto Android, under "Project Build Target" check "Android 6.0" (if you were not update android sdk this list not appear also) and under "Library" you should have libcocos2dx as your library.
Next, you're still in Properties goto C/C++ Build > Environment. Then you need to "Add..." path to any variable to NDK, Android SDK and Cocos2dx, click "OK".
Hint
Next, you're still in Properties goto Java Build Path and click tab "Libraries", then click "Add External JARs..." and browse to "android_sdk/extras/android/support/v7/appcompat/libs" and add "android-support-v4.jar" and "android-support-v7-appcompat.jar".
Click on tab "Order and Export" and checkd libs for support v4 and v7 after that click "OK".
[ Config Manifest file]
From Eclipse under project, open "AndroidManifest.xml" and add this line before <application>
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeOnPause() {
CCApplication::sharedApplication()->applicationDidEnterBackground();
CCNotificationCenter::sharedNotificationCenter()->postNotification(EVENT_COME_TO_BACKGROUND, NULL);
}
[ Free sample code]
Apk file: http://sh.st/IFhC2
Source code: http://sh.st/IFh5G
Test On:
- Mac OS X El Capitan
- Eclipse
- Android SDK
- Android NDK
- Python 2.7.11
- Cocos2d-x 2.2.3
- Lenovo A7000 + Android 6.0
[ Free sample code]
Apk file: http://sh.st/IFhC2
Source code: http://sh.st/IFh5G
[ Pre-require ]
You need to install and setup your environment first that not shown on this tutorial, so you can find from official site of cocos2dx here: http://www.cocos2d-x.org/ or read from my post http://gamedevtodied.blogspot.com/2014/09/cocos2d-x-v2x-create-c-lua-and.html after you've success created project without error then follow me next step.
[ Import and setting ]
Import your project into Eclipse by goto File > Import...
Then import wizard will open, choose Android > Existing Android Code Into Workspace and click "Next >".
"Browse..." to your cocos2dx folder (e.g. cocos2d-x-2.2.3) then check only two project the one is cocos lib for android (e.g. cocos2dx/platform/android/java) and other one is your project (e.g. projects/HelloWorld/proj.android), click "Finish".
Hint
[/] cocos2dx/platform/android/java
[/] projects/HelloWorld/proj.android
Now you should have 2 projects in left workspace.
Right-click on your project (e.g. HelloWorld) goto Properties > Resource > Linked Resources. Click on tab "Path Variables" and click "New..." add name and value of variable to your cocos2d-x-version folder and click "OK".
And your setting should be like this.
Next, you're still in Properties goto Android, under "Project Build Target" check "Android 6.0" (if you were not update android sdk this list not appear also) and under "Library" you should have libcocos2dx as your library.
Next, you're still in Properties goto C/C++ Build > Environment. Then you need to "Add..." path to any variable to NDK, Android SDK and Cocos2dx, click "OK".
Hint
name: NDK_ROOT variable: /Applications/android-ndk-r10c
name: COCOS2DX variable: /Users/poraweeraksasin/Downloads/cocos2d-x-2.2.3
name: ANDROID_SDK_ROOT variable: /Applications/adt-bundle-mac-x86_64-20140702/sdk
Next, you're still in Properties goto Java Build Path and click tab "Libraries", then click "Add External JARs..." and browse to "android_sdk/extras/android/support/v7/appcompat/libs" and add "android-support-v4.jar" and "android-support-v7-appcompat.jar".
Click on tab "Order and Export" and checkd libs for support v4 and v7 after that click "OK".
[ Config Manifest file]
From Eclipse under project, open "AndroidManifest.xml" and add this line before <application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
[ Implement Request Permission ]
Open HelloWorld.java, it's should be under project/src folder and then add import any line from following package.
import android.Manifest;
import android.annotation.TargetApi;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
On the line of HelloWorld class after extends Cocos2dxActivity add this implementation for callback result of permission here.
implements ActivityCompat.OnRequestPermissionsResultCallback
Before start onCreate function, declare variable for TAG printout debug log and location permission like this.
private String TAG = "HelloWorld";
private static final int REQUEST_FINE_LOCATION = 123;
Under onCreate function after line of super type this condition for check allowance of permission and android version that equal or greater than 23 (Android M).
if (Build.VERSION.SDK_INT >= 23) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Location permission has not been granted.
Log.d(TAG, "ContextCompat");
if (ActivityCompat.checkSelfPermission(Cocos2dxActivity.getContext(), Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Location permission has not been granted.
Log.d(TAG, "ActivityCompat");
requestFineLocationPermission();
} else {
// Location permissions is already available.
Log.d(TAG, "LOCATION permission has already been granted.");
}
} else {
// LOCATION permissions is already available.
Log.d(TAG, "LOCATION permission has already been granted.");
}
}
You can remove all line of "Log.d" when your app pass text verification of permission.
Next, you need to create function "requestFineLocationPermission" that's refer to previous check permission was not grant.
private void requestFineLocationPermission() {
Log.d(TAG, "LOCATION permission has NOT been granted. Requesting permission.");
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
// Provide an additional rationale to the user if the permission was not granted
// and the user would benefit from additional context for the use of the permission.
// For example if the user has previously denied the permission.
Log.d(TAG, "Displaying LOCATION permission rationale to provide additional context.");
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_FINE_LOCATION);
} else {
// Location permission has not been granted yet. Request it directly.
Log.d(TAG, "LOCATION permission has not been granted yet. Request it directly.");
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_FINE_LOCATION);
}
}
We're always need user to grant by allow permission because of your app need this access for function of app. If user choose deny will be didn't pass this thread.
The last thing is callback function name "onRequestPermissionResult" will capture user grant by deny or allow your permission request, then what you should do next. For this example i just force exit app if user not grant by allow.
@TargetApi(23) @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == REQUEST_FINE_LOCATION) {
// Received permission result for location permission.
Log.d(TAG, "Received response for LOCATION permission request.");
// Check if the only required permission has been granted
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Location permission has been granted, preview can be displayed
Log.d(TAG, "LOCATION permission has now been granted. Showing preview.");
} else {
Log.d(TAG, "LOCATION permission was NOT granted exit app.");
finish();
System.exit(0);
}
} else {
this.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
Let's start connect your android device with version 6.0 of Android OS and test.
[ Export and test ]
Connect your android device via usb, then right-click on your project and goto Run As > Android Application.
*If you found an error "libc Fatal signal 11 (SIGSEGV), code 1" and previous "CCApplication.cpp function:shareApplication line:60" seem like below image.
Under your project goto "cocos2dx > platform > android > jni" open file "Java_org_cocos2dx_lib_Cocos2dxRenderer.cpp" then check this nativeOnPause.
CCApplication::sharedApplication()->applicationDidEnterBackground();
CCNotificationCenter::sharedNotificationCenter()->postNotification(EVENT_COME_TO_BACKGROUND, NULL);
}
And change it to be like this by add if condition for CCApplication.
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeOnPause() {
if (CCDirector::sharedDirector()->getOpenGLView()) {
CCApplication::sharedApplication()->applicationDidEnterBackground();
}
CCNotificationCenter::sharedNotificationCenter()->postNotification(EVENT_COME_TO_BACKGROUND, NULL);
}
Now, "Run As > Android Application" again, it's should be fine.
Apk file: http://sh.st/IFhC2
Source code: http://sh.st/IFh5G




















Comments
Post a Comment