Android-Telephony application that keeps focus on incoming calls - android

I am developing a custom telephony application that is able to receive calls.
Using this code for handling the incoming call
https://code.google.com/p/auto-answer/source/browse/trunk/src/com/everysoft/autoanswer/AutoAnswerIntentService.java
Unfortunately my app loses focus on the incoming call.
THIS was a partial solution for outgoing calls
Android- Telephone app that keeps focus on outgoing & incoming phoneCall
What about incoming calls? How do I keep focus in my custom app?
I am guessing this might involve downloading and modifying the source code as simply accessing the SDK gives little control over the built-in phone application.

Since the reference you made about outgoing calls is acceptable, then you can place an activity in front of the incoming call screen shortly after it displays. The difficulty in doing this is that the call state will change to "RINGING" and then also "OFFHOOK" but the phone has not displayed the InCallScreen when these are broadcast.
Like the post you referenced, this solution does not actually embed the phone feature into the app (like a webview for web browsing) but rather places an activity in front of the InCallScreen shortly after it displays.
For incoming calls, you need to delay the launch of your activity, like in this post:
Android - Customised New Incoming Call Screen
You can put anything on the screen at the point, the hard part is determining the lag time so that it meets your needs (slow enough so that the InCallScreen has a chance to launch but fast enough to be minimally disruptive).
Beyond that, even extending AOSP will not help unless you have access to each physical device where this will be used to root them or put a custom build on them. Access to the PhoneApp features is not accessible to non-system apps (the com.android.phone package).

Mention the below broadcast receiver in manifest.xml file.
<receiver android:name="com.example.incomingcall.IncomingCallReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
IncomingCallReceiver.java:
public class IncomingCallReceiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Thread thread = new Thread(){
private int sleepTime = 400;
#Override
public void run() {
super.run();
try {
int wait_Time = 0;
while (wait_Time < sleepTime ) {
sleep(100);
wait_Time += 100;
}
}catch (Exception e) {
Toast.makeText(context,
"Error Occured Because:" + e.getMessage(),
Toast.LENGTH_SHORT).show();
}
context.startActivity(new Intent(context,CustomActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
};
thread.run();
}
}
}
}

Related

Remote Location Access App

So last week my phone got stolen and the wifi and mobile data were turned off so I had no access to it. GPS was turned off as well but that's of no use if the phone doesn't have an internet connection. Even though the phone was protected by a fingerprint and my data would be safe, I still would've loved that phone back. Thinking about this I came up with an idea.
What if I create an Android app that would run silently in the background on my (new)phone and would do absolutely nothing. All it would do is listen for a certain string of characters in all the SMS the phone receives.
Now say my new phone gets stolen too (talk about tough luck). I immediately call at my number from someone's phone. If the phone is still turned on and has cellular connectivity, I would send an SMS with THAT string of characters to my number.
As soon as the phone receives the SMS, the app picks up on it and does the following:
Turns on GPS.
Gets a lock on its position.
Starts sending its own coordinates to the number it received the SMS from at
a rate of once every minute via SMS.
This way I get to know where my phone is and the thief won't know whats happening because everything is being done silently and the phone is locked as well. The main thing I want to cover with this app is the fact that this doesn't rely on an internet connection. It just trusts the fact that only I know the certain string of characters and that the app exists and is running 24/7. I know this isn't suitable for public use but for personal use, its ok I guess.
What I want to ask for is advice on what approach should I take? Any suggestions about things I should add? Have you ever tried something like this and if so, then can you share your experience with me?
Thanks in advance!
I have done in my project. Hope it will help you. Ask if it not clear to you. First you need to create SMSReceiver class for receiving sms into application which extend broadcast receiver & SMSListener interface for listening the SMS data and pass it to your desired activity.
Here is the code:
public class SmsReceiver extends BroadcastReceiver {
private static SmsListener mListener;
#Override
public void onReceive(Context context, Intent intent) {
Bundle data = intent.getExtras();
Object[] pdus = (Object[]) data.get("pdus");//pdus is the protocol of SMS data.
for(int i=0;i<pdus.length;i++){
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
String sender = smsMessage.getDisplayOriginatingAddress();
//You must check here if the sender is your provider and not another one with same text.
String messageBody = smsMessage.getMessageBody(); //SMS text data
mListener.messageReceived(messageBody);//Pass on the text to our listener.
}
}
public static void bindListener(SmsListener listener) {
mListener = listener;
}
public static void unbindListener() {
mListener = null;
}
public interface SmsListener {
void messageReceived(String messageText);
}
}
Then you need to declare your receiver in manifest file. Like below.
<receiver android:name=".SmsReceiver" android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
Then finally in your activity add this code for getting SMS data.
public Pattern p = Pattern.compile("(|^)\\d{4}"); //It will detect 4 number OTP value.
SmsReceiver.bindListener(new SmsListener() {
#Override
public void messageReceived(String messageText) {
Log.d("MESSAGE TEXT",messageText);
if(messageText != null)
{
Matcher m = p.matcher(messageText);
if(m.find()) {
//Do your code here after your sms received.
}
else
{
//Do here if you want to trigger anyof sms received.
}
}
}
});
BroadcastReceiver always listening for the incoming SMS. If it fails to trigger at app close or screen close state or always running issue look at this solution will help you.
Keep broadcast receiver running after application is closed
EDITED
Have a look at some workground scenario of never end service. All you need is monitoring the service & restart if killed. Also you can store the status of service in shared preference as well. Here some of the options & workcode for never end service. You can find more on search.
Android: keep Service running when app is killed
Creating a never ending background service in Android
Hope it will helps.

