Get parameters from android to unity - android

I have a problem, i want to send a parameter (int) from an app of build in android studio and receive it in an app build it in unity.
The apps work like this, you open the android app, press a botom and open the second app, i send and intent when i open the unity app but idkhow to recieve the param.
I know that are some questions about this but didn't work or maybe idk how to implement it to my code.
This is how i send the param
Java app
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.bodyar");
if(user!=null){
launchIntent.putExtra(Constant.TYPE,user.getLearningStyleId());
}else{
launchIntent.putExtra(Constant.TYPE,Constant.VISUAL);
}
startActivity(launchIntent);
but i dont know how to receive the value. I tried the stuff of UnityPlayer.UnitySendMessage(), but u just get an error from that
And the funciton of the Script that it´s attached to GameObjectUI is this
public void TipoTest(string token){
prueba.text = token;
}
And when i export the unity project to android, in the method OnCreate I recieve the params like this
public class UnityPlayerActivity extends Activity {
protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code
// Setup activity layout
#Override protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
mUnityPlayer = new UnityPlayer(this);
setContentView(mUnityPlayer);
mUnityPlayer.requestFocus();
}
#Override protected void onNewIntent(Intent intent) {
// To support deep linking, we need to make sure that the client can get access to
// the last sent intent. The clients access this through a JNI api that allows them
// to get the intent set on launch. To update that after launch we have to manually
// replace the intent with the one caught here.
setIntent(intent);
}
}
And It works but idk how to pass the variable to unity script

Related

Flutter - Is possible to import a Fragment inside a Flutter App?

In Flutter is possible to call Java code from Dart. Also, the plugin android_intent allows to start a Java Android Activity using Dart code
But, if we want to use an Android Fragment inside a Flutter Widget, is this possible? How can we do that?
Or the only way to use Fragments is declaring a new Activity in Java, and call to start the Activity in Flutter? (Like I said at the beginning)
So far I couldn't find any way to implement directly implement, in a straight forward manner, android components inside a flutter app. However is possible the opposite things, that is using a Flutter fragment inside an android application. So I can propose you a workaround for this problem that may lead to the same result:
use a method channel to start a new android activity:
void startAndroidActivity() async {
//TODO: fix result type, etc.
dynamic result = await MethodChannel("CHANNEL_X").invokeMethod('METHOD');
}
public class MainActivity extends FlutterActivity {
private MethodChannel.Result result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new MethodChannel(getFlutterView(), "CHANNEL_X").setMethodCallHandler((call, result) -> {
if (call.method.equals("METHOD")) {
Intent intent = new Intent(this, MyActivity.class);
this.result = result;
startActivityForResult(intent, data);
}
});
GeneratedPluginRegistrant.registerWith(this);
}
//TODO: Handle result
}
Implement your android components and import the flutter fragment. For this topic, I leave you this reference: https://flutter.dev/docs/development/add-to-app/android/add-flutter-fragment
Another reference I can give you is this one, that maybe can allow you to import what you want in Flutter, but it requires a bit more time: https://medium.com/#KarthikPonnam/build-your-own-plugin-using-platformviews-flutter-5b42b4c4fb0a

All apps associated with this action have been disabled, blocked, or are not installed Storage Access Framework

In my application I am trying to get MS-Word and PDF files through Storage Access Framework which works well on some devices I've tested upon but on Samsung note 4 API 6 I am getting an error
All apps associated with this action have been disabled, blocked, or
are not installed
Code:
warantyButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/pdf,application/msword");
Intent i = Intent.createChooser(intent, "File");
getActivity().startActivityForResult(i, FILE_REQ_CODE);
}
});
I'm not sure if this is directly related to your issue, but it is related to an issue I personally had with this error when using Intents (Incorrectly). I have received this error when attempting to declare an Intent globally. For example:
public class MyClass{
// Class Variables (BAD)
private Intent someActivity = new Intent(this, SomeClass.class);
#Override
protected void onCreate(Bundle savedInstanceState){
// Some Code
}
}
Then, I found that this issue was resolved when I did this:
public class MyClass{
// Class Variables (Not Bad)
private Intent someActivity;
#Override
protected void onCreate(Bundle savedInstanceState){
someActivity = new Intent(this, SomeClass.class);
}
}
If this doesn't help you in your specific situation, I hope this helps someone at some point.
When debugging to find the reason behind this problem, I couldn't see any note of the Toast that generated the text "All apps associated with this action have been disabled, blocked, or are not installed." There was no trace of this being an "error." It seems to me more of an OS-handled exception for incorrect usage of Intents.
Since setType() method of Intent takes one argument as String i.e MIME type and its compulsary because based on the MIME type requested android system finds all actions that are supported.
For example if you want to choose any type of content then you can simply write setType("*/*").

