Broadcast receiver calls twice EXTRA_STATE_RINGING state causing invariant data - android

I am trying to save call details when call comes so I implemented a broadacast receiver which listens to PHONE STATE. The problem comes when the call comes, it goes to EXTRA_STATE_RINGING twice where I implemented the logic so my logic called up twice causing invariant data.
private static final String ACTION_IN = "android.intent.action.PHONE_STATE";
Below is the code for BroadCastReceiver's onReceieve()
public void onReceive(Context context, Intent intent) {
ctx = context;
if (intent.getAction().equals(ACTION_IN)) {
Log.v("onReceive", "ACTION IN");
if ((bundle = intent.getExtras()) != null) {
Log.v("onReceive", "Bundle != NULL");
state = bundle.getString(TelephonyManager.EXTRA_STATE);
Log.v("onReceive", "state: "+state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
//businesslogic
}
}
}
}
I have following permission in the manifest
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
my receiver is defined as this in manifest
<receiver android:name="IncomingCallInterceptor" >
<intent-filter android:priority="999">
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>

Try this out, it does not implement the business logic at EXTRA_STATE_RINGING, it sets if only after user disconnects the call(CALL_STATE_IDLE ).
public void onReceive(Context context, Intent intent) {
ctx = context;
if (intent.getAction().equals(ACTION_IN)) {
Log.v("onReceive", "ACTION IN");
if ((bundle = intent.getExtras()) != null) {
Log.v("onReceive", "Bundle != NULL");
state = bundle.getString(TelephonyManager.EXTRA_STATE);
Boolean singlecallstate=false;
switch (state) {
case TelephonyManager.EXTRA_STATE_RINGING:
singlecallstate=true;
//any other code you want
case TelephonyManager.CALL_STATE_IDLE:
if(singlecallstate){
//business logic
singlecallstate=false;
}
}
}
}

Related

onReceive method doesn't get called on the first launch of application

I am trying to perform some implementation against the phone call. by following this link. Everything works fine except the onReceive method from PhonecallReceiver doesn't get called on the first launch of the application, afterward, it works fine. Thanks for your time and help.
PhonecallReceiver
public class PhonecallReceiver extends BroadcastReceiver {
//The receiver will be recreated whenever android feels like it. We need a static variable to remember data between instantiations
private static int lastState = TelephonyManager.CALL_STATE_IDLE;
private static Date callStartTime;
private static boolean isIncoming;
private static String savedNumber;
#Override
public void onReceive(Context context, Intent intent) {
//We listen to two intents. The new outgoing call only tells us of an outgoing call. We use it to get the number.
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
int state = 0;
if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)){
state = TelephonyManager.CALL_STATE_IDLE;
}
else if(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
state = TelephonyManager.CALL_STATE_OFFHOOK;
}
else if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)){
state = TelephonyManager.CALL_STATE_RINGING;
}
onCallStateChanged(context, state, number);
}
//Derived classes should override these to respond to specific events of interest
protected void onIncomingCallReceived(Context ctx, String number, Date start){}
public void onCallStateChanged(Context context, int state, String number) {
if(lastState == state){
//No change, debounce extras
return;
}
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
isIncoming = true;
callStartTime = new Date();
savedNumber = number;
onIncomingCallReceived(context, number, callStartTime);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//Transition of ringing->offhook are pickups of incoming calls. Nothing done on them
break;
case TelephonyManager.CALL_STATE_IDLE:
//Went to idle- this is the end of a call. What type depends on previous state(s)
break;
}
lastState = state;
}
}
CallReceiver
public class CallReceiver extends PhonecallReceiver {
private static CallListener mCallListener;
public static void bindListener(CallListener callListener) {
mCallListener = callListener;
}
#Override
protected void onIncomingCallReceived(final Context ctx, final String number, Date start) {
try {
if (mCallListener != null) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
stopRinging(ctx);
mCallListener.displayNumber(number);
}
}, 2000);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Manifest
<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="com.example.expphonecall.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.expphonecall.CallReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
<!--<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>-->
</receiver>
</application>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
My English expression may not be very good, I hope you can understand
You can make it as `
PhoneStateReceiver phoneStateReceiver = new PhoneStateReceiver();
IntentFilter intentFilter = new IntentFilter();
for (String action : actions) {
intentFilter.addAction(action);
}
c.registerReceiver(phoneStateReceiver,intentFilter);
`
but not register as static,It well be work?
Or
If your application implement InCallService.
Use call.registerCallback(Call.Callback),the Callback worked No abnormality
Required permissions(READ_PHONE_STATE, CALL_PHONE) were being asked separately in the activity(not a better way)
if (Build.VERSION.SDK_INT >= 23) {
if (getApplicationContext().checkSelfPermission(Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
// Permission has not been granted, therefore prompt the user to grant permission
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_PHONE_STATE},
//MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
11);
}
if (getApplicationContext().checkSelfPermission(Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED) {
// Permission has not been granted, therefore prompt the user to grant permission
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CALL_PHONE},
//MY_PERMISSIONS_REQUEST_PROCESS_OUTGOING_CALLS);
12);
}
}
And as the request for these permissions is asked collectively then the receiver started to invoke on the first launch of the application
int PERMISSION_ALL = 1;
String[] PERMISSIONS = {
Manifest.permission.CALL_PHONE,
Manifest.permission.READ_PHONE_STATE,
};
private void checkPermissions(){
if (!hasPermissions(this, PERMISSIONS)) {
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}
}
public static boolean hasPermissions(Context context, String... permissions) {
if (context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}

Broadcast receiver not working in android oreo

My Broadcast receiver is not working on oreo but its working below oreo it's working fine, I searched a lot regarding this but could not find the suitable solution. Does anyone face the same problem, here is my code regarding my service in which broadcast has been implemented. Kindly suggests me that how I can make in working in oreo.
Here is the class
public int onStartCommand(Intent intent, int flags, int startId) {
mContext = this;
mAppPreferences = new AppPreferences(mContext);
if (intent.getExtras() != null) {
data = (String) intent.getExtras().get("showPopUp");
phoneNumber= (String) intent.getExtras().get("dialNumber");
}
final IntentFilter intentFilter = new IntentFilter();
if (data.equalsIgnoreCase("true")) {
showPopup(getApplicationContext());
Utils.ApiHit(phoneNumber,getApplicationContext());
}
intentFilter.setPriority(2147483647);
intentFilter.addAction("android.intent.action.PHONE_STATE");
callExplicitReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
} else {
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
phoneNumber = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
int state = 0;
if (stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
state = TelephonyManager.CALL_STATE_IDLE;
} else if (stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
state = TelephonyManager.CALL_STATE_OFFHOOK;
} else if (stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
state = TelephonyManager.CALL_STATE_RINGING;
}
onCallStateChanged(context, state, phoneNumber);
}
}
}
};
mContext.registerReceiver(callExplicitReceiver, intentFilter);
return START_NOT_STICKY;
}
public void onIncomingCallReceived(Context ctx, String number, Date start) {
}
public void onIncomingCallAnswered(Context ctx, String number, Date start) {
if (popupView.getVisibility() == View.GONE) {
popupView.setVisibility(View.VISIBLE);
}
}
public void onIncomingCallEnded(Context ctx, String number, Date start, Date end) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
DeleteCallLogByNumber(number);
}
}, 2000);
if (popupView.getVisibility() == View.VISIBLE) {
popupView.setVisibility(View.GONE);
}
}
public void onOutgoingCallStarted(Context ctx, String number, Date start) {
// mAppPreferences.setPrefrenceString("busy", "yes");
// if (data.equalsIgnoreCase("true")) {
mediaPlayer = MediaPlayer.create(ctx, R.raw.speech_audio);
// } else {
// mediaPlayer = MediaPlayer.create(ctx, R.raw.speech_audio);
// }
mediaPlayer.start();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
}
}
}, 12000);
if (popupView.getVisibility() == View.GONE) {
popupView.setVisibility(View.VISIBLE);
}
}
public void onOutgoingCallEnded(Context ctx, String number, Date start, Date end) {
mAppPreferences.setPrefrenceString("busy", "no");
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
DeleteCallLogByNumber(phoneNumber);
}
}, 2000);
if (popupView.getVisibility() == View.VISIBLE) {
popupView.setVisibility(View.GONE);
}
}
public void onMissedCall(Context ctx, String number, Date start) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
DeleteCallLogByNumber(phoneNumber);
}
}, 2000);
if (popupView.getVisibility() == View.VISIBLE) {
popupView.setVisibility(View.GONE);
}
}
public void onCallStateChanged(Context context, int state, String number) {
if (lastState == state) {
return;
}
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
isIncoming = true;
callStartTime = new Date();
savedNumber = number;
onIncomingCallReceived(context, number, callStartTime);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if (lastState != TelephonyManager.CALL_STATE_RINGING) {
isIncoming = false;
callStartTime = new Date();
onOutgoingCallStarted(context, savedNumber, callStartTime);
} else {
isIncoming = true;
callStartTime = new Date();
onIncomingCallAnswered(context, savedNumber, callStartTime);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
if (popupView.getVisibility() == View.VISIBLE) {
popupView.setVisibility(View.GONE);
}
if (lastState == TelephonyManager.CALL_STATE_RINGING) {
onMissedCall(context, savedNumber, callStartTime);
} else if (isIncoming) {
onIncomingCallEnded(context, savedNumber, callStartTime, new Date());
} else {
onOutgoingCallEnded(context, savedNumber, callStartTime, new Date());
}
break;
}
lastState = state;
}
#Override
public void onDestroy() {
mContext.unregisterReceiver(callExplicitReceiver);
}
Noting is in coming inside receiever,Can anyone help me out in this?
New Additions as per discussion
Manifest data :-
Permission used :-
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Reciver:-
<receiver android:name="com.example.dialer.AppUtils.StartUpBootReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
My BroadCast Reciever Class :-
public class StartUpBootReceiver extends BroadcastReceiver {
private Context mContext;
#Override
public void onReceive(Context context, Intent intent) {
mContext= context;
String action = "START";
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
context.startForegroundService(new Intent(context, PhoneStateService.class));
}
else
{
context.startService(new Intent(context, PhoneStateService.class));
}
}
}
private boolean isServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
}
Rest the same service will get the call, but the problem is i still does not get call in receiver.And m primary point is that service should only get called once user tap on button , not automatically as i have to pass some values in the service.
Thanks
Broadcast Limitations
If an app registers to receive broadcasts, the app's receiver consumes resources every time the broadcast is sent. This can cause problems if too many apps register to receive broadcasts based on system events; a system event that triggers a broadcast can cause all of those apps to consume resources in rapid succession, impairing the user experience. To mitigate this problem, Android 7.0 (API level 25) placed limitations on broadcasts, as described in Background Optimization. Android 8.0 (API level 26) makes these limitations more stringent.
Apps that target Android 8.0 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest. An implicit broadcast is a broadcast that does not target that app specifically. For example, ACTION_PACKAGE_REPLACED is an implicit broadcast, since it is sent to all registered listeners, letting them know that some package on the device was replaced. However, ACTION_MY_PACKAGE_REPLACED is not an implicit broadcast, since it is sent only to the app whose package was replaced, no matter how many other apps have registered listeners for that broadcast.
Apps can continue to register for explicit broadcasts in their manifests.
Apps can use Context.registerReceiver() at runtime to register a receiver for any broadcast, whether implicit or explicit.
Broadcasts that require a signature permission are exempted from this restriction, since these broadcasts are only sent to apps that are signed with the same certificate, not to all the apps on the device.
From the Official Documentation
The problem comes with the service you're trying to run, services or persistent background services are not permitted to run for long for apps targeting Oreo and above.
Check this guide and this as well for migrating your app to support Oreo.
I also had this kind of issue, but I found a better solution:
Class MyReceiver
#BroadcastReceiverActions({
"android.intent.action.SCREEN_ON",
"android.intent.action.SCREEN_OFF",
"android.intent.action.DREAMING_STARTED",
"android.intent.action.DREAMING_STOPPED",
"android.intent.action.ACTION_POWER_DISCONNECTED",
"android.intent.action.ACTION_POWER_CONNECTED",
"android.net.conn.CONNECTIVITY_CHANGE"
})
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
super();
}
#Override
public void onReceive(Context context, Intent intent) {
Session.getGlobalReceiverCallBack(context, intent);
//Log.e("dfd", "" + intent.getAction());
}
}
Class AppController
public class AppController extends Application {
private BroadcastReceiver receiver;
MyReceiver mR;
#Override
public void onCreate() {
super.onCreate();
mR = new MyReceiver();
receiver = DynamicReceiver.with(mR)
.register(this);
}
}
Class MainActivity
public class MainActivity extends AppCompatActivity implements GlobalReceiverCallBack {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Session.setmGlobalReceiverCallback(this);
}
#Override
public void onCallBackReceived(Context context, Intent intent) {
Toast.makeText(context, "" + intent.getAction(), Toast.LENGTH_LONG).show();
}
}
For complete reference you can see also https://github.com/devggaurav/BroadcastReceiver-For-Naught-and-Oreo-devices
Register your broadcast receiver in activity on create method rather than in manifest and unregister it on destroy method. Hope this will work on android 9.
Android 8.0 offers several improvements to JobScheduler that make it easier to replace services and broadcast receivers with scheduled jobs:
https://developer.android.com/about/versions/oreo/background
In many cases, apps that previously registered for an implicit broadcast can get similar functionality by using a JobScheduler job. For example, a social photo app might need to perform cleanup on its data from time to time, and prefer to do this when the device is connected to a charger. Previously, the app registered a receiver for ACTION_POWER_CONNECTED in its manifest; when the app received that broadcast, it would check whether cleanup was necessary. To migrate to Android 8.0 or higher, the app removes that receiver from its manifest. Instead, the app schedules a cleanup job that runs when the device is idle and charging.
I have faced the similar issue when implementing call recording app,
I have added the following code in the AndroidManifest.xml file, then the register is working normally
<receiver android:name=".Services.Receiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
<action android:name="android.intent.action.ANSWER"/>
<action android:name="android.intent.action.CALL_BUTTON"/>
<action android:name= "android.intent.action.NEW_OUTGOING_CALL"/>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

