Cordova (Android) Show Configuration page - android

As I do , from my APP , to show the settings page in Android GPS (only) ?
I want, if the GPS is off, to take the user directly to the configuration
Is it possible ?
With some plugin?
Thanks

First download custom plugin form here https://github.com/don/cordova-plugin-hello
Install plugin from local :- cordova plugin install LocalPathofDownloadedPlugin
and in JavaScript add following code:-
hello.greet("World", success, failure);
var success = function(message) {
alert(message);
}
var failure = function() {
alert("Error calling Hello Plugin");
}
once you installed the above plugin you will have one java file (Hello.java) in src folder:-
public class Hello extends CordovaPlugin {
#Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
if (action.equals("greet")) {
//here you need to send intent
this.cordova.getActivity().startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), 1);
return true;
} else {
return false;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 1) {
switch (requestCode) {
case 1:
break;
}
}
}
}

I found the solution.
Thse two plugins can do it
https://www.npmjs.com/package/cordova.plugins.diagnostic#switchtolocationsettings
https://github.com/dpa99c/cordova-plugin-request-location-accuracy

Related

Open Android Activity and iOS ViewController from Flutter

I have a Flutter project that requires some certain features that needs to be implemented in native Android Activity or iOS ViewController. is there a way to navigate to android Activity and pass data to it and also retrieve data from it in Flutter?
and if it's impossible, is it possible to show an Activity or fragment from Android, and a ViewController from iOS, as a Widget in Flutter?
Not sure whether this is the best way and I only created it for Android, but this is what I did.
Simple Flutter method channel calling native:
static const platform = const MethodChannel(MY_CHANNEL);
string result await platform.invokeMethod("mycall");
From the native Android part in your mainActivity:
//Class attribute
private Result myresult;
//Method chanel
new MethodChannel(getFlutterView(), MY_CHANNEL).setMethodCallHandler(
(call, result) -> {
// Note: this method is invoked on the main thread.
if (call.method.equals("mycall")) {
myresult = result; //Store the flutter result
Intent intent1 = new Intent(MyClass.class);//Start your special native stuff
startActivityForResult(intent1, RQ_CODE);
} else {
result.notImplemented();
}
});
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Check which request we're responding to
if (requestCode == RQ_CODE) {
myresult.success("this will be your result"); //Probably do something with the data instead of a static string.
}
}
Basically the same can be done for iOS
use intent or android_intent packages for android and for ios use https://flutter.dev/docs/get-started/flutter-for/ios-devs link

how to Send object/string from android Activity onActivityResult to react native