Activity method gets call before oncreate in android from the other class

I have a scenario where I have created library project in which I am loading the layout as well defining the method which I want to call from the Application project.
public class LibraryActivity {
public LibraryActivity() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.barchart_layout);
bindViews();
}
public void bindViews() {
bChart = (BarChart)findViewById(R.id.barchart);
}
public void setData(int count, float range) {
//definition of the method
// I have to user bChart view here
System.out.println("part 1 "+bChart); <---- this is null
}
Now I have successfully created the AAR project and I want to use this setData from the other project.
So when I run this From the other project By using below code
Intent in = new Intent(MainActivity.this,LibraryActivity.class);
startActivity(in);
LibraryActivity barChartCallBack = new LibraryActivity();
LibraryActivity.setData(15,25);
I got the null pointer on the bChart because my method setData got call first before oncreate so findviewbyid gives me null for the bChart.
You are setting the data on a completely different LibraryActivity object. First you tell the system with the intent to start one, then you start another one that does not get displayed, but you use it to call setData.
Read up on the android developer site:
https://developer.android.com/training/basics/firstapp/starting-activity.html
Basically you need to pass the data via extras in with the intent.

Calling an activity from a fragment - activity doesn't run

New to Android so bear with me.
I have a fragment (DirectionsFragment) that calls an Activity to get some data from Yelp (YelpActivity).
When I run the YelpActivity itself, without calling it from DirectionsFragment, it runs succesfully (displays the received information, prints out appropriate log messages, etc.).
However, when I create YelpActivity from the fragment, nothing happens - no log messages, no retrieved information.
Here's how I create the YelpActivity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
YelpActivity ya = new YelpActivity();
}
Thanks
Activity is started by startActivtiy(intent). But you have
YelpActivity ya = new YelpActivity();
Should not create an instance of Activity class.
Use
startActivity(new Intent(getActivtiy(),YealpActivtiy.class);
Make sure you have declared the activity in manifest file

Check for 3rd party app, open-if not install

I am working on a class that when selected by the user should open an application. If that application is not installed they will click the "Find it" button and install it.
Here is what I have so far
public class calc extends Activity {
static final String MARKET_SEARCH_Q_PNAME_PROVIDER = "market://search?q=pname:com.packagename.package";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.getpft);
setTitle("Install Marine PFT?");
((Button) findViewById(R.id.Ok)).setOnClickListener(new Openpft());
((Button) findViewById(R.id.FindIt)).setOnClickListener(new FindZxingOnclickListener());
}
public class FindZxingOnclickListener implements OnClickListener {
public void onClick(View v) {
Intent marketLaunch = new Intent(Intent.ACTION_VIEW);
marketLaunch.setData(Uri.parse(MARKET_SEARCH_Q_PNAME_PROVIDER));
startActivity(marketLaunch);
}}
};
So far the page opens up and it searches correctly for the app. However now that the app is downloaded I need to automatically skip this screen and just open that app. How is that done?
Somehow, you are launching that app. Presumably, you have an Intent that you are passing to startActivity() that does this. If so, you have two choices:
Just call startActivity() and route to your above code in the ActivityNotFoundException catch block
Use PackageManager and queryIntentActivities() to see if anything will respond to your Intent, and if not, route to your above code without calling startActivity() first

Categories

Resources