I have a problem with my app, when user press recent button my app disable close but when user press home button my app will close
protected void onPause() {
super.onPause();
ActivityManager activityManager = (ActivityManager) getApplicationContext()
.getSystemService(Context.ACTIVITY_SERVICE);
activityManager.moveTaskToFront(getTaskId(), 0);
}
And permission
<uses-permission android:name="android.permission.GET_TASKS"/>
<uses-permission android:name="android.permission.REORDER_TASKS" />
I try with onUserLeaveHint only Android 12 work
Related
I want to make an app which will dial keystrings for me in phone dialer.
On clicking button in an app the app goes in background and open phone app and by autoclicking it will dial keystring.
Even if you can auto click call button on different phone are positioned in different positions. If you want to make a call from your app you can just use Intent
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+8802177690));//change the number
startActivity(callIntent);
and add to your Manifest
<uses-permission android:name="android.permission.CALL_PHONE" />
Several Android devices famously the Nexus series have a magnetic sensor that is NOT the android.hardware.Sensor.TYPE_MAGNETIC_FIELD used exclusively to turn the screen ON/OFF automatically using phone cases that have a small magnet inside them.
On my application, I'm using magnet detection using android.hardware.Sensor.TYPE_MAGNETIC_FIELD and the SensorManager to detect user touches the phone in a case with some magnets inside.
The code works without issues, the problem is that it's extremely easy on those devices to accidentally trigger the screen ON/OFF sensor thou, turning off the screen.
I've tried:
android:keepScreenOn="true" on the XML
using the permission android.permission.WAKE_LOCK and acquiring a wake-lock via PowerManager.
Both without success.
Is there a way to temporarily disable this sensor while my activity is resumed?
The keepScreenOn = true in manifest also doesn't work here as the action of hall effect sensor is same as pressing power button.
Here, I offer a work-around that I have tested. In this approach you display your activity regardless of hall-effect sensor locking the device and turning of the display.
Before beginning the unlocking action
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
credits for above code - https://stackoverflow.com/a/45951927/9640177
Also make sure to add Permissions
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
Setting these flags will make sure that your window will be visible above lockscreen.
Now, listen for ACTION_SCREEN_OFF intent. To do this add a broadcast receiver that listens for this intent broadcasted by the system. You need to register the receiver locally. (stackoverflow.com/a/9478013/9640177)
in manifest
<receiver android:name=".receiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"/>
</intent-filter>
</receiver>
Receiver class
public class receiver extends BroadcastReceiver {
MyActivity activity;
receiver(MyActivity activity){
this.activity = activity;
}
#Override
public void onReceive(final Context context, Intent intent) {
activity.turnOnScreen();
}
}
Inside activity
// register receiver
...
IntentFilter screenStateFilter = new IntentFilter();
screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF);
receiver r = new receiver(MyActivity.this);
registerReceiver(r, screenStateFilter);
...
// function to turn on display
public void turnOnScreen(){
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "my:Tag");
wakeLock.acquire();
wakeLock.release();
}
credit - https://stackoverflow.com/a/44706632/9640177
This will turn the display on.
Don't forget to remove these flags after your activity has done the job and does not require to be in forefront.
getWindow().removeFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
This will make your window disappear from lock-screen and make the application function normally.
Hope this helps.
the most pragmatic solution to the problem might be ...
to disable the smart cover feature physically rather than by software.
eg. take a razorblade and tweezers and then remove the magnet.
... or simply get another cover without a magnet.
alternatively, file a bug against drivers/input/lid.c.
I am newbie to native android development. I am working on android studio. I have a tabbed activity in which i am using 2 tabs. In one tab there is a map with marker on gps location of the user. The second tab is to take picture and then to save it in the storage provided by the OS. For this i followed this tutorial, run the app on my device but it's crashes. I am running it on Marshmallow. Below is the error i get
java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE cmp=android/com.android.internal.app.ResolverActivity } from ProcessRecord{e8f9820 15070:com.example.accurat.faisal/u0a228} (pid=15070, uid=10228) with revoked permission android.permission.CAMERA
at android.os.Parcel.readException(Parcel.java:1620)
at android.os.Parcel.readException(Parcel.java:1573)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3181)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1541)
at android.app.Activity.startActivityForResult(Activity.java:4298)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
at android.support.v4.app.ActivityCompatJB.startActivityForResult(ActivityCompatJB.java:30)
at android.support.v4.app.ActivityCompat.startActivityForResult(ActivityCompat.java:146)
at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:937)
at android.support.v4.app.FragmentActivity$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:1047)
at android.support.v4.app.Fragment.startActivityForResult(Fragment.java:954)
at android.support.v4.app.Fragment.startActivityForResult(Fragment.java:943)
at com.example.accurat.faisal.Camera$1.onClick(Camera.java:51)
at android.view.View.performClick(View.java:5716)
at android.widget.TextView.performClick(TextView.java:10926)
at android.view.View$PerformClick.run(View.java:22596)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
I searched it and found that i should ask permission for it so i followed this link and tried to add permission check like as bellow
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Check permission for CAMERA
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
// Check Permissions Now
// Callback onRequestPermissionsResult interceptado na Activity MainActivity
ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission.CAMERA},
MainActivity.REQUEST_CAMERA);
}
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE );
}
});
But i am getting red text of CAMERA and REQUEST_CAMERA. Though i have imported them as well but still the red text is showing
import static android.Manifest.permission.CAMERA;
import static android.Manifest.permission.RECORD_AUDIO;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
Below is my manifest file
<uses-permission android:name="com.example.accurat.faisal.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.hardware.camera"/>
I don't know what is the problem and don't know the solution and i am stuck to it.
Any help would be highly appreciated
You need to add permission check in your code and based on user action of accecpt or denial should show the camera preview Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running. Also, you need to ask user permission for android.Manifest.permission.WRITE_EXTERNAL_STORAGE if you are creating a file which will be used as an input for image(captured via camera).
Hí,
I have an java application that makes use bluetooth to search for devices. When the user presses the native Bluetooth button to turn it off my application shows the bluetooth status offline. When the user presses the native Bluetooth button to turn it on, my application should go back to work but it does not. For my scan back to work I need to close and open my application.
How to fix this programmatically ?
Make sure you have the following permissions in your manifest:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
You can enable bluetooth by using the following in an Activity or Service class:
BluetoothAdapter.getDefaultAdapter().enable();
If you are trying to listen for when Bluetooth is enabled, register a BroadcastReceiver listening for the BluetoothAdapter.ACTION_STATE_CHANGED action:
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
if (state == BluetoothAdapter.STATE_OFF) {
// Bluetooth is turned off
} else if (state == BluetoothAdapter.STATE_ON) {
// Do your scan
}
I'm new in android programming and I don't know how to resolve in detecting access point of my phones hotspot. My application is to connect android devices via same access point, the problem is that whenever I start my phone hotspot, my application doesn't seem to read it as an access point. I want users to connect first in any access point(including my phones hotspot before going to the next activity. here's my code:
MainActivity.java
ImageButton class3 = (ImageButton) findViewById(R.id.multiplayer);
class3.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) { //whenever this button is click
WifiManager wifi = (WifiManager) context.getSystemService(WIFI_SERVICE);
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (wifi.isWifiEnabled() || netInfo != null){ //connected in any access point(including own hotspot) then go to next activity
startActivity(new Intent(MainActivity.this.getBaseContext(), classmultiplayer.class));
}
else {
MainActivity.this.finish(); //to simplified the code i just used exit if its not connected to any acces point
}
}
});
Manifest
<uses-permission android:name="android.permission.GET_TASKS"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
When I am connected to wifi, it works perfectly. but when I start my hotspot on my phone without any wifi connectivity it do "else statement" which is exit. I already tried some answers in stackoverflow and I don't think it solves my problem.
"just want to make sure that users are already connected to an access point before going to the next activity including its own hotspot, because I'm developing an offline multiplayer game and make them communicate with each other via same access points".
there is already an existing android application that can do this, I just want to apply it on my app. Please answer..