Call a number programatically when app is in background - android

How can we perform a call to any number when application is in the background using volume key (number set by own self-using application)?
When I press up the volume key 3 times, I want the app to call my Dad's number even if the app is in background.

Using following code you can achieve it -
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + bundle.getString("mobilePhone")));
context.startActivity(intent);
Also, don't forget to add the permission for the same in Manifest file -
uses-permission android:name="android.permission.CALL_PHONE" />
And lastly you need to check for the volume up button event to trigger the call -
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
down = false;
} else if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){
up = false;
}
return true;
}
Based on the condition you can achieve the same. Thanks :)

Related

Handle alert when shut down device android in rooted app

I want change this message "Your phone is shut down" that appear when shut down the device.
Do you have any ideas?
My app download videos in the background, I must warn you not to switch off the device if the download is not yet finished, such as happens when you shut down Windows and is downloading updates.
Adriana take a look at Intent.ACTION_SHUTDOWN, i am not sure if you can handle like you want.
Additionally this post is related to this.
In AndroidManifest.xml:
<uses-permission android:name="android.permission.PREVENT_POWER_KEY" />
<uses-permission android:name="android.permission.DEVICE_POWER"
tools:ignore="ProtectedPermissions" />
MainActivity.java I intercept the power off button and force close system dialog and create a View custom above it
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_POWER) {
// Do something here...
event.startTracking(); // Needed to track long presses
Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(closeDialog);
mViewCustom.setVisibility(View.VISIBLE);
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_POWER) {
return true;
}
return super.onKeyLongPress(keyCode, event);
}

When I made call through SIP phone, the mobile number edit into my dialed call log that I want to clear, how can I do that?

I have done the following changes into my code. I just want to clear the call log that written by the SIP phone, I have made my SIP phone using SIPDROID.
If I delete the permissions from Android manifest it gives me an error.
All I want is to remove the dialed call logs into the recent calling history.
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
Log.e("Test", "Back button pressed!");
}
else if(keyCode == KeyEvent.KEYCODE_HOME)
{
Log.e("Test", "Home button pressed!");
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}

Make a phone call without clicking phone call button in android

I want to creat an Android app that can get phone number from a text file then make a phone call immediatelly without clicking any extra button. But, i find no way to do that. All samples on internet use the default call button to make a phone.
here is the code i used
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_CALL) {
performDial();
return true;
}
return false;
}
public void performDial(){
if(edittext.getText()!=null){
try {
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + edittext.getText())));
} catch (Exception e) {
e.printStackTrace();
}
}//if
}
Thanks in advance
First, I do not know if you can get the KEYCODE_CALL event or not in onKeyDown().
Second, use ACTION_CALL instead of ACTION_DIAL. You will need to hold the CALL_PHONE permission for this to work.
It is very Simple. Do it like this ---->
Fire an intent ---->
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("Tel:(+12)123456789"));
startActivity(intent);
Add this permission to your manifest file ----->
And your ready to go....

onClick HOME key display alert box in android

I am sincerely trying to display alert box onClick of Home key, but I am not succeeded. So please help me to write the code otherwise, show me the code.
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK )
{
finish();
return false;
}
else if (keyCode == KeyEvent.KEYCODE_HOME)
{
finish();
return false;
}
return super.onKeyDown(keyCode, event);
}
it is not possible to intercept KEYCODE_HOME by external applications:
http://code.google.com/p/android/issues/detail?id=1979
I don't think you can intercept HOME key presses. It is reserved to ensure that you don't get locked within any application.
KEYCODE_HOME : This key is handled by the frame work and is never delivered to applications.
So you can't use it

Confused about Android key event handling. Any good explanations?

I'm a relative beginner with Android. Does anybody have a sane explanation for how to listen for keys and soft keys in an EditText/TextView?
I'd love to see a comprehensive tutorial or set of examples.
As I understand it, I can add a KeyListener to my Activity, e.g. onKeyDown(), onKeyUp() but when I try this I can't trigger the events for normal keys only HOME and BACK for example.
I have seen mention of using a TextWatcher but that isn't the same as handling raw key events.
There seem to be a number of half-solutions here on SO. Hoping you can help clear the mists of confusion...
You have to assign a key listener not to activity but rather to EditText itself.
This is what I have to listen to BACK or MENU key events. Simply add this method, without implementing any Interface. I do this in my BaseActivity, from which every Activity inherits.
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(NAME, "Key pressed");
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
Log.d(NAME, "Back pressed");
// IGNORE back key!!
return true;
/* Muestra el MenĂº de Opciones */
case KeyEvent.KEYCODE_MENU:
Intent menu = new Intent(this, Menu.class);
// start activity
startActivity(menu);
return true;
}
return super.onKeyDown(keyCode, event);
}
PS: I highly discourage ignoring the back key.
For example:
myEditText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
//your code here
}
return false;
}
});
I recently found another way that be stuck using Activity onKeyDown, or event setting a key listener on view (which is not really working with key events from ADB in my case) with view.setOnKeyListener.
Since android P method addOnUnhandledKeyEventListener has been introduced. It allows you to do whatever you need to do when your view is able to catch unhandled key events.
Here is an example of how I used it :
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) yourView.addOnUnhandledKeyEventListener { v, event ->
when (event.keyCode) {
KeyEvent.KEYCODE_UNKNOWN -> {
TODO("Do whatever you need to do.")
true // Specify you did handle the event
}
KeyEvent.KEYCODE_SOFT_RIGHT -> {
TODO("Do whatever you need to do.")
true // Specify you did handle the event
}
// etc...
else -> false // Specify you didn't handle the event
}
}

Categories

Resources