From Unity android app open setting panel within custom canvas view - android

I would like to open Unity Android application from the setting panel within the custom canvas view.
Can I get a code snippet to bring it within canvas view rather than opening over the whole window?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Runtime.InteropServices;
using System.IO;
using System;
public class GetSetting : MonoBehaviour
{
void Start()
{
#if UNITY_ANDROID
string bundleId = "com.android.settings";
AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject ca = up.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject packageManager = ca.Call<AndroidJavaObject>("getPackageManager");
//if the app is installed, no errors. Else, doesn't get past next line
AndroidJavaObject launchIntent = packageManager.Call<AndroidJavaObject>("getLaunchIntentForPackage", bundleId);
launchIntent.Call<AndroidJavaObject>("addCategory", "android.intent.category.MAIN");
launchIntent.Call<AndroidJavaObject>("addCategory", "android.intent.category.HOME");
launchIntent.Call<AndroidJavaObject>("setFlags", 0x10000000);
ca.Call("startActivity", launchIntent);
up.Dispose();
ca.Dispose();
packageManager.Dispose();
launchIntent.Dispose();
#else
Debug.Log("No Toast setup for this platform.");
#endif
}
}

Related

Xamarin displays blank PDF in custom webviewer

I am currently busy with a Xamarin mobile application (only android now) and I am trying to show PDF files. I found a free plugin here: https://github.com/xamarin/docs-archive/tree/master/Recipes/xamarin-forms/Controls/display-pdf but I run into an issue.
When I go to the view I see the PDF controls, but not the PDF itself. (shows a gray screen). Here is a similar issue: display local PDF file in webview control - Displays Blank Pdf File but nothing is working.
I put the PDF file in the correct place with the correct options, see here:
And here you can see the view in the emulator:
I also tried the run the given demo project, but I get a lot of errors :(
Some help would be really appreciated!
CustomView:
using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;
namespace BizzTimeTest.ViewModels
{
public class CustomPDFWebView : WebView
{
public static readonly BindableProperty UriProperty = BindableProperty.Create(propertyName: "Uri",
returnType: typeof(string),
declaringType: typeof(CustomPDFWebView),
defaultValue: default(string));
public string Uri
{
get { return (string)GetValue(UriProperty); }
set { SetValue(UriProperty, value); }
}
}
}
Custom renderer:
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using BizzTimeTest.Droid;
using BizzTimeTest.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(CustomPDFWebView), typeof(CustomWebViewRenderer))]
namespace BizzTimeTest.Droid
{
[Obsolete]
public class CustomWebViewRenderer : WebViewRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
var customWebView = Element as CustomPDFWebView;
Control.Settings.AllowUniversalAccessFromFileURLs = true;
Control.LoadUrl(string.Format("file:///android_asset/pdfjs/web/viewer.html?file={0}", string.Format("file:///android_asset/Content/{0}", "test_pdf_file.pdf")));
}
}
}
}
xaml
<local:CustomPDFWebView x:Name="custom_pdf_viewer" IsVisible="false"
Uri="test_pdf_file.pdf" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />

Xamarin.Android Samsung Crashes on DatePickerDialog

I have a UserHandler Singleton Class and I declared a variable like this
class UserHandler
{
...
public DatePickerDialog dialog = null;
...
}
Then in my MainActivity.cs file
using Android.App;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;
using Android.Support.Design.Widget;
using Android.Support.V7.App;
using Android.Text;
using Android.Views;
using Android.Widget;
using Java.Interop;
using Java.Lang;
using System;
using System.Collections.Generic;
using System.Globalization;
using AlertDialog = Android.Support.V7.App.AlertDialog;
using Exception = System.Exception;
class MainActivity
{
private readonly UserHandler U_Handler = UserHandler.GetInstance;
...
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.activity_main);
...
U_Handler.dialog = new DatePickerDialog(this);
...
}
When I run it on my device (Oppo, Android 8.1.0), it works normally but when I tried it on other device (Samsung, Android 6.0.1), it crashes and upon debugging, it shows this
**Java.Lang.NoSuchMethodError:** 'no non-static method "Landroid/app/DatePickerDialog;.<init>(Landroid/content/Context;)V"'
How to solve this?
Should I use 3rd party Libraries?
Because the constructor DatePickerDialog(Context context) was added in API level 24,when you run it in Samsung, Android 6.0.1,it will throw the Java.Lang.NoSuchMethodError exception.
You can make a judgment call and use the old API when the SDK version is less than 24, and use the new API when it is greater than 24.
The api you could look at DatePickerDialog

i am passing data from android to unity and based on the name it sets the state active of that object. But i keep geting game object null reference

