Phone State Broadcast Receiver not working - android

I am trying to get caller phone number and date/time of the call after an incoming call ends then start an activity and pass the data but the Broadcast receiver doesn't work.
I added a code to print an message on the console to check if the onReceived method gets called but it never prints.
My AndroidManifest.xml code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.inyavic.inyavicsage.distresscallsmanager">
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AddDistressCallManagerActivity" />
<activity android:name=".UpdateDistressCallManagerActivity" />
<activity android:name=".AddDistressCallInfoActivity" />
<activity android:name=".UpdateDistressCallInfoActivity" />
<activity android:name=".DistressCallInfoListActivity" />
<activity android:name=".DistressCallInfoActivity" />
<activity android:name=".DistressCallManagersListActivity" />
<receiver
android:name=".PhoneStateBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>
My PhoneStateBroadcastReceiver.java code:
package com.inyavic.inyavicsage.distresscallsmanager;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
public class PhoneStateBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "PhoneStateBroadcastReceiver";
Context mContext;
String incomingNum;
private int prevState;
#SuppressLint("LongLogTag")
#Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
CustomPhoneStateListener customPhoneStateListener = new CustomPhoneStateListener();
// register our listener with TelephonyManager
telephony.listen(customPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
Bundle bundle = intent.getExtras();
String phoneNum = bundle.getString("incoming_number");
Log.v(TAG, "phoneNum" + phoneNum);
mContext = context;
System.out.println("RECEIVED");
}
public class CustomPhoneStateListener extends PhoneStateListener {
private static final String TAG = "CustomPhoneStateListener";
#SuppressLint("LongLogTag")
#Override
public void onCallStateChanged(int state, String incomingNumber) {
if (incomingNumber != null && incomingNumber.length() > 0)
incomingNum = incomingNumber;
// Answered call which is ended
if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
prevState = state;
Log.d(TAG, "CALL_STATE_OFFHOOK");
}
}
}
}
What could be the problem and what's the solution?
UPDATE:
I tried registering the broadcast receiver dynamically as suggested by Jonas Lochmann but still not working.
This is the code I added to the onCreate method of my main activity to register my broadcast receiver dynamically:
PhoneStateBroadcastReceiver broadcastReceiver = new PhoneStateBroadcastReceiver();
this.registerReceiver(broadcastReceiver,
new IntentFilter("android.intent.action.PHONE_STATE"));

Related

Android service does not start all the times after reboot

I want to start a service after reboot. The problem I have is that this does not happen every time (at least the first 20 minnutes). I have study many questions into stackoverflow and try a number of the provided solutions however sometimes the service does not automaticaly start after reboot.
Also I have to add another parameter this of the foreground service for android versions O and above.
Could someone give me any advice?
AndroidManifest.xml
.... <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name=".App"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
>
<activity
android:name=".Activities.MainActivity"
....
</activity>
<activity
...
</activity>
<receiver android:name=".Helpers.BootCompletedIntentReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<!-- I think that this is also not necessery <category android:name="android.intent.category.DEFAULT" /> -->
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
<service
android:name=".Services.myService"
android:enabled="true"
android:exported="true"></service>
</application>
BroadcastReciever
package com.abc.Helpers;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import com.abx.Activities.MainActivity;
import com.abc.Services.myService;
public class BootCompletedIntentReceiver extends BroadcastReceiver {
private static final String TAG = "MyBroadcastReceiver";
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent serviceIntent = new Intent(context, myService.class);
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ContextCompat.startForegroundService(context,serviceIntent);
} else {
context.startService(serviceIntent);
}
}
}
I also found in a comment here that reciever should be registered into an activity of the application and this is the code in Mainactivity. Do you agree?
#Override
protected void onCreate(Bundle savedInstanceState) {
...
final ComponentName onBootReceiver = new ComponentName(getApplication().getPackageName(), BootCompletedIntentReceiver.class.getName());
if(getPackageManager().getComponentEnabledSetting(onBootReceiver) != PackageManager.COMPONENT_ENABLED_STATE_ENABLED)
getPackageManager().setComponentEnabledSetting(onBootReceiver,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
..
}

Broadcast receiver for incoming call not working?

I am trying to display toast on incoming call received. but i am not getting any display. I have mention the receiver inside the manifest file which also contains permission required for phone calls.
the following is code that i have used.
// inside IncomingCall broadcastreceiver
package com.example.shailesh.callbroadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
public class IncomingCall extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
// This code will execute when the phone has an incoming call
// get the phone number
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast.makeText(context, "Call from:" +incomingNumber, Toast.LENGTH_LONG).show();
} else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_IDLE)
|| intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_OFFHOOK)) {
// This code will execute when the call is disconnected
Toast.makeText(context, "Detected call hangup event", Toast.LENGTH_LONG).show();
}
}
}
And i have specify in menifest file as follows :
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".IncomingCall" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
what else i have to include in order to get toast display on incoming call received.
i Have also included following code in my MainActivity
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = new Intent(getApplicationContext(),IncomingCall.class);
startService(i);
}
}
is class IncomingCall a service ? if not then why you are firing an intent to start service.

