I try to disable screenshot in android with flutter.
I try this :
class MainActivity: FlutterFragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this)
//not working
getActivity().getWindow().addFlags(LayoutParams.FLAG_SECURE)
}
}
not sure but try this...
try removing getActivity()
getWindow().addFlags(LayoutParams.FLAG_SECURE)
i hope it helps...
I was able to get access to the activity (and then window) using from the Registrar via registrarFor
Registrar registrar = registrarFor(null);
Activity activity = registrar.activity();
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
Feels super hacky, but it appears to work.
Related
Seems like a simple question, but I haven't been able to find a simple answer. Essentially I want to choose which page in the app to start on based on some stored state. I added a GoToAsync call in the AppShell constructor, but this didn't work--which makes sense because the AppShell hasn't been fully constructed yet.
I found this answer, but it feels like it kind of skirts around the issue:
Maui AppShell - Navigate on Open
Where is the best place to inject some code that will run once on startup and can successfully navigate a .NET Maui app to a chosen page?
After playing around with overrides, it seems like overriding Application.OnStart works! Shell.Current is set at this point and navigation works.
Here's additional code that allows for asynchronous initialization and uses a Loading Page until the initialization is complete:
using MyApp.Services;
using MyApp.UI;
namespace MyApp;
public partial class App : Application
{
ConfigurationProviderService m_configProvider;
public App(ConfigurationProviderService configProvider)
{
m_configProvider = configProvider;
InitializeComponent();
MainPage = new LoadingPage();
}
protected override void OnStart()
{
var task = InitAsync();
task.ContinueWith((task) =>
{
MainThread.BeginInvokeOnMainThread(() =>
{
MainPage = new AppShell();
// Choose navigation depending on init
Shell.Current.GoToAsync(...);
});
});
base.OnStart();
}
private async Task InitAsync()
{
await m_configProvider.InitAsync();
}
}
at the moment I have a Flutter App that when I press a button it opens a Native Kotlin activity(mainActivity), once this executes I automatically open a SecondActivity that extends AppCompatActivity, the issue becomes is that I want to return a bool from the SecondActivity back to Flutter.
At the moment when the process finishes i use "finish()" to close my Native code and return to flutter successfully but I'd like to pass a result to Flutter.
I know how to do it from the mainActivity as it uses the FlutterActivity directly and can pass "result.success()", but from the different activity I can't find a solution.
I was wondering if somebody can tell me how to pass the bool from the secondActivity to Flutter.
Thank you very much everyone in advance!
It works exactly the same. Store Result in class property and call one of its methods from onActivityResult when the second activity finishes. Something like this:
class YourPlugin ... {
...
private var pendingResult: Result? = null
...
override fun onMethodCall(#NonNull call: MethodCall, #NonNull result: Result) {
...
pendingResult = result
activity?.startActivityForResult(...)
...
}
override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?): Boolean {
...
pendingResult?.success(<result>)
...
return false
}
I have implemented the notificationServiceExtension as mentioned by onesignal in it's documentation but now I am not being able to run background notification handler in one signal.
I have implemented the following code in main activity:::
package com.example.just_normal
import com.onesignal.OneSignal
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
private val methodchannel = "background/notification";
class MainActivity: FlutterActivity() {
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
OneSignal.OSRemoteNotificationReceivedHandler { context, osNotificationReceivedEvent ->
val CHANNEL : MethodChannel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger , methodchannel)
CHANNEL.invokeMethod("onBackgroundNotification" , osNotificationReceivedEvent);
// osNotificationReceivedEvent.complete(null);
}
}
}
In the above code I am trying to call the flutter method immediately after the notification has been received in the background. How can I run flutter method immediately after notification is received in backgound. Please help....
use this in the init function where you are registering onesignal in the app:
OneSignal.shared.setNotificationWillShowInForegroundHandler((OSNotificationReceivedEvent event) {
event.complete(event.notification);
});
Now you will be able to get notifications even when the app got killed.
My goal is to select a picture directly from phone's Gallery and use it in my app, like you would share it with other apps like WhatsApp, Mail, Messenger..etc etc.
I need to:
Select the picture from gallery.
Select my app from the share dialog.
Launch a specific screen passing in the picture.
I've seen a lot of sharing packages but I'm not looking to load a picture by accessing the gallery from my app, but the other way around.
This package https://pub.dev/packages/receive_sharing_intent seems to do what I need but not in a very transparent and native looking way.. as instead of app icon and name, in the share dialog it shows the iOS extension created for this plugin..
In this post How do I share an image on iOS and Android using Flutter? I see Simon's answer could be the solution, but trying to implement it I'm getting quite a few errors after pasting the Kotlin sample code into MainActivity.kt file. I guess it has to be updated.
This is my MainActivity.kt file with added code from Simon's answer:
package com.vinny.fixit_cloud_biking
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant
class MainActivity: FlutterActivity() {
override fun configureFlutterEngine(#NonNull flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this)
MethodChannel(flutterView,"channel:me.albie.share/share").setMethodCallHandler { methodCall, _ ->
if (methodCall.method == "shareFile") {
shareFile(methodCall.arguments as String)
}
}
}
private fun shareFile(path:String) {
val imageFile = File(this.applicationContext.cacheDir,path)
val contentUri = FileProvider.getUriForFile(this,"me.albie.share",imageFile)
val shareIntent = Intent()
shareIntent.action = Intent.ACTION_SEND
shareIntent.type="image/jpg"
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri)
startActivity(Intent.createChooser(shareIntent,"Compartir usando"))
}
}
Errors onCreate():
Cannot access 'androidx.lifecycle.lifecycleOwner' which is a super type of 'com.vinny.fixit_cloud_biking.MainActivity'. Check your module class path for missing or conflicting dependencies.
onCreate: overrides nothing
Errors MethodChannel:
Cannot access 'androidx.lifecycle.lifecycleOwner' which is a super type of 'io.flutter.embedding.android.FLutterActivity'. Check your module class path for missing or conflicting dependencies.
Unresolved reference: flutterView
As always many thanks for your help.
I know these libraries flutter_isolate and isolate_handler exist that support doing these but I couldn't find any method to call platform specific codes from inside of them. Can someone show any example of how it's done ?
My answer might not answer to your question directly but I was facing the same situation.
I was working on this to run a computing intensive task avoiding UI lagging. After a long research, no, you cannot run Platform from other isolates rather than the main one.
Instead of making and running the native code on another isolate, let's make the native code run in background instead with TaskQueue.
Channels and platform threading
The example code in the above link is using onAttachedToEngine, you can check it out. However, I was using is configureFlutterEngine so I have to figure out a little bit until I found a solution that I need binaryMessenger to make it working. Luckily it can called from flutterEngine too!
This is the example code of when using configureFlutterEngine
class MainActivity: FlutterFragmentActivity() {
val MOBILE_SDK_CHANNEL = "com.example.app/flutter"
override fun configureFlutterEngine(#NonNull flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
super.configureFlutterEngine(flutterEngine)
val taskQueue = flutterEngine.dartExecutor.binaryMessenger.makeBackgroundTaskQueue()
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, MOBILE_SDK_CHANNEL,
StandardMethodCodec.INSTANCE, taskQueue).setMethodCallHandler { call, result ->
// TODO : Do implementation here
when (call.method) {
"example_method" -> {
// success
// result.success(data)
}
else -> result.notImplemented()
}
}
}
}
With taskQueue the native code will be ready to run in background.
Future<void> getData() async {
if (Platform.isAndroid) {
const platform = MethodChannel("com.example.app/flutter");
var data = await platform.invokeMethod("example_method");
// data is ready to use and non-blocked the UI thread
return data
}
}
The native code now runs in non-blocking manner, no more UI lag. :)