How to do Call forwading from Android Application

I Want to do call forwarding from my application the scenario is when any GSM call came then from my application i want to forward that number to other desitination number.
Here i have done some code for Call forwarding but its not working. I have created Receiver and also declared in Manifest and has given Call permission.
But anyhow it is not working.
Manifests Code:-
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<receiver android:name=".PhoneStateReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
Receiver Code :-
public class PhoneStateReceiver extends BroadcastReceiver {
Context ctx;
#Override
public void onReceive(Context context, Intent intent) {
this.ctx=context;
try {
System.out.println("Receiver start");
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
System.out.println("Incoming Number" + incomingNumber);
System.out.println("INcoming State" + state);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
Toast.makeText(context,"Incoming Call State",Toast.LENGTH_SHORT).show();
Toast.makeText(context,"Ringing State Number is -"+incomingNumber,Toast.LENGTH_SHORT).show();
String fwdMobNumVar = ("**21*" + "1234567890" + "#");
callforward(fwdMobNumVar);
}
if ((state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))){
Toast.makeText(context,"Call Received State",Toast.LENGTH_SHORT).show();
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
Toast.makeText(context,"Call Idle State",Toast.LENGTH_SHORT).show();
}
}
catch (Exception e){
e.printStackTrace();
}
}
private void callforward(String callForwardString) {
PhoneCallListener phoneListener = new PhoneCallListener();
TelephonyManager telephonyManager = (TelephonyManager)
ctx.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
Intent intentCallForward = new Intent(Intent.ACTION_CALL);
Uri mmiCode = Uri.fromParts("tel", callForwardString, ("#"));
intentCallForward.setData(mmiCode);
ctx.startActivity(intentCallForward);
}
public class PhoneCallListener extends PhoneStateListener
{
private boolean isPhoneCalling = false;
#Override
public void onCallStateChanged(int state, String incomingNumber)
{
if (TelephonyManager.CALL_STATE_RINGING == state)
{
// phone ringing
Toast.makeText(ctx,"Callforward ringing",Toast.LENGTH_SHORT).show();
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state)
{
// active
Toast.makeText(ctx,"Callstate hook",Toast.LENGTH_SHORT).show();
isPhoneCalling = true;
}
if (TelephonyManager.CALL_STATE_IDLE == state)
{
// run when class initial and phone call ended, need detect flag
// from CALL_STATE_OFFHOOK
if (isPhoneCalling)
{
// restart app
Toast.makeText(ctx,"Call phone forwading ",Toast.LENGTH_SHORT).show();
Intent i = ctx.getPackageManager()
.getLaunchIntentForPackage(ctx.getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
ctx.startActivity(i);
isPhoneCalling = false;
}
}
}
}
}