Detecting phone activity in a class

So in my Android app, I've been testing how to detect an incoming and outgoing call. I got it to work by building a class that extends a BroadcastReceiver, but in that class if I call another class it crashes. For example MyClass mc = new MyClass(); mc.functionname();
My actual app is running a looping sound clip. I want to pause that sound clip when an incoming or outgoing call is made. The soundclip is played in a class that extends Activity. How do I do this?
This is what I have for the BroadcastReceiver class and my manifest.
package com.anthony.phone;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
public class inComingReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
Log.i("MYTAG",bundle.toString());
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
Log.i("MYTAG","State: "+ state);
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.i("MYTAG",":) Incomng Number: " + phonenumber);
String info = "Detect Calls sample application\nIncoming number: " + phonenumber;
Toast.makeText(context, info, Toast.LENGTH_LONG).show();
}
}
}
and my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.anthony.phone"
android:versionCode="1"
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".CallTActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.anthony.phone.inComingReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>
</manifest>
i had also this kind of problem. i solved by calling activity from broadcast receiver And writing my class code in particular Activity. As mentioned below
Intent i = new Intent(context,newActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
i hope it will help you to solve your query.
ask me if you further have any doubt.

Call BroadcastReceiver from Activity

I am beginner android developer.
I am trying to call BroadcastReceiver from Activity because I need to update entry if somebody call me.
This is the activity where I am calling BroadcastReceiver.
Source code:
public class CalendarCall extends Activity {
private static final String TAG = "CalendarCall";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = new Intent(this, MyBroadcastReceiver.class);
startActivity(i);
}
// ......
}
MyBroadcastReceiver.class:
package org.example.calendarcall;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.w("DEBUG", state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String phoneNumber = extras
.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.w("DEBUG", phoneNumber);
}
}
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example.calendarcall"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".CalendarCall"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="MyBroadcastReceiver">
</receiver>
</application>
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
</manifest>
Error:
04-21 12:37:40.821: ERROR/AndroidRuntime(793): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.calendarcall/org.example.calendarcall.CalendarCall}: android.content.ActivityNotFoundException: Unable to find explicit activity class {org.example.calendarcall/org.example.calendarcall.MyBroadcastReceiver};
I think the problem is declaraction in Manifest and i also dont know if i can call BroadcastReceiver from Activity using:
Intent i = new Intent(this, MyBroadcastReceiver.class);
startActivity(i);
A BroadCastReceiver receives intents from sender's who's calling the sendBroadcast() method. If you would like your MyBroadcastReceiver to be invoked when someone calls you, you could add the filter android.intent.action.PHONE_STATE like:
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
You don't really need your CalendarCall activity.
If you want to transfer information from your broadcast receiver to your activity, see this stackoverflow question.
you can call a broadcaste reciever from activity.
you need to register and unregister the reciever and you need to call sendbroadcastereciver() also in your code

Broadcast Receiver doesn't work

Main Activity Code. BroadcastExample.java
package com.example.broadcast;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class BroadcastExaple extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("PHONE", "Main Activity...Called");
setContentView(R.layout.main);
Log.d("PHONE", "After Mainf Activity...");
}
}
MyBroadCastReceiver.java
package com.example.broadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
/*public void onReceive(Context context, Intent intent) {
try{
Log.d("Call BroadCAST","Calling Broad CAST");
MyPhoneStateListener phoneListener = new MyPhoneStateListener();
TelephonyManager telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
catch(Exception e)
{
}
}*/
public void onReceive(Context context, Intent intent) {
Log.d("Message", "Message Received");
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcast" android:versionCode="1"
android:versionName="1.0">
<uses-prmission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".BroadcastExaple" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>
Program doesn't enter into the onReceive Mathod of MyBroadCastReceiver class.
First, i suggest you to format your code here. So that people here will be glad to read your problem code.
Your code list above does NOT register any BroadcastReceiver to the system. You'd better to check out ApiDemo for more details.

Categories

Resources