I am trying to set up android app link for my app. The problem is that the intent is received by an activity and I need to send this event to my javascript code(to navigate to the correct screen) through RCTDeviceEventEmitter but for this to happen I need to get react context somehow in my activity's code...I have no idea how to do this..This is what I have tried:
public class NewFriendActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(null);
class ReactContextFetcher extends ReactContextBaseJavaModule {
#Override
public String getName() {
return "ReactContextFetcher";
}
public ReactContext fetchReactContext() {
return getReactApplicationContext();
}
}
Intent intent = getIntent();
String data = intent.getData().toString();
String user = data.split("/add-friend/")[1];
Log.d("obscure_tag", user);
ReactContext rContext = new ReactContextFetcher().fetchReactContext();
Log.d("obscure_tag", "this is run as well..");
sendEvent(rContext, "NewFriend", user);
}
private void sendEvent(ReactContext reactContext,
String eventName, String user) {
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, user);
}
}
"user" gets logged but "this is run as well.." doesn't, the app crashes before that..So I get that fetchReactContext() is not the correct way to get react context..I referred to this but actually I did not understand what that guy is trying to say..How should I get react context in this situation?
Related
I have set up an android app link for my react native application...But
whenever I tap the link and choose my application to open it, system does try to open my app but my app crashes...I have set up the android linking properly and the activity which I want to start when the link is tapped does open..But I am not able to set up the RCTDeviceEventEmitter in the activity...I was confused about how to provide react context here..So I created a custom class in ReactContextFetcher.java like this:
public class ReactContextFetcher extends ReactContextBaseJavaModule {
#Override
public String getName() {
return "ReactContextFetcher";
}
public ReactContext fetchReactContext() {
return getReactApplicationContext();
}
}
And I am using this context in the activity started by android app link like this:
public class NewFriendActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(null);
ReactContext rContext = new ReactContextFetcher().fetchReactContext();
Intent intent = getIntent();
String data = intent.getData().toString();
String user = data.split("/add-friend/")[1];
Log.d("obscure_tag", data.toString());
sendEvent(rContext, "NewFriend", user);
}
private void sendEvent(ReactContext reactContext,
String eventName, String user) {
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, user);
}
}
This is how I am receiving this event in javascript:
import {NativeEventEmitter, NativeModules} from 'react-native';
class NewFriendReceiver extends React.Component {
componentDidMount() {
const eventEmitter = new NativeEventEmitter(NativeModules.ToastExample);
this.eventListener = eventEmitter.addListener('NewFriend', event => {
console.log(event.eventProperty); // "someValue"
});
}
componentWillUnmount() {
this.eventListener.remove(); //Removes the listener
}
}
What am I doing wrong? Why does my app crash when I tap on the android link? Does it have anything to do with how I use react context?
I am integrating Telr payment gateway in my app. I have used code from there dummy project which is working fine, I used their code in my fragment but it is throwing null context error. I have tried everything like "getContext" , "getApplicationContext" etc. but showing me error that context is null
Code :
public void sendMessage() {
Intent intent = new Intent(getActivity(), WebviewActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
intent.putExtra(WebviewActivity.EXTRA_MESSAGE, getMobileRequest());
intent.putExtra(WebviewActivity.SUCCESS_ACTIVTY_CLASS_NAME, "com.marketplace.activity.SuccessTransationActivity");
intent.putExtra(WebviewActivity.FAILED_ACTIVTY_CLASS_NAME, "com.marketplace.activity.FailedTransationActivity");
intent.putExtra(WebviewActivity.IS_SECURITY_ENABLED, isSecurityEnabled);
startActivity(intent);
}
There is a variable 'a' that exists in TelrApplication class.
extend your application with TelrApplication. This will resolve your problem.
public class TelrApplication extends Application {
private static Context a;
public TelrApplication() {
}
public void onCreate() {
super.onCreate();
Log.d("Hany", "Context Started....");
a = this.getApplicationContext();
}
public static Context a() {
return a;
}
}
I´m pretty new in android. I have made communication between two Apps with BroadcastReceiver and intentServices .
The thing is, I want to send information to the app2 from app1. In app1 I need to access a variable which is in MainActivity.class , I need to send it to servicev.class (the service where the intent is handled) but the variable "res" is null when I access it, why does that happen? (App2 calls app1 onHandleIntent and it breaks in res.getOtp() ) I try to create an extra setter getter class and also an intent but getIntent() does not work inside onHandleIntent... how can I achieve to pass res.getOTP (string) ? I really dont want to use SQLite
servicev:
public class servicev extends IntentService {
private static final int RESULT_OK = 1;
protected ResultReceiver mReceiver;
public servicev() {
super("yeah");
}
#Override
protected void onHandleIntent(Intent intent) {
//I receive here the intent from app2 and I need to response with res.getOTP()
helper h = new helper();
String val = intent.getStringExtra("foo");
Intent in = new Intent("com.banorte.bem.movil.veriToken.SendBroadcast");
in.putExtra("resultCode", this.RESULT_OK);
in.putExtra("resultValue", "My Result Value. Passed in: " + h.getRes().getOtp()); //h here is null... setter and getter approach does not work... maybe sqlite could work but it is necesary?
sendBroadcast(in);
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
VTTokenAPI api;
TextView textView;
Button button;
EditText input;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidSetup.getInstance().init(this);
helper h = new helper();
api = new VTTokenAPI("FFFFFF");
res = api.getStatus();
res.getOtp(); //correct value
h.setRes(res);
setContentView(R.layout.activity_main);
}
}
helper:
public class helper {
public VTResult getRes() {
return res;
}
public void setRes(VTResult res) {
this.res = res;
}
VTResult res;
}
You are trying to instantiate a new MainActivity which is not the same as the running activity but a new instance.
If you need your IntentService to be able to get data from a running Activity you have options such as using SharedPreferences or SQLite. Instead of keeping the data in memory try to persist it in some database in the onCreate and then try to read it from the storage during handleIntent
I am working on unity android project.
I have called the android side methods from unity like this
AndroidJavaObject aObj = new AndroidJavaObject("com.mypackage.UnityBridge",param1,param2);
aObj.Call("callme");
And on Android side
public class UnityBridge{
public UnityBridge(final String param1, final int param2) {
activity = UnityPlayer.currentActivity;
this.param1= param1;
this.param2= param2;
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
// INITIALIZATION OF ANDROID CLASS CONSTRUCTOR HERE
}
});
}
public void callme() {
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
if(obj!= null)
{
// ANDROID METHOD CALL HERE
}
}
});
}
This much is working perfectly.
If I want to call Activity specific methods like onPause(), onResume(), so there is a method in unity to do so.
void OnApplicationPause(bool pauseStatus) {
// HERE I CAN CALL activity specific **onPause()** and **onResume()** based on pauseStatus
}
Is there anything in unity from which I can give call to onNewIntent(Intent i) of Android. If not then how is it possible to call onNewIntent()
Please help to resolve this.
I wrote a post how this problem can be solved without overriding Unity activity (its about onActivityResult but the principle is the same): https://medium.com/#tarasleskiv/unity-android-plugins-and-onactivityresult-callback-abef4b6bbc87#.6d7sl8z81
Basically you create a custom empty activity and start it just to receive these callbacks and finish it immediately after that.
Check also this post about how to create activity for onNewIntent callback: https://blog.getsocial.im/android-unity-app-and-the-intent-issue/
Here is the example code of the intermediate activity yo have to create:
public class MyDeepLinkingActivity extends Activity
{
private static String TAG = "GetSocial GetSocialDeepLinkingActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Log.v(TAG, "Forward the intent");
// Do with intent what you need to
Log.v(TAG, "Returning to main activity");
//start main activity
Intent newIntent = new Intent(this, getMainActivityClass());
this.startActivity(newIntent);
finish();
}
private Class<?> getMainActivityClass() {
String packageName = this.getPackageName();
Intent launchIntent = this.getPackageManager().getLaunchIntentForPackage(packageName);
try {
return Class.forName(launchIntent.getComponent().getClassName());
} catch (Exception e) {
Log.e(TAG, "Unable to find Main Activity Class");
return null;
}
}
}
Looks like there is no built it way to get onNewIntent callback in Unity. However i can suggest to export google android project from unity and override this method in main unity activity
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
//some code
}
Or you can create android plugin with your own activity which extends unity activity, but keep in mind that some plugins could stop working if you do this.
I am using social auth library for sharing and login with social media in android app.
I have logged in and authorize successfully with facebook.
But when I try to signout app crashed with nullpointer exception
adapter.signOut(getActivity(), Provider.FACEBOOK.toString());
Getting below error:
05-09 10:24:23.010: E/AndroidRuntime(19998): java.lang.NullPointerException
05-09 10:24:23.010: E/AndroidRuntime(19998): at org.brickred.socialauth.android.SocialAuthAdapter.signOut(SocialAuthAdapter.java:797)
I am using latest version. socialauth-4.4.jar and socialauth-android-3.2.jar
Please make sure to call from activity. Getactivity() from fragment not working after calling method from activity like this will work adapter.signOut(this, Provider.FACEBOOK);
I had a similar problem, this solution works for me:
https://code.google.com/p/socialauth-android/issues/detail?id=108#c16
basically there is a bug in the log out function, you have to log in before log out, otherwise you may get an NPE. the solution above is create a new SocialAuthManager when necessary.
I suggest to import that source code as a java module instead of using the jar file, so you can fix something by yourself, like change the dialog title text, etc...
In signOut function put these lines,
if (providerName != null) {
if (socialAuthManager == null) {
socialAuthManager = new SocialAuthManager();
try {
loadConfig(ctx);
} catch (Exception e) {
Log.d(" SocialAuthAdapter ", "Could not load configuration");
}
}
before
if (socialAuthManager.getConnectedProvidersIds().contains(providerName)) socialAuthManager.disconnectProvider(providerName);
This has solved the issue for me for now, in the class where you would like to sign out instantiate a new adapter if the application-wide adapter is null
//My Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
//Get the currently available adapter
myApp = (MyApplication) getApplication();
adapter = myApp.getSocialAuthAdapter();
//Adapter initialization if null
if (adapter==null){
adapter = new SocialAuthAdapter(new ResponseListener());
}
}
//ResponseListener Class
public final class ResponseListener implements DialogListener {
#Override
public void onComplete(Bundle values) {
String providerName = values.getString(SocialAuthAdapter.PROVIDER);
// Set a application wide reference to the social adapter here
myApp = (MyApplication) getApplication();
myApp.setSocialAuthAdapter(adapter);
}
#Override
public void onError(SocialAuthError error) {
Log.d("Custom-UI", "Error");
error.printStackTrace();
}
#Override
public void onCancel() {
Log.d("Custom-UI", "Cancelled");
}
#Override
public void onBack() {
Log.d("Custom-UI", "Dialog Closed by pressing Back Key");
}
}
//Code for application class
public class MyApplication extends Application {
// SocialAuth Component
private SocialAuthAdapter socialAuthAdpater;
public SocialAuthAdapter getSocialAuthAdapter() {
return socialAuthAdpater;
}
public void setSocialAuthAdapter(SocialAuthAdapter socialAuthAdapter) {
this.socialAuthAdpater = socialAuthAdapter;
}