Android- Telephone app that keeps focus on outgoing & incoming phoneCall

Using this simple example to create a PhoneCall application that dials out a hard coded # and monitors phone state.
http://www.mkyong.com/android/how-to-make-a-phone-call-in-android/
Unfortunately, on making the phone call, we always switch to the actual built -in phone application.
I want to avoid this, or at the very least hide the dialer pad button. The user SHOULD NOT have the option to enter a phone#.
Does anyone know of a way to achieve this?
i.e. keep the actual built-in phone application in the background
(I would need to add buttons for speaker, and end call in the primary application)
OR
alternatively, hide just the dial pad button in the native, built-in phone application?
Here is a solution I came up with to hide the caller app shortly after the call is placed. I don't believe there is a way to make it totally transparent without re-writing the Android system. I believe this could be improved by detecting when the caller app is set up and dialing instead of the postDelayed() I'm using which could be unreliable.
EDIT: I tried making a receiver to listen for NEW_OUTGOING_CALL to restart the original Activity, but it doesn't really improve anything, the dialer app must be running for an arbitrary amount of time before it can start it's background service.
EDIT: I tried making a PhoneStateListener that listens for CALL_STATE_OFFHOOK and re starts the Activity there. This doesn't work either as it happens before the dialing app is fully ready to go into the background.
EDIT: You can look at this thread: Reflection to access advanced telephony features, but I believe Google has since locked down all methods of placing a call outside the standard app.
This solution will start the dialing, and then switch back to the original Activity after a couple of seconds.
In my manifest I have:
android:launchMode="singleInstance"
on my Activity so I don't get a new instance.
public class MainActivity extends Activity
{
....
public void clickMe(View view)
{
startService(new Intent(this, PhoneService.class));
}
}
public class PhoneService extends Service
{
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Intent call = new Intent(Intent.ACTION_CALL);
call.setData(Uri.parse("tel:XXXXXXXXX"));
call.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(call);
Handler h = new Handler();
h.postDelayed(new Runnable() {
#Override
public void run()
{
Intent act = new Intent(PhoneService.this, MainActivity.class);
act.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(act);
}
}, 4000);
return super.onStartCommand(intent, flags, startId);
}
#Override
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
return null;
}
}
I believe it impossible to provide a cleaner solution, given the constraints of the SDK.
The functionality you are wanting isn't possible without some type of hack-ish work around. The Android system only allows the Phone app to control the underlying RIL and telephony stack and the Phone app UI responds to the dial URI by presenting this user with the dial screen where they must confirm (or alter) the number. This is a security provision to prevent unwanted apps from using the telephone device without the user knowing about it. Also, due to the way the Intent system works in Android, it is possible for other apps to handle calls using SIP or other VoIP functionality (i.e. Skype). In this case the user may have set a global preference to always use the other app and you have no control over how that app behaves with the dial Intent.

Don't know why code isnt working.. Android/Java