In my react native project ,I have implemented payment native sdk in android (that does not supported in react native).so i am tried to call native sdk with native modules..
i am able calling the payment SDKUI from react native native module ,but when the time of results can not send results back to react native component..
Payment gateway is -> PAYUBIZ
for more details pls find below code..
at the end of payment gateway i have displayed payment response in android native alert..
Code used..
1. Created NATIVE MODULES in react native side..
import {NativeModules} from 'react-native';
module.exports = NativeModules.PayUBizAccess;
in button action following code to call native method from android
PayUBizAccess.showPayuBiz();
2. Created ReactContextBaseJavaModule based PayUBizModule
#ReactMethod
public void showPayuBiz() {
final Activity activity = getCurrentActivity();
Intent intent = new Intent(activity, PayuActivity.class);
getReactApplicationContext().startActivity(intent);
}
PayuActivity.class is the payment activity class
3. Display results after payment success or failure..
#Override
public void onActivityResult(int requestCode, int resultCode, final Intent data) {
if (requestCode == PayuConstants.PAYU_REQUEST_CODE) {
if (data != null) {
new AlertDialog.Builder(this)
.setCancelable(false)
.setMessage("Payu's Data : " + data.getStringExtra("payu_response") + "\n\n\n Merchant's Data: " + data.getStringExtra("result"))
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
finish();
}
}).show();
} else {
Toast.makeText(this, getString(R.string.could_not_receive_data), Toast.LENGTH_LONG).show();
}
}
}
4. After alert clicking button in alert it directly moves to react native component..
So now i want results data's to react native ,Kindly suggest me any solutions
thanks in advance
You can send an event from your native code like this:
private void sendEvent(ReactContext reactContext,
String eventName,
#Nullable WritableMap params) {
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
//supply the result in params
.emit(eventName, params);
}
And in your react native code you can receive the event like this:
componentWillMount: function() {
DeviceEventEmitter.addListener('keyboardWillShow', function(e: Event) {
// handle event.
});
}
Check full docs here
Another way of doing this is given here
I would suggest use of promise.
In your native module have a property as Promise mPromise;(also include import com.facebook.react.bridge.Promise;)
And accept promise in your react native method as
#ReactMethod
public void showPayuBiz(Promise promise) {
mPromise = promise;
final Activity activity = getCurrentActivity();
Intent intent = new Intent(activity, PayuActivity.class);
getReactApplicationContext().startActivity(intent);
}
And in your onActivityResult you can use it as follows.
#Override
public void onActivityResult(int requestCode, int resultCode, final Intent data)
{
if (requestCode == PayuConstants.PAYU_REQUEST_CODE) {
//example for handling success response
this.promise.resolve(data.getDataString()); // you can further process this data in react native component.
}
else{
//example for handling error response
this.promise.reject(data.getDataString());
}
}
Then you can use it as follows
PayUBizAccess.showPayuBiz()
.then(data => {
console.log(data);
//success
})
.catch(error => {
console.log(data);
//failure
});
Edit
If onActivityResult() is in another file. add mReactInstanceManager.onActivityResult(requestCode, resultCode, data); in onActivityResult() which is in MainActivity.
And inside you native module, add following method.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
this.mPromise.resolve(data.getDataString());
}
I have used #Ayush Khare responses..faced few issues while debugging ..so i post here exact answer
1.React Native Side
Component will mount add following
DeviceEventEmitter.addListener('PAYUEVENT', this.payuResponseGet);debugger
Add following method for trigger event
payuResponseGet = (payUData) => {
console.log(payUData);debugger // logging twice
// this.setState({
// modalVisible: args.visible
// })
}
2. Activity side
import com.facebook.react.ReactInstanceManager;
//import com.facebook.react.ReactApplicationContext;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.modules.core.DeviceEventManagerModule;
Add following on result method
// mPromise.resolve(data.getDataString());
ReactInstanceManager mReactInstanceManager = getReactNativeHost().getReactInstanceManager();
ReactApplicationContext context = (ReactApplicationContext) mReactInstanceManager.getCurrentReactContext();
WritableMap payuData = Arguments.createMap();
payuData.putString("PayuResponses", data.getStringExtra("payu_response"));
payuData.putString("Merchant's Data", data.getStringExtra("result"));
sendEvent(context,"PAYUEVENT",payuData);
//send event method
private void sendEvent(ReactContext reactContext,
String eventName,
#Nullable WritableMap params) {
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
//supply the result in params
.emit(eventName, params);
}
once added above, run and get responses from trigger event method..

React Native(Android) Scan QRCode

I wanna write a native module for to scan barcode but it too hard for me to figure out a way to handle the result without adding a method to the MainActivity. It's not a good idea to modify the MainActivity such heavily because it's no easy job for application developers who writes javascript to use the module.
For example, if I use ZXing Android Embedded: https://github.com/journeyapps/zxing-android-embedded, I have to add a method to MainActivity to handle the result.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
if(result.getContents() == null) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
} else {
// This is important, otherwise the result will not be passed to the fragment
super.onActivityResult(requestCode, resultCode, data);
}
}
That makes an issue that any one who uses this module has to modify their MainActivity, which means the module is hard to use. So, any ideas to work it out?
You can just use react-native-rn-zxing:
npm i react-native-rn-zxing
then link it :
react-native link react-native-rn-zxing
And enjoy

Android Google Plus SDK: how to get callback on the +1 button (PlusOneButton)

I added a +1 button in my Android app. I would like to add a callback in order to know what happened after the user clicked on the +1 button (did he validate its +1 ?, did he abort ? ...)
How can I do that ?
Thanks !
You can add a listener to check when the button is clicked and later check the result of the activity.
static final int PLUS_ONE_REQUEST = 1;
...
mPlusOneButton.setOnPlusOneClickListener(new PlusOneButton.OnPlusOneClickListener() {
#Override
public void onPlusOneClick(Intent intent) {
//here you can handle the initial click
//Start the activity to display the +1 confirmation dialog.
startActivityForResult(intent, PLUS_ONE_REQUEST);
}
});
...
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PLUS_ONE_REQUEST) {
switch(resultCode) {
case RESULT_OK:
//here the operation was successful
break;
case RESULT_CANCELED:
//here the user backed out or failed
break;
}
}
}
Sources:
Handling the click
Getting a result from an activity
I hope that this is what your were asking, and more importantly that this was helpful.

