Read Phone State Reciever not working on real device - android

I have tested my reciever on emulator and simulate face call to emulator and can log phone state even get calling number,
However if i try this on real device, it is not working. I added an activity and if it working background, it works. But why should i add it? Why has to activity run on background?
#Override
public void onReceive(Context cnx, Intent nt) {
Bundle bnd = nt.getExtras();
Log.e("EXTRA", "EXTRA");
if (bnd != null) {
String state = bnd.getString(TelephonyManager.EXTRA_STATE);
Log.i("PHONE STATE", state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Log.i("PHONE STATE",
bnd.getString(TelephonyManager.EXTRA_INCOMING_NUMBER));
}
}

Activity meant to be run in Foreground, If you want to run your process in Background you should use Services.
read here more
For your question, You should have separate class for receiver, Register receiver in Manifest file for Phone state change action and with permission to read phone state.

Related

Bring app to front from foreground service

I have an app that enables people to send alarms to groups of other users and I have implemented support to trigger alarms from a BLE device. It all works as expected, except for when the app is in background.
I use a foreground service to listen for the device event and a local broadcast to trigger the actual alarm. The alarm get triggered as expected and all code runs (sending alarm to server, playing audio, etc) but the app stays in background.
I have tried several solutions but failed to come up with a working one. Either the answers are too old or they just do not work (as in unwanted results).
I want the exact same result as when tapping the foreground notification = resume the current activity without passing destroy.
I'm actually surprised how hard this seems to be, in my opinion it should be easy as "bringToFront()" since the activity is obviously running.
Any help would be much appreciated!
I don't think any of my code would help to understand my problem, but here's some anyways:
AlarmDeviceService.java - The service listening for BLE events:
private void sendAlarmTriggerBroadcastMessage(String deviceName){
Intent intent = new Intent();
intent.setAction(ALARM_DEVICE_TRIGGERED);
intent.putExtra(INTENT_EXTRA_SOURCE_NAME, deviceName);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
}
AlarmDeviceBroadcastReceiver.java:
public void onReceive(Context context, Intent intent){
String action = intent.getAction();
Log.d(TAG, "Received: "+ action);
if(mReceiver != null){
if(action.equals(AlarmDeviceService.ALARM_DEVICE_TRIGGERED)) {
mReceiver.onDeviceAlarmBroadcast();
}
else if(action.equals(AlarmDeviceService.ALARM_DEVICE_CONNECTED)){
String mac = intent.getStringExtra(AlarmDeviceService.INTENT_EXTRA_SOURCE_MAC);
mReceiver.onAlarmDeviceConnected(mac);
}
else if(action.equals(AlarmDeviceService.ALL_DEVICES_CONNECTED)){
mReceiver.onAllDevicesConnected();
}
}
}
AlarmActivity.kt - The activity that should be brought to front:
override fun onDeviceAlarmBroadcast(){
if (alarmUtilities?.alarmSentHandler?.currentSentAlarm == null) {
Log.d(LOG, "Doing it!")
triggerDefaultAlarm()
}else{
alarmUtilities?.alarmSentHandler?.cancelAlarm()
}
}
If my question is poorly put, please excuse me - it's my first post.

Removing app from task manager kills Broadcast

I'm creating an app which detects Power Key press (both in foreground and background). I'm using the following BroadcastReceiver for this
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
String action = arg1.getAction();
if (action.equals(Intent.ACTION_SCREEN_OFF)
|| action.equals(Intent.ACTION_SCREEN_ON)) {
Toast.makeText(context, "DETECTED", 5000).show();
}
}
I'm starting the broadcast from my MainActivity
Intent i = new Intent(MainActivity.this, Receive.class);
sendBroadcast(i);
This works fine in both foreground and background, but when I swipe away my app from Task manager, it won't detect it any more. Also, this works in other phones.
I thought it might be a case with my phone only, but I've an app in my mobile which detects the power key press even on removing it from Task manager (So, it's also possible in my phone)
You have to define all the action in manifest file. Why it is needed because Android system maintains list of intent-filters at the time of installation of application. You should use staic intent-filter when you want to hanldle broadcast even if application is not in running state.
You have use dynamin intent-filter registering. This should be used only when you want to handle broadcast when application is in running state.

How to unregister incoming call and outgoing call receivers?

I'm developing an application for Android.
I use Samsung Galaxy S3 with original ROM Jelly Bean 4.3.
I have a problem with the receivers of incoming call and outgoing call.
Here is the code of IncomingCallReceiver:
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
if (Start.getMusicService().isPlaying()) {
pauseMusicService();
isMusicPlaying = true;
}
}
else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
if (isMusicPlaying){
restartMusicService();
isMusicPlaying = false;
}
}
}
However this is not the problem, this code works and also the outgoing call receiver works perfectly.
My problem is that when i exit the application this receivers still work.
It's happened that, days after the last time i opened the application, i received a phone call and the application crashed.
I think that it's happened because i write the code:
if (Start.getMusicService().isPlaying())
because Start (my first activity) doesn't exist.
I never register this receiver, they work without that i "call" them.
I don't know if i have to unregister and in case when i have to do it.
So, can anyone help me please?
Andrea
When declaring a BroadcastReceiver or other Intent receiver in your AndroidManifest it is always "on". Move the registration and deregistration to the beginning and end of the execution of your app, or other times in your app; then you will be able to control when its on at runtime.
Above answer is correct. If you want to register and/or unregister receiver yourself at you should register in onResume and unregister in onPause in case of activity. Look out this example http://sohailaziz05.blogspot.com/2012/05/broadcast-receiver-two-ways-to.html

Android-Telephony application that keeps focus on incoming calls

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();
}
}
}
}

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

Categories

Resources