How to launch another app from the flutter desktop application? - flutter

I have a flutter application that communicates to a server in the same machine. So now I need to start the server with the flutter app automatically. So is there any way to start the server when I start the flutter app and kill the server when I close the flutter app?

If is in windows you can use the package Win32 :
https://pub.dev/packages/win32
try {
final verb = 'open'.toNativeUtf16();
final process = 'app.exe'.toNativeUtf16();
final params = 'p1'.toNativeUtf16();
final nullParams = ''.toNativeUtf16();
ShellExecute(0, verb, process, params, nullParams, SW_SHOW);
return true;
}catch(_){
return false;
}
For Linux u can use process run:
https://pub.dev/packages/process_run
var shell = Shell();
await shell.run('''
app.sh
''');

Related

Unique device id in flutter

I want to get a device id or any unique number that can not be changed when someone uninstalls the app. This device is for both android and iOS and is in the flutter framework. I want this to manage my device management.
Thanks for the help.
I tried some of the most commonly used packages in flutter like device_info_plus and flutter_udid but IDs generated from these changes every day.
Did you tried platform_device_id
This package can help you to get deviceId in android, iOS, windows, macOS and even in web and linux.
After importing the package you need to use following code
try {
deviceId = await PlatformDeviceId.getDeviceId;
} on PlatformException {
deviceId = 'Failed to get deviceId.';
}
You can use #Rohan's answer also. platform_device_id package. Read more
There is guide for device_info_plus package Read more
add package to your flutter project.
Create a method like this:
Future<String> getUniqueDeviceId() async {
String uniqueDeviceId = '';
var deviceInfo = DeviceInfoPlugin();
if (Platform.isIOS) {
var iosDeviceInfo = await deviceInfo.iosInfo;
uniqueDeviceId =
'${iosDeviceInfo.name}:${iosDeviceInfo.identifierForVendor}'; //
unique ID on iOS
} else if(Platform.isAndroid) {
var androidDeviceInfo = await deviceInfo.androidInfo;
uniqueDeviceId =
'${androidDeviceInfo.name}:${androidDeviceInfo.id}' ; // unique ID
on Android
}
return uniqueDeviceId;
}
use it like this:
String deviceId = await getUniqueDeviceId();
Note (updateded):
Don't use androidDeviceInfo.androidId. This would change when your mac address changes. Mobile devices above Android OS 10/11 will generate a randomized MAC. This feature is enabled by default unless disabled manually. This would cause the androidId to change when switiching networks. You can confirm this by yourself by changing androidDeviceInfo.id to androidDeviceInfo.androidId above.
(Got details from #Ariel)

Can I run a CLI command inside a flutter project

When I click the Boilerplate button I want to run the CLI command "flutter create projectName" (inside a flutter project/dart file)
I have already heard about
process.run
import 'dart:io';
main() async {
// List all files in the current directory in UNIX-like systems.
var result = await Process.run('ls', ['-l']);
print(result.stdout);
}
&
shell
var shell = Shell();
await shell.run('''
echo "hello world"
''');
But I don't know how to use in this situation

How do i make flutter application that open another app within it and not webview

I have tried to make flutter app in which within it I have a list of another application. I want on tap of any app within a list to open that app, like the code below. How to do it in flutter ?
openExternalApp : function(packageName) {
try {
var KonyMain = java.import("com.konylabs.android.KonyMain");
var context = KonyMain.getAppContext();
var pm = KonyMain.getAppContext().getPackageManager();
var intent = pm.getLaunchIntentForPackage(packageName);
if(intent !== null && intent!== undefined){
context.startActivity(intent);
}
else {
var url = "https://play.google.com/store/apps/details?id="+packageName;
kony.application.openURL(url);
}
} catch(e) {
kony.print("error opening app external: "+e)
}
}
If i understood correctly you want to do something like kindle which allows the user to open a book (in this case an app) directly from your app.
That being the case, maybe this package can help you https://pub.dev/packages/device_apps
to check for installed apps List<Application> apps = await DeviceApps.getInstalledApplications();
and to launch the app DeviceApps.openApp('package.name');
This package currently only supports android.

Flutter AspNet Core SocketException (SocketException: OS Error: Connection timed out, errno = 110, address = 192.168.1.87, port = 46186)

I'm trying to connect using my smartphone (not emulator) to my Aspnet Core with SignalR backend.
I added Cors to Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy(MyAllowSpecificOrigins,
builder => builder.SetIsOriginAllowed((host) => true)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
services.AddSignalR();
}
readonly string MyAllowSpecificOrigins = "AllowOrigins";
...
app.UseCors(MyAllowSpecificOrigins);
I tried to connect with an Angular frontend and it works well, and I would like to do the same with my Flutter App.
Here's part of the code:
const url = 'http://192.168.1.87:44327/signalrtc';
class _MyHomePageState extends State<MyHomePage> {
final hubConnection = HubConnectionBuilder().withUrl("$url").build();
#override
void initState() {
super.initState();
hubConnection.onclose((_) {
print("Connessione persa");
});
hubConnection.on("ReceiveMessage", onReceiveMessage);
startConnection();
}
void startConnection() async {
print('start');
await hubConnection.start();
}
I'm only using Flutter SignalR package and I copied my local computer IPv4 to connect to my backend, because as I already said I'm not using an emulator so I can't insert 10.0.2.2, but if I try to run the App it will gives me that error.
Both my computer and my smartphone are connected to the same network.
Any idea?
SOLVED
I just installed Conveyor by Keyoti for Visual Studio.
ASP.NET Core - remote access from smartphone
With it you don't have to change any configuration, it just runs in the background and gives you a port you can use remotely, and also it can tunnel to the internet so you have a public URL if you like.
You only need to install it, run in debug mode and opening Keyoti you'll find another URL that you can use for your smartphone.
In the end I changed my URL in the Flutter project from:
const url = 'http://192.168.1.87:44327/signalrtc';
To:
const url = 'https://192.168.1.87:45456/signalrtc';
Basically is my IPv4 given from keyote but with another port.

Upgrade home launcher app from .apk file

I am trying to upgrade home app from .apk file stored in external sdcard. But, the bellow error appears infinity and then the device is re-booted itself.
09-25 11:58:44.040: I/ActivityManager(1313): Force finishing activity ActivityRecord{4238d680 u0 com.jkpark.cluster/.MainActivity}
09-25 11:58:44.040: I/ActivityManager(1313): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000000 cmp=com.jkpark.cluster/.MainActivity} from pid 0
I guess the ActivityManager starts whatever home app when I call event to re-install my home app, but there isn't exist any home app else. So ActivityManager force starting and then calls starting home app again and again.
When I installed just another home app categoried "android.intent.category.HOME" and upgrade my home app, It works fine.
but, I want to upgrade without any other home app installed.
So, anyone helps me please.
Thank you.
I had a similar problem. During the launcher update process android OS has no home activity to run. This happens, because one thread is installing and one thread is trying to run the luncher and the same time. This problem is easy to solve by creating another Launcher, lets call it launcher switcher. This launcher has only one purpose - running your original louncher.
Works like charm for me.
This is a code for launcher switcher.
private void runLauncher() {
if(isMyLauncherDefault()) {
unsetThisLauncherAsDefault();
}
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage(LAUNCHER_PACKAGE);
startActivity(LaunchIntent);
}
}, TWO_SECONDS_IN_MILLISECONDS);
}
private boolean isMyLauncherDefault() {
final IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);
filter.addCategory(Intent.CATEGORY_HOME);
List<IntentFilter> filters = new ArrayList<IntentFilter>();
filters.add(filter);
final String myPackageName = getPackageName();
List<ComponentName> activities = new ArrayList<ComponentName>();
PackageManager packageManager = (PackageManager) getPackageManager();
packageManager.getPreferredActivities(filters, activities, LAUNCHER_SWITCHER_PACKAGE);
if(activities.size() == NO_LAUNCHER)
return true;
for (ComponentName activity : activities) {
if (myPackageName.equals(activity.getPackageName())) {
return true;
}
}
return false;
}
private void unsetThisLauncherAsDefault() {
getPackageManager().clearPackagePreferredActivities(getPackageName());
}
I think u just want to remove system homescreen app and only want ur app as a default launcher..
In that case u have to root your device to remove system app..
In android environment it is not possible to remove system apps without root access.