Facebook requestCodes

I have an Activity that should handle results from both the Facebook SDK, and from other custom Activities.
Where can I find the requestCodes used by the Facebook SDK, in order to not use the same for my Activities?
I should be able to tell them apart in the onActivityResult using their requestCode so they need to be unique.
Pass the request code in the sdkInitialize call
FacebookSdk.sdkInitialize(context, 1200);
Then
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (FacebookSdk.isFacebookRequestCode(requestCode)) {
//Facebook activity result
//Do your stuff here
//Further you can also check if it's login or Share etc by using
//CallbackManagerImpl as explained by rajath's answer here
if (requestCode == CallbackManagerImpl.RequestCodeOffset.Login.toRequestCode()) {
//login
}
else if (requestCode == CallbackManagerImpl.RequestCodeOffset.Share.toRequestCode()){
//share
}
}
From the docs
isFacebookRequestCode(int)
Returns true if the request code is within the range used by Facebook SDK requests. This does not include request codes that you explicitly set on the dialogs, buttons or LoginManager. The range of request codes that the SDK uses starts at the callbackRequestCodeOffset and continues for the next 100 values.
sdkInitialize(Context, int)
This function initializes the Facebook SDK, the behavior of Facebook SDK functions are undetermined if this function is not called. It should be called as early as possible.
public static synchronized void sdkInitialize(Context applicationContext, int callbackRequestCodeOffset)
applicationContext The application context
callbackRequestCodeOffset The request code offset that Facebook activities will be called with. Please do not use the range between the value you set and another 100 entries after it in your other requests.
Go to CallbackManagerImpl.RequestCodeOffset. I personally used a piece of code like this to prevent unwanted behaviour.
if (requestCode == CallbackManagerImpl.RequestCodeOffset.Login.toRequestCode()) {
callbackManager.onActivityResult(requestCode, resultCode, data);
}
Try this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
//Log.iClassMethod();
switch(requestCode)
{
case 1:
if (resultCode == Activity.RESULT_OK)
{
// do something ...
}
break;
case ...:
if (resultCode == Activity.RESULT_OK)
{
// do something ...
}
break;
case Session.DEFAULT_AUTHORIZE_ACTIVITY_CODE:
{
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
Log.i("Facebook");
}
break;
}
}
Offering an alternative if you're using FB login via LoginButton
Set request code of login button
Use the request code to differentiate activity
private LoginButton mFacebookLoginButton;
private static int RC_FB_SIGN_IN;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
mFacebookLoginButton = (LoginButton) findByViewId(R.id.fb_login_button);
mFacebookLoginButton.registerCallback(...)
RC_FB_SIGN_IN = mFacebookLoginButton.getRequestCode();
...
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_GP_SIGN_IN) {
...
} else if (requestCode == RC_FB_SIGN_IN) {
Log.i(TAG, "Doing my facebook usual things");
mFacebookCallbackManager.onActivityResult(requestCode, resultCode, data);
}
}
You can set your own request codes to disambiguate. All the OpenRequest and NewPermissionsRequest classes take a requestCode parameter:
setRequestCode
Adding to Apetroaei Andrei's answer, there are other options available at Facebook SDK's CallbackManagerImpl class:
public enum RequestCodeOffset {
Login(0),
Share(1),
Message(2),
Like(3),
GameRequest(4),
AppGroupCreate(5),
AppGroupJoin(6),
AppInvite(7),
;
These can be accessed by the foll. code:
if (requestCode == CallbackManagerImpl.RequestCodeOffset.Share.toRequestCode()) {
callbackManager.onActivityResult(requestCode, resultCode, data);
}
in the lates sdk, (4.4.1), you can manually set the request code by modifying two files in the facebook library:
1) LoginClient.java
public static int getLoginRequestCode() {
//return CallbackManagerImpl.RequestCodeOffset.Login.toRequestCode();
return [your request code here];
}
2)LoginManager.java
public void registerCallback(
...
((CallbackManagerImpl) callbackManager).registerCallback([your request code here],
new CallbackManagerImpl.Callback() {
#Override
...
}
);
}

Categories

Resources