Ionic4 hardware mobile back buttion issue - ionic-framework

if click on the back button to close ionic4 app in android mobile getting following error
code to close the app:
ionViewDidEnter() {
this.subscription = this.platform.backButton.subscribe(async () => {
navigator['app'].exitApp();
});
}
ionViewWillLeave() {
this.subscription.unsubscribe();
}
error:
E/Capacitor/Plugin/Console: ERROR Error: Uncaught (in promise): Error: StaticInjectorError[t -> t]:
StaticInjectorError(Platform: core)[t -> t]:
NullInjectorError: No provider for t!
Error: StaticInjectorError[t -> t]:
StaticInjectorError(Platform: core)[t -> t]:
NullInjectorError: No provider for t!

Please refer to this snippet workaround
this.platform.backButton.subscribe(() => {
// code that is executed when the user pressed the back button
})
// To prevent interference with ionic's own backbutton handling
// you can subscribe with a low priority instead
this.platform.backButton.subscribeWithPriority(0, () => {
// code that is executed when the user pressed the back button
// and ionic doesn't already know what to do (close modals etc...)
})
PS: Hope you'd check whether platform module is imported or not.
import { Platform } from '#ionic/angular';
#Component({...})
export class MyPage {
constructor(public plt: Platform) {
}
}

Related

Flutter get android screen DPI inside fragment activity?