Broadcast receiver to check the Internet Connection

I am beginner in android. I just want to know what is Broadcast receiver (with an example if possible) and it's on onReceive() method. and how we can use that receiver to check the Internet connection.
So should I use the same to check the Internet connection or just create a static method in Main class and access it in other classes?
Okay so i'm not the best programmer but this should work.
this is for a fragment but you should easily be able to change it for an
activity.
first make 3 variables:
Context _mContext;
BroadcastReceiver br = null;
IntentFilter filter;
in your onCreate() paste this:
BroadcastReceiver br = new MyBroadCastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
_mContext = getActivity().getApplicationContext()
_mContext.registerReceiver(br, filter);
with this you register your fragment to the broadcastreceiver class MyBroadCastReceiver.
Now make a MyBroadCastReceiver class and paste this:
public class MyBroadCastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
int extraWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
WifiManager.WIFI_STATE_UNKNOWN);
switch(extraWifiState) {
case: WifiManager.WIFI_STATE_DISABLED:
//do something
break;
case WifiManager.WIFI_STATE_ENABLED:
//do something
break;
case: WifiManager.WIFI_STATE_ENABLING:
//do something
break;
case: WifiManager.WIFI_STATE_DISABLING:
//do something
break;
case: WifiManager.WIFI_STATE_UNKNOWN:
//do something with data if you desire so, I found it unreliable until now so i've done nothing with it
}
}
}`
Go back to your fragment and paste this somewhere at the bottom:
#Override
public void onPause() {
try {
if (br == null) {
Log.d("Receiver", "Can't unregister a receiver which was never registered");
} else {
getActivity().getApplicationContext().unregisterReceiver(br);
br = null;
}
} catch(Exception err) {
Log.e(err.getClass().getName(), err.getMessage(), err);
Log.e("Receiver not registered", "Couldn't get context");
}
super.onPause();
}
#Override
public void onResume() {
if(br != null) {
Log.d("Receiver", "Can't register receiver which already has been registered");
} else {
try {
br = new MyBroadCastReceiver();
filter = new IntentFilter();
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
filter.addAction(CONNECTIVITY_SERVICE);
_mContext = getActivity().getApplicationContext();
_mContext.registerReceiver(br, filter);
} catch(Exception err) {
Log.e(err.getClass().getName(), err.getMessage(), err);
}
}
super.onResume();
}
You should be able to call register/unregister the broadcast like this anywhere.
I hope this is enough
<receiver
android:name=".InternetConnectorBroadcastReceiver"
android:enabled="true">
<intent-filter>
<!-- Intent filters for broadcast receiver -->
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
<action android:name="android.net.wifi.STATE_CHANGE" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
public class InternetConnectionBroadcastReceiver extends BroadcastReceiver {
public InternetConnectorBroadcastReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
}
}

how to detect the end of outgoing ringtone?

I want to make a call with my program and after that recognize the call status . I want to detect the end of outgoing ringtone.
How to detect the end of outgoing ringtone ?
I use of Flowing code for make call .
EditText ed = (EditText) findViewById(R.id.txtcall);
Uri uri = Uri.fromParts("tel", ed.getText().toString(), null);
callIntent = new Intent(Intent.ACTION_CALL,uri);
startActivity(callIntent);
Following code snippet can help you in finding the phone call current statue whether its picked, ringing or idle. You can easily use these states and implement your functionality accordingly.
private class CustomPhoneStateListener extends PhoneStateListener
{
#Override
public void onCallStateChanged(int state, String incomingNumber)
{
super.onCallStateChanged(state, incomingNumber);
//TelephonyManager.CALL_STATE_IDLE
//TelephonyManager.CALL_STATE_OFFHOOK
//TelephonyManager.CALL_STATE_RINGING
}
}
Check by This:- you will be able to call user
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:+9189745847"));
startActivity(callIntent);
and also use in androidmanifest.xml file:
<uses-permission android:name="android.permission.CALL_PHONE" />
Have a look at intent NEW_OUTGOING_CALL
<receiver android:name=".receiver.OutgoingCallReceiver" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
Then you can check the state of call with a PhoneStateListener :
public class OutgoingCallReceiver extends BroadcastReceiver {
protected static final String CLASS_TAG = "OutgoingCallReceiver : ";
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle) return;
String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i(LOG_TAG, CLASS_TAG + "phone number " + phonenumber);
final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(new PhoneStateListener() {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
Log.d(LOG_TAG, CLASS_TAG + "RINGING");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d(LOG_TAG, CLASS_TAG + "OFFHOOK");
Process.onCallStateIsOFFHOOK();
break;
case TelephonyManager.CALL_STATE_IDLE:
Process.onCallStateIsIDLE();
Log.d(LOG_TAG, CLASS_TAG + "IDLE");
break;
default:
Log.d(LOG_TAG, CLASS_TAG + "Default: " + state);
break;
}
}
}, PhoneStateListener.LISTEN_CALL_STATE);
}
}
In the Process class you can detect the ringtone and create more state like CALLBEGINS, CALLANSWERED by recording the VOICE_CALL.
Example of onCallStateIsIDLE() :
public void onCallStateIsIDLE() {
setSpeakingOn(false);
if (stateCall == STATE_CALLANSWERED ||
stateCall == STATE_CALLBEGINS ||
stateCall == STATE_DIALISOFFHOOK ||
stateCall == STATE_DIALENDREQUESTED) {
Log.i(LOG_TAG, CLASS_TAG + " - forcing state : STATE_DIALREADY : old state = " + stateCall);
stateCall = STATE_DIALREADY;
}
}

Categories

Resources