I am currently trying to create an app that can track how much time I spend in a phone call and then display that on a toast message after clicking a button.
Code found here: http://paste.ideaslabs.com/show/6INd0afyi
I can't seem to figure out why the app is not working...
The idea is to create a service that starts as soon as I make a phone call (and keeps running indefinitely from then on). The Service has two while loops that track the start time and end time of the conversation using the getCallState() method through the TelephonyManager class. And then the values the end time and start time variables are stored and used in the activity class.
The activity class simply uses a button to display a toast message that says how much time I have spent.
When I try to run the app on my phone I can see the service running, but the app crashes sometimes or just shows that the time spent calling is 0 mins (which is not true..)
Hope you guys can point out any mistakes?!
Thanks!
Just by seeing the code you have posted I would say that you have not read properly the docs about services. You don't create a service by doing a MyService s = new MyService()
Read the Android developer guide or the Android SDK documentation. You'll see for instance how to start a local service or to use intents to start a service.
E.g.:
Intent intent = new Intent(this, HelloService.class);
startService(intent);
There are some events that the Operating System broadcast when they happen. eg. receiving sms, phone call state( sending, receiving). By reading your post, i think you should register your app with broadcast receiver. Here is a sample code.
public class PhoneCallState extends BroadcastReceiver
{
static long start_time, end_time;
#Override
public void onReceive(Context context, Intent intent)
{
final Bundle extras = intent.getExtras();
if(intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED))
{
final String state = extras.getString(TelephonyManager.EXTRA_STATE);
if ("RINGING".equals(state))
{
Toast.makeText(context, "Ringing", Toast.LENGTH_LONG).show();
}
if ("OFFHOOK".equals(state))
{
start_time = System.currentTimeMillis();
Toast.makeText(context, "Off", Toast.LENGTH_LONG).show();
}
if ("IDLE".equals(state))
{
end_time = System.currentTimeMillis();
long duration = (end_time - start_time) /1000;
Toast.makeText(context, "Duration : " + duration, Toast.LENGTH_LONG).show();
}
}
}
And register your receiver in the manifest file.
<receiver android:name=".PhoneCallState">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
}
Finally don't forgate to add a PHONE_STATE Permission.
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Looking at your previous questions I suggest reading this: How to make a phone call in android and come back to my activity when the call is done?
It describes how to set up a PhoneStateListener so that you can receive an intent when a call is started locally, received from someone else, and ended.
The Service has two while loops that track the start time and end time
These while loops are unnecessary with a PhoneStateListener, you can simply get two time stamps and subtract the difference, without having two while loops running every millisecond.

stop android emulator call

I am working on an Android application, having functionality like voicemail.
I am using BroadcastReceiver to get dialing events.
I have to get the event "WHEN CALL IS UNANSWERED (not picked after few rings) FROM RECEIVER".
I will do some actions on caller end against this event.
I am using AVD emulator, and
I do call from one instance to another instance and it calls perfectly,
but the problem is: It continuously calls until I reject or accept the call.
This way I cannot detect that "CALL IS UNANSWERED AFTER A NUMBER OF RINGS".
So I want the Caller emulator to drop the call after a number of rings (if unanswered) like a normal phone.
I can do it (drop the call after some time) by writing some code, but I need the natural functionality of phone in the emulator.
Can anyone please guide me? Is there any settings in the emulator? Or something else?
The code is shown below in case it helps:
public class MyPhoneReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
Bundle extras = intent.getExtras();
if (extras != null)
{
String state = "my call state = " + extras.getString(TelephonyManager.EXTRA_STATE);
Log.w("DEBUG", state);
}
}
Hi i think this is impossible with reference to the link:
http://android.bigresource.com/Track/android-sr1t1eagx/
Regards

Is it possible for an Android app to end a call that it initiated?

Looking through forums, it seems that there is not a way to end calls, but on the Android Developers page, the permission PROCESS_OUTGOING_CALLS "Allows an application to monitor, modify, or abort outgoing calls." I can't find any documentation on how to end a call even with this permission. Is this possible, or is it just a mistake on the page?
http://developer.android.com/reference/android/Manifest.permission.html#PROCESS_OUTGOING_CALLS
The attention is not to end call that is already started, but to drop the call before that was started. The meaning is - you dial some number , press send , program receives the call request and aborts it , so the call never starts. It is also possible just to change the phone number (to extend short numbers to full one, etc.) and pass the call.
It works fine:
public class phoneReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
abortBroadcast();
if (getResultData()!=null) {
String number = null;
setResultData(number);
}
}
}
}

Categories

Resources