I have a flutter activity which gets screen DPI which works fine, but i need to make it a FlutterFragmentActivity because i need to use local_auth plugin inside the app, i've tried something like this:
class MainActivity: FlutterFragmentActivity() {
// Native android channel used to get the screen DPI.
// Using a lot of code from the flutter docs: https://docs.flutter.dev/development/platform-integration/platform-channels?tab=android-channel-kotlin-tab
private val CHANNEL = "screenDimensions"
override fun configureFlutterEngine(#NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
// Note: this method is invoked on the main thread.
call, result ->
if(call.method == "getScreenDPI") {
val screenDPI = getScreenDPI()
result.success(screenDPI)
}
else {
result.notImplemented()
}
}
}
// Code from
// https://stackoverflow.com/a/15699681
private fun getScreenDPI(): Double {
val w = activity.windowManager
val d: Display = w.defaultDisplay
val metrics = DisplayMetrics()
d.getMetrics(metrics)
val dpi = metrics.densityDpi
return dpi.toDouble();
}
}
but now it doesn't recognize activity anymore and throws this exception: `MainActivity.kt: (39, 17): Unresolved reference: activity
FAILURE: Build failed with an exception.
What went wrong:
Execution failed for task ':app:compileDebugKotlin'.
A failure occurred while executing org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction
Compilation error. See log for more details`
Does anyone know how to keep the getDPI working and enable local_auth at the same time?
Any help appreciated.

Flutter: how can I permanently register a sensor (and never unregister it?)

TL;DR how can I have an Android sensor permanently running/active/registered for my app, even if I close it?
Objective:
I'm making a Flutter application that counts your steps using the pedometer package,
which uses the built-in sensor TYPE_STEP_COUNTER of Android,
which returns the # of steps taken since last boot (iOS). On Android, any steps taken before installing the app are not counted.
How I implemented it:
When the app is actively running in the foreground, each step causes
a myStepCount to increment by 1.
In all other cases (phone locked, went to home-screen, closed the app...), the android TYPE_STEP_COUNTER sensor should
still be running in the background, and once I open my app again, the
difference between new stepCount and last saved stepCount (saved
using shared_prefs) will be calculated and added to myStepCount.
Important:
The TYPE_STEP_COUNTER sensor must be permanently running/stay registered in the background, even after I lock my phone, go to the home-screen, or close the app...
Observations:
On my Samsung Galaxy A02s, my app works perfectly fine, as it it supposed to
(as described above). That is because on that phone I also have the
Google Fit app installed, which tracks your steps 24/7 (so the
TYPE_STEP_COUNTER sensor is permanently registered).
On my Samsung Galaxy S7, my app does not work as it's supposed to.
myStepCount gets incremented when I take steps while the app is
running in the foreground. But steps taken while the app is closed
will NOT be added to myStepCount once I open the app again.
Note: I don't have any other step-counting-apps like Google Fit on this phone.
Conclusion:
I need to find a way to register the TYPE_STEP_COUNTER sensor from my Flutter app, and keep it registered even after I close the app.
2 Attempted (but unsuccessful) Solutions:
1st Attempt:
Calling Native Android Code from my Flutter Code to register the sensor
This is my main.dart file (with the unimportant parts left out for simplicity):
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(App());
}
class App extends StatefulWidget {
#override
_AppState createState() => _AppState();
}
class _AppState extends State<App> with WidgetsBindingObserver {
#override
void initState() {
super.initState();
if (Platform.isAndroid) {
_activateStepCounterSensor();
} else if (Platform.isIOS) {
//TODO check if anything is needed to to here
}
}
void _activateStepCounterSensor() async {
MethodChannel _stepCounterChannel = MethodChannel('com.cedricds.wanderapp/stepCounter'); //convention
dynamic result = await _stepCounterChannel.invokeMethod('activateStepCounterSensor');
switch (result) {
case "success":
//The following line gets printed when I run the flutter app on my Samsung Galaxy S7:
print('_activateStepCounterSensor(): successfully registered step counter sensor for android');
break;
case "error":
print('_activateStepCounterSensor(): failed to register step counter sensor (not available) for android');
//TODO display errorpage (because app is completely useless in this case)
break;
default:
print('_activateStepCounterSensor(): unknown result: $result');
break;
}
}
//build() and other lifecycle-methods and helper methods: not important for this question
}
This is my MainActivity.kt file:
package com.cedricds.wanderapp
import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.util.Log
import android.widget.Toast
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
class MainActivity2: FlutterActivity(), SensorEventListener {
private val STEP_COUNTER_CHANNEL = "com.cedricds.wanderapp/stepCounter";
private lateinit var channel: MethodChannel
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
channel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, STEP_COUNTER_CHANNEL)
channel.setMethodCallHandler { call, result ->
when(call.method){ //this is like switch-case statement in Java
"activateStepCounterSensor" -> {
activateStepCounterSensor(result)
}
}
}
}
private var sensorManager : SensorManager?=null
private var sensor: Sensor ?= null
private fun activateStepCounterSensor(result: MethodChannel.Result) {
//This line gets printed when I run the flutter app, so the method gets called successfully:
Log.d("Android", "Native Android: activateStepCounterSensor()")
sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
sensor = sensorManager?.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)
if (sensor == null) {
Toast.makeText(this, "missing hardware.", Toast.LENGTH_LONG).show()
result.error("error", "error", "error")
} else {
sensorManager?.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL)
//This line gets printed:
Log.d("Android", "Native Android: registered TYPE_STEP_COUNTER")
//and never unregister that listener
result.success("success")
}
}
override fun onSensorChanged(p0: SensorEvent?) {}
override fun onAccuracyChanged(p0: Sensor?, p1: Int) {}
}
Despite the few print(...) and Log.d(...) being printed in the console as expected, the app doesn't work how I expected it to work. When I exit the app, walk for example 50 steps, then open the app again, those 50 steps are missing. It seems the sensor is being unregistered somewhere.
2nd Attempt:
Modifying the pedometer package's code by removing unregisterListener(...):
The only changes I did to the file were 2 Log.d(...) statements and more importantly, commenting out a specific line of code.
modified SensorStreamHandler.kt from the pedometer package:
package com.example.pedometer
import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.Looper
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.EventChannel
import android.os.Handler
import android.util.Log
class SensorStreamHandler() : EventChannel.StreamHandler {
private var sensorEventListener: SensorEventListener? = null
private var sensorManager: SensorManager? = null
private var sensor: Sensor? = null
private lateinit var context: Context
private lateinit var sensorName: String
private lateinit var flutterPluginBinding: FlutterPlugin.FlutterPluginBinding
constructor(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding, sensorType: Int) : this() {
this.context = flutterPluginBinding.applicationContext
this.sensorName = if (sensorType == Sensor.TYPE_STEP_COUNTER) "StepCount" else "StepDetection"
sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager
sensor = sensorManager!!.getDefaultSensor(sensorType)
this.flutterPluginBinding = flutterPluginBinding
}
override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {
Log.d("Pedometer", "Native Android: onListen()")
if (sensor == null) {
events!!.error("1", "$sensorName not available",
"$sensorName is not available on this device");
} else {
sensorEventListener = sensorEventListener(events!!);
sensorManager!!.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
}
override fun onCancel(arguments: Any?) {
Log.d("Pedometer", "Native Android: onCancel()")
//The only change I did: commenting out the following line:
// sensorManager!!.unregisterListener(sensorEventListener);
}
}
This also did not solve my problem. So if someone knows how I can permanently register the TYPE_STEP_COUNTER sensor in my flutter app, please let me know.
Update:
I've contacted one of the developers of the pedometer package, and he suggested me to use flutter_foreground_service (which is developed by the same team/company as pedometer). It works.
But I would still find it interesting, if there is another way (maybe similar to my 2 failed attempts).

Is there a way to show a warning dialog when closing a browser tab running a flutter app?

I would like to indicate to the user that there's a file being downloaded when they try to close the tab in my flutter web app. Is there a way of hooking into the app to detect this behavior and to show such a warning message?
You may register code for a BeforeUnloadEvent like this:
import 'dart:html' as html;
/// Subscription on application termination warning
StreamSubscription? _onBeforeUnloadSubscription;
//enable warning for closing browser tab:
_onBeforeUnloadSubscription = registerOnBeforeUnload("Transfers in progress!");
//disable warning for closing browser tab:
stopBeforeUnloadHandler();
StreamSubscription? registerOnBeforeUnload(String warningMessage) {
StreamSubscription<html.BeforeUnloadEvent> _onBeforeUnloadSubscription;
_onBeforeUnloadSubscription = html.window.onBeforeUnload.listen((e) async {
(e as html.BeforeUnloadEvent).returnValue = warningMessage;
return Future.value(warningMessage);
}) as StreamSubscription<html.BeforeUnloadEvent>;
return _onBeforeUnloadSubscription;
}
void stopBeforeUnloadHandler() {
if (_onBeforeUnloadSubscription != null) {
_onBeforeUnloadSubscription!.cancel();
_onBeforeUnloadSubscription = null;
}
}

Get and show live data from cordova plugin in ionic 4 page

im trying to code a simple step counter app, im using this plugin (https://github.com/leecrossley/cordova-plugin-pedometer) in android to get the step counter data, so i want to get this data and show it on my ionic page (front end view), i tryed to use ngZone to refresh the object while the method subscribe to the startPedometerUpdates function, but it takes many seconds to work and sometimes stucks few seconds and then start to show the counter again...
this is my code:
steps: any = []
constructor(private ngZone: NgZone, private pedometer: Pedometer) { }
ngOnInit() {
}
getSteps(){
this.pedometer.startPedometerUpdates()
.subscribe((data: IPedometerData) => {
this.ngZone.run(() => this.steps.push(data))
});
}
my html is simple for debug the data:
<ion-content>
{{this.steps | json}}
<ion-button (click)="getSteps()">Show steps</ion-button>
</ion-content>
so i want to show the data in "real time" with something simple as posible...
thanks in advance
you can't push data in subscribe even if you using NgZone bcoz push is a function ngZone doesn't work in the function
steps: any = []
constructor(private ngZone: NgZone, private pedometer: Pedometer) {
this.getSteps();
setInterval(()=>{
console.log('read in a sec')
},1000)
}
ngOnInit() {
}
getSteps(){
this.pedometer.startPedometerUpdates()
.subscribe((data: IPedometerData) => {
this.ngZone.run(() => this.steps.push(data))
});
}

How to override hardware back button action in Ionic 3?

I want to know which function is called when we click ion-navbar back button by default in ionic 3.
I want to call the same function on hardware back button click.
You can use registerBackButtonAction of Platform Service.
You can override hardware back button action as below inside app.component.ts.
Remember to call registerBackButtonAction after Platform.ready().
import { Platform, App } from 'ionic-angular';
#Component({
templateUrl: 'app.html'
})
export class MyApp {
constructor(public platform: Platform, private app: App) {
this.platform.ready().then(() => {
this.platform.registerBackButtonAction(() => {
let nav = this.app.getActiveNav()
if (nav.canGoBack()) {
// If there are pages in navigation stack go one page back
// You can change this according to your requirement
nav.pop();
} else {
// If there are no pages in navigation stack you can show a message to app user
console.log("You cannot go back");
// Or else you can exit from the app
this.platform.exitApp();
}
});
});
}
}
Hope this will help you.