I'm working with C# xamarin forms android and I'm trying to get the IMEI number. I've been searching about it and everything I found doesn't works. Now I'm trying with this code but again, Doesn't works.
Errors:
The name settings does not exist in the current context.
The name Forms does not exist in the current context.
C# code:
[assembly: Xamarin.Forms.Dependency(typeof(UniqueIdAndroid))]
namespace AppMobile.Models
{
public class UniqueIdAndroid
{
public string GetIdentifier()
{
return Settings.Secure.GetString(Forms.Context.ContentResolver, Settings.Secure.AndroidId);
}
}
}
Somebody knows how could I resolve this or knows other solution? Thank you very much!
UPDATE 1
c# code:
using Android.OS;
using AppMobile.Models;
using System;
using System.Collections.Generic;
using System.Text;
using static Android.Provider.Settings;
using Android.Provider;
using Xamarin.Forms;
[assembly: Xamarin.Forms.Dependency(typeof(UniqueIdAndroid))]
namespace AppMobile.Models
{
public class UniqueIdAndroid : IDevice
{
public string GetIdentifier()
{
return Settings.Secure.GetString(Forms.Context.ContentResolver, Settings.Secure.AndroidId);
}
}
}
I have added the IDevice interface that I don't know really what it should be and I fixed the settings problem with using Android.Provider;.
this link shows that I have to call with this:
string deviceIdentifier = DependencyService.Get<IDevice>().GetIdentifier();
But i don't understand how to implement IDevice interface.
Someone can help me? Thank you very much!
UPDATE 2
so, now I can use the function.
C# code:
using Android.OS;
using AppMobile.Models;
using System;
using System.Collections.Generic;
using System.Text;
using static Android.Provider.Settings;
using Android.Provider;
using Xamarin.Forms;
using Android.Content;
[assembly: Xamarin.Forms.Dependency(typeof(UniqueIdAndroid))]
namespace AppMobile.Models
{
public class UniqueIdAndroid
{
public string GetIdentifier()
{
//return Settings.Secure.GetString(Forms.Context.ContentResolver, Settings.Secure.AndroidId);
return Settings.Secure.GetString(Android.App.Application.Context.ContentResolver, Settings.Secure.AndroidId);
}
}
}
My problem is that this doesn't give me the IMEI. Insted it gives me the ID of the phone, ¿is there some way to get the IMEI string? Thank you very much!
You can try the following code:
Android.Telephony.TelephonyManager mTelephonyMgr;
mTelephonyMgr = (Android.Telephony.TelephonyManager)Android.App.Application.Context.GetSystemService(Android.Content.Context.TelephonyService);
string imei = mTelephonyMgr.GetMeid(0);
And from android document getMeid, we know that :
Starting with API level 29, persistent device identifiers are guarded
behind additional restrictions, and apps are recommended to use
resettable identifiers (see Best practices for unique identifiers).
This method can be invoked if one of the following requirements is
met:
If the calling app has been granted the READ_PRIVILEGED_PHONE_STATE permission; this is a privileged permission that can only be granted
to apps preloaded on the device.
If the calling app is the device owner of a fully-managed device, a profile owner of an organization-owned device, or their delegates
(see DevicePolicyManager.getEnrollmentSpecificId()).
If the calling app has carrier privileges (see hasCarrierPrivileges()) on any active subscription.
If the calling app is the default SMS role holder
Note:
In general, you can't get READ_PRIVILEGED_PHONE_STATE permission. It's only available to privileged system apps. Unless this is a personal app and you have a rooted device so you can make your app a privileged system app.
For more information, you can check document getMeid.
Related
TL;DR:
Need to use class Intent for different things. Found examples online where using Android.Content is used. This doesn't apply to Xamarin.Forms (I think). What NuGet package is needed for it to work?
Nah.. I got time:
Intro (unnecessary read)
I'm starting out with mobile app development so I'm pretty much clueless about anything. Hoping to get a concise answer here.
My app is primarily WebView container with some other minor functionalities. I'm using Visual Studio 2019 and Xamarin.Forms, but at the moment I am compiling to Android alone. iOS and UWP are my goal once, at least one app is fully finished for Android.
Problem 1 and where I am
Now I need to add two things. One is "Rate us" and the other is "Share" link, both in the main menu (swipe from left or click the hamburger icon to open it). I already have three list items in the menu, where each of them open a page in the app.
I figured out how to add another item to the menu, but I currently link this list item to market://details?id=com.mycompany.myapp url of my app on PlayStore. It opens nicely in PlayStore, but after pressing the back button it goes to home screen instead of back to the app.
Current code where I added "Rate Us"
MainPage.xaml.cs
using MyApp.Models;
using Plugin.Geolocator;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace MyApp.Views {
...
public partial class MainPage : MasterDetailPage {
...
public async Task NavigateFromMenu(int id) {
if (!MenuPages.ContainsKey(id) {
switch(id) {
...
case (int)MenuItemType.RateUs:
await Browser.OpenAsync("market://details?id=com.mycompany.myapp", BrowserLaunchMode.SystemPreferred);
// startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.mycompany.myapp")));
break;
...
}
}
...
}
}
}
MenuPage.xaml.cs
using MyApp.Models;
using System.Collections.Generic;
using System.ComponentModel;
using Xamarin.Forms;
namespace MyApp.Views {
...
public partial class MenuPage : ContentPage {
...
public MenuPage() {
InitializeComponent();
menuItems = new List<HomeMenuItem> {
...
new HomeMenuItem { id = MenuItemType.RateUs, Title="Rate Us", Icon="\uf005" },
...
}
}
}
}
HomeMenuItem.cs
namespace MyApp.Models {
public enum MenuItemType {
...
RateUs,
...
}
public class HomeMenuItem {
public MenuItemType Id { get; set; }
public string Title { get; set; }
public string Icon { get; set; }
}
}
What I tried
As you can see in // comment in MainPage.xaml.cs I found a different solution, that would perhaps return me back to my app after going back from PlayStore.
This particular solution is (obviously???) problematic at startActivity, tooltip stating
The name 'startActivity' does not exist in the current context
but there are other much more complex solution like THIS ONE that have startActivity wrapped in public void function() which - I guess - helps.
Problem 2
I need to add another List Item to side menu titled "Share" where the device will ask which app do you want to share with (or copy to clipboard) and then use that app to share some text and a link of my choosing (same for all users). Again, any LINK I find uses Intent.
At a complete loss here. If you can't give me the tools and a manual how to use them, at least point me in the right direction please.
There are a couple of ways to call the Intent class in your App, one of them, and maybe the best would be by making use of DependencyService.
From the docu:
The DependencyService class is a service locator that enables Xamarin.Forms applications to invoke native platform functionality from shared code.
Small Example for your use case
As you can see from the docu, the first you have to do is to create an Interface to define the method you want to implement. In our case, i will create a simple interface that defines a single method called LaunchAppInPlayStore (this you can call whatever you want!):
namespace App2
{
public interface Interface1
{
void LaunchAppOnPlayStore();
}
}
Lets go on and implement this in the Android project: for this purpose i add a class called MyInterfaceImplementationAndroid in it and make inherit from Interface1. Then i implement the method from the Interface as follows:
using Xamarin.Forms;
using Android.Content;
using Plugin.CurrentActivity;
[assembly: Dependency(typeof(App2.Droid.MyInterfaceImplementationAndroid))]
namespace App2.Droid
{
class MyInterfaceImplementationAndroid : Interface1
{
public void LaunchAppOnPlayStore()
{
CrossCurrentActivity.Current.AppContext.StartActivity(new Intent(Intent.ActionView, Android.Net.Uri.Parse("market://details?id=com.mycompany.myapp")));
}
}
}
You should note that in my example i use the Context provided by CurrentActivity plugin by #JamesMontemagno (this is just circumstantial, and in my own apps i myself use a different approach as i described in another SO Post (See the part where i add the MainApplication class to get the Current Context, or see the blog post by #DavidBritch (worth to read!)))
We are almost done! Now you just have to call the method in your shared code as follows:
For the sake of this example i created a simple page with a button:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="App2.TestInyection">
<ContentPage.Content>
<StackLayout>
<Button Text="Go to shop!"
Clicked="Button_Clicked"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
and in the code behind i just call the LaunchAppOnPlayStore method as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace App2
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class TestInyection : ContentPage
{
public TestInyection()
{
InitializeComponent();
}
private void Button_Clicked(object sender, EventArgs e)
{
DependencyService.Get<Interface1>().LaunchAppOnPlayStore();
}
}
}
Heey all,
I'm working on a Umbraco website. I try to reach my renderModels from my controller but, it says to me that type or namespace could not be found. So i try to add using namespace.Models;.
When i do this the namespace.controllers is reachable but when i try namespace.Models; it gives me the error the type or namespace name 'Models' does not exist in the namespace.
The folder Models does exist.
I tried searching online for it but could not find a awnser. Pls help
Edit 1
Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Core.Models;
using Umbraco.Web;
using Umbraco.Web.Models;
using System.Globalization;
namespace MyNamespace.Models
{
public class HomeRenderModel : BaseRenderModel
{
public HomeRenderModel()
{
//
// TODO: Add constructor logic here
//
}
}
}
Controller
using System.Web.Mvc;
using Umbraco.Web.Models;
using Umbraco.Web.Mvc;
using MyNamespace.Models;
namespace MyNamespace.Controllers
{
public class HomeController : RenderMvcController
{
public override ActionResult Index(RenderModel model)
{
var rendermodel = new HomeRenderModel(model.Content, model.CurrentCulture);
return CurrentTemplate(rendermodel);
}
}
}
Hope this helps, but basically I did the tutorial that mentions the model and controller etc. out there and had the same problem. I figured out that I had created the model folder containing the classes in the wrong place. You may have the same simple problem here.
Minimize all your folders in the project explorer, and then expand to see where you placed them. Your sub folders need to be in the project (in the 'folder' with the little globe icon, not above it with the solution properties.)
How do I check how many times facebook have been opened?
I'm using Xamarin.android with C#.
My goal is to check how many times facebook have been opened.
I want to increment a integer, each time the user opens the facebook app.
I have been looking at app usage statistics but it just seems very difficult.
If anyone could provide me with a snippet or something, it would be great!
Here's my fragment code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
namespace MyApp
{
public class Fragment1 : Android.Support.V4.App.Fragment
{
private ISharedPreferences prefs;
private ISharedPreferencesEditor editor;
private int totalCount;
public override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
editor = prefs.Edit();
}
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate(Resource.Layout.Fragment1, container, false);
TextView faceText = view.FindViewById<TextView>(Resource.Id.faceTextView1);
return view;
}
}
}
Thanks in advance!
There are a few ideas outlined here, but overall it's hard to get information about an app on the user's device from your app, unless the user goes through your app to launch Facebook.
You'd have to have a service running in the background listening for the user to open another app, and some way of verifying the other app had been opened, without access to the other app's code.
I have basic knowledge to create a web service using asmx.cs
and consume from any client application.
I want to create a Web Service using asmx.cs in .NET 3.5 using Visual Studio 2010.
For the following program,By default it gives textBox to take input from user.
But I want to use comboBox to take input from the user.
The result will be displayed as xml output.
I want to do a Web Service program where one city from ComboBox will be taken as input
and the temperature will be shown as xml output.
This code gives only textBox as input.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebService1
{
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string GetCityByZip(int Zip)
{
return "City Name = XYZ, Temperature = 30 Degree";
}
}
}
I think, what you're talking about is this html to test your web service methods, right?
http://3.bp.blogspot.com/_nuQwSyDoLk8/RvWnnpahW8I/AAAAAAAAAJw/UTFuJmCx5M0/s1600-h/WS3.bmp
This test ui is generated automatically based upon your service's WSDL.
The input types are based on the methods your service exposes. So if there's a method like CityInfo GetCityInfo(string cityName) in your service, there will be a textbox with input type string (as you already noticed). If you're exposing a method like CityInfo GetCityInfo(int cityId) the input type will be an int.
What's not possible, is to put a combobox there, as the service is autogenerated and has no knowledge which cities are selectable.
What you can do, is expose several methods and build a ui yourself (which you should do anyway).
public interface IYourServiceInterface
{
City[] GetCities(); // returns all possible cities
CityInfo GetCityInfo(City city); // returns detail Infos about a concrete city
}
There is an application which is used for taking surveys. I want to use REST api and add some questions to the survey. So basically I want to update some pages of the survey. Here are the links of that application.
My surveys name is trial and the page number that I want to update is 2.
fields#updateobject
Here is my code to do. But i think the http call I have used is not correct. and I am not able to figure out how to make the request.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
namespace ConsoleApplication1
{
class Program
{
private static object post;
private static object response;
static void Main(string[] args)
{
RunAsync().Wait();
}
static async Task RunAsync()
{
string webReq = "https://restapi.survey.com/v4/survey/trial/surveypage/2?_method=POST?api_token="/*token goes here*/;
// $.post{webReq};
}
}
}
Use System.Net.Http.HttpClient class for sending requests to any API. Tutorial and examples on sending various requests (GET, POST, etc.) can be found here.