I'm building an application for web/Android with Vue+Capacitor. What's the correct way to manipulate the Android part?
For example, I'd like the screen to not turn off through idle. Apparently, the way to do this is to put
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
in the MainActivity. But since the Android part gets built again all the time it doesn't really make sense to edit it directly (or maybe I'm mistaken here somehow?)
So how do I achieve something like this?
Edit for clarification:
This is the MainActivity:
import com.getcapacitor.BridgeActivity;
public class MainActivity extends BridgeActivity {}
As you can see it does absolutely nothing besides extending BridgeActivity
This is the (generated!) onCreate of BridgeActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bridgeBuilder.setInstanceState(savedInstanceState);
getApplication().setTheme(getResources().getIdentifier("AppTheme_NoActionBar", "style", getPackageName()));
setTheme(getResources().getIdentifier("AppTheme_NoActionBar", "style", getPackageName()));
setTheme(R.style.AppTheme_NoActionBar);
setContentView(R.layout.bridge_layout_main);
/* The initally mentioned line here works but gets overwritten*/
}
You can just use this community plugin: https://github.com/capacitor-community/keep-awake.
This way you don't have to change the native code.
import { KeepAwake } from '#capacitor-community/keep-awake';
const keepAwake = async () => {
await KeepAwake.keepAwake();
};
const allowSleep = async () => {
await KeepAwake.allowSleep();
};
Once you run npx cap add android, the native project gets created and it’s never modified by capacitor, so you can make any changes you want in the MainActivity.java file or any other files (except the ones that start with “automatically generated, do not edit” or similar messaging).
Related
I tried implementing react-native-draggable-flatlist to my project, but I'm not able to drag the elements. I am able to "pick them" (They get bigger if I press them), but I can't move them to another position.
I had this problem before and found a working soloution in a GitHub issue, but as it seems, MainActivity.java has changed in some version and I don't know how to correctly upgrade it.
The soloution I found:
When I add the #Override for the ReactActivityDelegate, my App crashes after the build.
I noticed that my MainActivity.java looks a little bit different, it seems like the ActivityDelegate is Managed through a class called MainActivityDelegate which extends ReactActivityDelegate:
public static class MainActivityDelegate extends ReactActivityDelegate {
public MainActivityDelegate(ReactActivity activity, String mainComponentName) {
super(activity, mainComponentName);
}
#Override
protected ReactRootView createRootView() {
ReactRootView reactRootView = new ReactRootView(getContext());
// If you opted-in for the New Architecture, we enable the Fabric Renderer.
reactRootView.setIsFabric(BuildConfig.IS_NEW_ARCHITECTURE_ENABLED);
return reactRootView;
}
}
It seems like I have to change something here to achieve the same result, but I don't really know what.
EDIT:
According to the RNGH docs you shouldn't change your MainActivity.java that way anymore and wrap your whole App into a GestureHandlerRootView. Some Libaries like react-native-navigation do this by default (in my case). BEcause of that (I think) my App gets completely grey when I use a GestureHandlerRootView. If I don't addd it myself, so I use the one from neavigaion, I still can't drag the items in my list.
I fixed it by wrapping my screen component in a GestureHandlerRootView
I need a way to make a button or a container that can still be visible even when I close the app like so: . But this is just simply impossible in flutter. So what I've been doing is making a platform channel on my app and trying to make the container and button with native components. But I've been using kotlin and do not know much about this programming language. Is there a way for my code to be able to make such widgets?(I would thank you so much if you could edit my full code.)
Full Code:
Flutter:
class FloatingContainer extends StatelessWidget {
static const platform = const MethodChannel('flutter.App.com.channel');
#override
Widget build(BuildContext context) {
return Container ();
}
Future<Null> _showNativeView() async {}
}
Kotlin:
package com.example.swipe
import android.os.Bundle
import io.flutter.app.FlutterActivity
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.plugin.common.MethodChannel
class MainActivity() : FlutterActivity() {
private val CHANNEL = "flutter.App.com.channel"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this)
MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result ->
}
}
}
This is known as a Draw Over Other apps.
This feature is not available in the iOS probably that's the reason flutter doesn't has this feature or official package for it.
But to achieve this feature in your application you can write up some PlatformChannels.
This permission allow you to draw on top of everything.
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Allows an application to open windows using the type
TYPE_SYSTEM_ALERT, shown on top of all other applications.
Very few applications should use this permission; these windows are intended
for system-level interaction with the user.
Constant Value: "android.permission.SYSTEM_ALERT_WINDOW"
This code can also help you out to achieve this.
You can also check out springy-heads library by Flipkart-Incubator too.
Hello in this case you are searching for PlatformChannels You can review the documentation here: https://flutter.dev/docs/development/platform-integration/platform-channels or if you want i have a little post about this on my website: http://fjbatresv.com/flutter-tambien-habla-nativo/ (Select your language on the top of the post.)
Apart from restriction that it won't work on IOS, there is a plugin in flutter called System Alert Window, which does the same
It shows Truecaller like overlay window, over all other apps along with callback events
Is there any library which has ability like moveTaskToBack in React Native?
Previously I use https://github.com/jaysoo/react-native-activity-android and it has moveTaskToBack. But unfortunately this repo is not active anymore, and since React Native 0.29, it has some internal change which make that library didn't work.
I'm not sure it's what you need, but if you put this in your MainActivity.java class:
#Override
public void invokeDefaultOnBackPressed() {
// do not call super.invokeDefaultOnBackPressed() as it will close the app. Instead lets just put it in the background.
moveTaskToBack(true);
}
Then when user press the android back button on the "root" page, the app will go in background instead of being closed.
Source: background a react-native android app using back button
Use react-native-navigation, then navigate to your projects node modules folder and locate the react-native-navigation folder and inside it, follow the path to the NavigationActivity.java:
/android/app/src/main/java/com/reactnativenavigation/controllers/NavigationActivity.java.
In the NavigationActivity.java file, you will see the following method at around line 196:
#Override
public void invokeDefaultOnBackPressed() {
if (layout != null && !layout.onBackPressed()) {
super.onBackPressed();
}
}
Comment out the line: super.onBackPressed() and add the line: this.moveTaskToBack(true);
and thats all! Happy coding
I'm developing my team along with an application that must run in the background when an event called by sockets should put the application in the foreground .
The application must come to foreground similar to viber or whatsapp call. I stopped at this point. My application can now call an audio and vibrate, but I have to draw the screen to the foreground.
I'm using version 5.1.1 phonegap.
I am this plugin: https://github.com/katzer/cordova-plugin-background-mode
Could someone give me a hand? Very grateful this already.
I found a way! Using the "toForeground" plugin. https://github.com/caioladislau/cordova-toforeground
cordova.plugins.backgroundMode.enable();
cordova.plugins.backgroundMode.onactivate = function() {
setTimeout(function(){
toForeground("MainActivity", "com.me.myapp", function() {
navigator.notification.vibrate(1000);
}, function(){
navigator.notification.vibrate(5000);
});
}, 4000);
};
Note where it is called in:
toForeground(mainClassName, packageName, successFunction, errorFunction);
To find the "mainClassName" and "packageName" I searched: platforms/android/src/com/me/myapp/MainActivity.java, and I found:
package com.me.myapp;
import android.os.Bundle;
import org.apache.cordova.*;
public class MainActivity extends CordovaActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Set by <content src="index.html" /> in config.xml
loadUrl(launchUrl);
}
}
I have used this plugin instead of developing one for my own, its a forked repo
https://github.com/suhail339/cordova-bring-to-front
Keep in mind to install from Cordova CLI, visual studio plugin
installer might create problem.
I'm working on an Xamarin Android app using MVVMCross and have (only for android) multiple screens/activities I want to start from.
I tried to duplicate the SplashScreen, but then none of the Activities boot anymore.
Any suggestions how to get multiple Activities with MainLauncher=true workable?
You should add a AppStart.cs to your core project and add this function:
public async void Start(object hint = null)
{
if (CheckSomething == true)
ShowViewModel<ViewModels.FirstViewModel>();
else
ShowViewModel<ViewModels.SecondViewModel>();
}
Then in your App.cs do:
public override void Initialize()
{
RegisterAppStart(new AppStart());
}