here I have exported project and created an aar file and imported in the android studio and my android studio simply passes an intent to unity player activity on a button click. but I keep getting the game object as null. What could be the problem?
this doesn't even work when I set the object manually and set it to true and run in unity only.. it doesn't even print debug.log();
Here i am using vuforia ground plane detection and my object is kitchen stool and i am placing it inside ground plane stage.
and the script is in ar camera.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class data : MonoBehaviour
{
// private Transform childObj;
// Start is called before the first frame update
void Start()
{
AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject intent = currentActivity.Call<AndroidJavaObject>("getIntent");
bool hasExtra = intent.Call<bool>("hasExtra", "arguments");
if (hasExtra)
{
AndroidJavaObject extras = intent.Call<AndroidJavaObject>("getExtras");
string arguments = extras.Call<string>("getString", "arguments");
GameObject g = GameObject.Find(arguments);
g.SetActive(true);
Debug.Log("hello");
}
}
// Update is called once per frame
void Update()
{
}
}
You cannot use .Find() to find a gameobject that is inactive in your scene. You can either assign the object up front in the inspector or use Resources.FindObjectsOfTypeAll. Alternatively, make the object the child of an empty gameobject, and use transform.GetChild(0) to toggle its state.

Xamarin.Forms implement VideoView

How to implement a VideoView into my Xamarin.Forms shared code class?
What I tried:
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using XamNative.ViewModels;
using XamNative.Droid;
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class VideoPage : ContentPage
{
public VideoPage ()
{
InitializeComponent ();
#if __ANDROID__
var linearLayout = new LinearLayout(Forms.Context);
linearLayout.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FillParent, LinearLayout.LayoutParams.WrapContent);
var _videoView = new VideoView(linearLayout.Context) { };
_videoView.SetMinimumHeight(300);
_videoView.SetMinimumWidth(300);
linearLayout.AddView(_videoView);
//MediaRecorder Code...
#endif
}
I think it has something to the with the VideoPage class inheriting from ContentPage, which is not the right inheritance.
The error is logged as "Application lost the surface".
I can implement a TextView this way, but not a VideoView
I would suggest using a Third Party Nuget as all the code is done for you.
You can either use:
Rox.Xamarin.Video Player: https://www.nuget.org/packages/Rox.Xamarin.Video/
Which is free.
Or a paid for component:
Octane.VideoPlayer: https://components.xamarin.com/view/video-player

WCF service working on debug but not working when calling from android

I created a WCF service in Visual Studio 2012 that I intend to consume in Xamarin and have it save user registration information to a table in SQL server. The service works fine when debugging in Visual Studio, the data entries actually save to my table in SQL. I want to consume the service in Xamarin and here is my code:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using ItusService;
using Android.App;
using Android.Locations;
using Android.OS;
using Android.Util;
using Android.Widget;
//some code here...
public static readonly EndpointAddress EndPoint = new EndpointAddress("http://192.168.1.155:12100/Service1.svc");
private Service1Client service;
protected override void OnCreate (Bundle bundle)
{
//some code here...
InitializeService1Client();
}
private void InitializeService1Client()
{
BasicHttpBinding binding = new BasicHttpBinding ();
service = new Service1Client(binding, EndPoint);
}
async void AddressButton_OnClick(object sender, EventArgs eventArgs)
{
//some code here...
UserLocation useraddress = new UserLocation ();
useraddress.ClientID = _client.Text = "ddsdfs";
useraddress.FullName = _name.Text = "dsffdf";
useraddress.LocationCoordinates = _locationText.Text;
useraddress.LocationAddress = _addressText.Text;
useraddress.DistressTime = _time.Text = "djdsk";
string result = service.ToString ();
_resultTxt.Text = result;
}
I don't know if there is something I am missing out because nothing is being saved to my SQL table from my Android device. I debug using both an emulator and my device, nothing has been saved using both. I have opened the port for listening, I have created a reference file using silverlight slsvc tool according to the Xamarin Walkthrough http://developer.xamarin.com/guides/cross-platform/application_fundamentals/web_services/walkthrough_working_with_WCF/ I have tried everything I possibly can. Please help.
The general pattern for calling a WCF method is something like this (this is general WCF and really has nothing to do with Xamarin or Android)
// create the client and bind it
client = new ServiceClient(binding, endpoint);
// specify the callback
client.GetLocationCompleted += OnGetLocationCompleted;
// make the request to your method, passing any required parameters
client.GetLocationAsync(useraddress);
because the WCF call is async, you need to provide a callback method (the naming of the EventArgs class will depend on how your service is defined)
public void OnGetLocationCompleted(object sender, GetLocationCompletedEventArgs args) {
// if something bad happend, args.Error will contain an exception
// otherwise args.Result will contain any return value from you method
}

Categories

Resources