Android – How to open an app programatically?

How to open an Android app with sent data?

Open an Android app and send data

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var appPackageName = "xxx.yyy.zzzz"
PackageManager pm = Android.App.Application.Context.PackageManager;
Intent it = pm.GetLaunchIntentForPackage(appPackageName);
if (it != null)
{
it.PutExtra("data", someThing);
it.SetFlags(ActivityFlags.NewTask);
Android.App.Application.Context.StartActivity(it);
}
var appPackageName = "xxx.yyy.zzzz" PackageManager pm = Android.App.Application.Context.PackageManager; Intent it = pm.GetLaunchIntentForPackage(appPackageName); if (it != null) { it.PutExtra("data", someThing); it.SetFlags(ActivityFlags.NewTask); Android.App.Application.Context.StartActivity(it); }
var appPackageName = "xxx.yyy.zzzz"
PackageManager pm = Android.App.Application.Context.PackageManager;
Intent it = pm.GetLaunchIntentForPackage(appPackageName);
if (it != null)
{
    it.PutExtra("data", someThing);
    it.SetFlags(ActivityFlags.NewTask);
    Android.App.Application.Context.StartActivity(it);
}

Android app is opened and get data

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
protected override void OnCreate(Bundle savedInstanceState)
{
...
var data = Intent.GetStringExtra("data");
if (data != null)
{
DataModel obj = JsonConvert.DeserializeObject<DataModel>(data);
}
}
protected override void OnCreate(Bundle savedInstanceState) { ... var data = Intent.GetStringExtra("data"); if (data != null) { DataModel obj = JsonConvert.DeserializeObject<DataModel>(data); } }
protected override void OnCreate(Bundle savedInstanceState)
{
...
    var data = Intent.GetStringExtra("data");
    if (data != null)
    {
        DataModel obj = JsonConvert.DeserializeObject<DataModel>(data);
    }
}

Why can’t app be opened in Android 10, 11?

It’s because of Android background limitation
background-starts

It’s necessary to add this permission into AndroidManifest file, user needs to turn it on

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
	<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

Be the first to comment

Leave a Reply

Your email address will not be published.


*