I am developing an android application related to geofencing and I tried Broadcast receiver to get a notification when entering into geofence and I will work only when the application is running and then I tried Service to get a notification but now no notification is coming when entering into geofence.
Here is the code
**
package com.example.fypautosilenceapp;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.Nullable;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofencingEvent;
import java.util.List;
public class GeofenceService extends Service {
private static final String TAG = "GeofenceBroadcastRecive";
AudioManager audioManager;
#Nullable
#Override
public IBinder onBind(Intent intent) {
NotificationHelper notificationHelper=new NotificationHelper(getApplicationContext());
GeofencingEvent geofencingEvent=GeofencingEvent.fromIntent(intent);
if(geofencingEvent.hasError()){
Toast.makeText(getApplicationContext(), "Geofence has error", Toast.LENGTH_SHORT).show();
Log.d(TAG,"OnRecive:Error recive geofence event...");
}
List<Geofence> geofenceslist=geofencingEvent.getTriggeringGeofences();
//Location geofencelocation=geofencingEvent.getTriggeringLocation();
for (Geofence geofence:geofenceslist){
Log.d(TAG,"OnRecive:"+geofence.getRequestId());
}
int transtiontype=geofencingEvent.getGeofenceTransition();
switch (transtiontype){
case Geofence.GEOFENCE_TRANSITION_ENTER:
// Toast.makeText(getApplicationContext(),"Enter geofence",Toast.LENGTH_SHORT).show();
notificationHelper.sendHighPriorityNotification("Enter geofence","",MapsActivity.class);
// audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
break;
case Geofence.GEOFENCE_TRANSITION_EXIT:
// Toast.makeText(getApplicationContext(),"Exit geofence",Toast.LENGTH_SHORT).show();
notificationHelper.sendHighPriorityNotification("Exit geofence","",MapsActivity.class);
// audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
break;
case Geofence.GEOFENCE_TRANSITION_DWELL:
// Toast.makeText(getApplicationContext(),"Enter Dwell",Toast.LENGTH_SHORT).show();
notificationHelper.sendHighPriorityNotification("Enter Dewell","",MapsActivity.class);
// audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
break;
}
return null;
}
}
**
Related
I am trying to implement an alarm manager that will be called every day to update a database. I started by trying the set Function of the alarmManager and it worked to trigger the alarm at a certain time. Then I moved on to try and use setRepeating to trigger the alarm on a daily basis. The setRepeating function which is done to Repeat every minute just for testing is only working the first time and is not doing the function repeatedly.
Here is my BroadcastReceiver:
package com.example.bleh.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.example.bleh.myapplication.DB.AppDatabase;
import com.example.bleh.myapplication.DB.Plan;
import com.example.bleh.myapplication.DB.User;
import com.example.bleh.myapplication.Utils1.FormulaUtils;
import com.github.lzyzsd.circleprogress.DonutProgress;
public class AlarmReceiver extends BroadcastReceiver {
private static final String DEBUG_TAG = "AlarmReceiver";
public AppDatabase mydb;
Plan plan;
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
mydb = AppDatabase.getInstance(context);
final long planid = intent.getExtras().getLong("planid");
final long userid = intent.getExtras().getLong("uid");
final String requirements = intent.getExtras().getString("requirements");
Log.wtf("PlanId: ",planid+"");
Log.wtf("UserId: ",userid+"");
Log.wtf("Requirements",requirements);
plan = mydb.getPlanDao(context).getPlanById((int) planid);
final User user = mydb.getUserDao(context).getUserById((int) userid);
plan.setCurrentWeight(FormulaUtils.reCalculateWeight(plan.getCurrentWeight(), Double.parseDouble(requirements)));
plan.setBmr(Double.parseDouble(FormulaUtils.calculateBmr(user.getSex(), plan.getCurrentWeight(), user.getHeight(), user.getBirthDay())));
plan.setNbOfDays(plan.getNbOfDays() - 1);
mydb.getPlanDao(context).update(plan);
String requirement = FormulaUtils.CalulcateDailyRequirements(plan.getWorkoutPerWeek(), plan.getBmr());
String day = plan.getNbOfDays() + "";
float progress = 0F;
// Start feature2 activity with updated data
Intent updateFeature2Intent = new Intent(context, feature2.class);
updateFeature2Intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); // Add this flag
updateFeature2Intent.putExtra("requirement", requirement);
updateFeature2Intent.putExtra("day", day);
updateFeature2Intent.putExtra("progress", progress);
context.startActivity(updateFeature2Intent);
}
}
Here is my Feature Activity:
package com.example.bleh.myapplication;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import com.example.bleh.myapplication.DB.AppDatabase;
import com.example.bleh.myapplication.DB.Exercise;
import com.example.bleh.myapplication.DB.Food;
import com.example.bleh.myapplication.DB.Plan;
import com.example.bleh.myapplication.DB.User;
import com.example.bleh.myapplication.Utils1.FormulaUtils;
import com.github.lzyzsd.circleprogress.DonutProgress;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class feature2 extends AppCompatActivity {
public AppDatabase mydb;
TextView BMR,requirements,days;
Button addfood,addex,nextday;
LinearLayout mainLayout;
Button Meas,Bluetooth;
DonutProgress donutProgress;
Plan plan;
// Add this method
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// Get data from intent
String requirement = intent.getStringExtra("requirement");
String day = intent.getStringExtra("day");
float progress = intent.getFloatExtra("progress", 0F);
// Update UI
TextView requirements = findViewById(R.id.requirements);
TextView Days = findViewById(R.id.days);
DonutProgress dailyProgress = findViewById(R.id.donut_progress);
requirements.setText(requirement);
Days.setText(day);
dailyProgress.setProgress(progress);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feature2);
Intent intent = getIntent();
requirements = findViewById(R.id.requirements);
donutProgress = findViewById(R.id.donut_progress);
days = findViewById(R.id.days);
final long planid = intent.getExtras().getLong("planid");
final long userid = intent.getExtras().getLong("uid");
mydb = AppDatabase.getInstance(feature2.this);
plan = mydb.getPlanDao(feature2.this).getPlanById((int) planid);
try {
requirements.setText(FormulaUtils.CalulcateDailyRequirements(plan.getWorkoutPerWeek(), plan.getBmr()));
}
catch (Exception ex)
{
Log.wtf("There is no plan","!");
}
Intent intent1 = new Intent(this, AlarmReceiver.class);
intent1.putExtra("uid", userid);
intent1.putExtra("planid", planid);
intent1.putExtra("requirements",requirements.getText().toString());
Calendar updateTime = Calendar.getInstance();
updateTime.setTimeZone(TimeZone.getTimeZone("GMT"));
updateTime.set(Calendar.HOUR_OF_DAY, 8);
updateTime.set(Calendar.MINUTE, 3);
updateTime.set(Calendar.SECOND,0);
Date milliseconds = updateTime.getTime();
long millis = milliseconds.getTime();
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent1, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, millis ,60000, pendingIntent);
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();
}
}
Here is my Manifest Part:
<receiver android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true">
</receiver>
I had encountered this problem earlier with my app
Repeating alarms will not trigger for api > 23 if phone is in idle state. Refer Schedue Repeating alarms
Doze mode also blocks the alarm.
What you can do is to use setExact and setExactAndAllowWhileIdle for api > 23 and use your own logic to repeat the alarm.
if (Build.VERSION.SDK_INT >= 23) {
alarmManager.setExactAndAllowWhileIdle(RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
} else {
alarmManager.setExact(RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
}
Trigger the alarm again in the receiver or in the activity which is triggered by the alarm manager.
In your case set the alarm again in the AlarmReceiver for next day.
I'd recommend you to read about Doze mode and this topic. Also you can use this lib by Evernote. It helps to handle scheduling of background tasks.
This question already has answers here:
How to sound notifications sound when new Message arrives from Firebase Realtime Data base
(4 answers)
Closed 4 years ago.
I'm using firebase data base to create my chat applicaiton. Now that I have sucessfully completed my chat application, but when new message arrives I would like to notify user with sound and NOtification in Notification bar even when the app is not running.
I used the below code to do that
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Notifications Example")
.setContentText("This is a test notification");
Intent notificationIntent = new Intent(this, MenuScreen.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
builder.setAutoCancel(true);
builder.setLights(Color.BLUE, 500, 500);
long[] pattern = {500,500,500,500,500,500,500,500,500};
builder.setVibrate(pattern);
builder.setStyle(new NotificationCompat.InboxStyle());
builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
NotificationManager manager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, builder.build());
But it only set alarm when i open the chat activity, then aftearwards when new message arrives it does nothing.
Here is my chat activity code
package com.nepalpolice.mnemonics.chat;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.media.AudioManager;
import android.media.RingtoneManager;
import android.media.ToneGenerator;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.messaging.RemoteMessage;
import com.nepalpolice.mnemonics.R;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* Created by filipp on 6/28/2016.
*/
public class Chat_Room extends AppCompatActivity{
private Button btn_send_msg;
private EditText input_msg;
private TextView chat_conversation;
private Toolbar mainToolbar;
private String user_name,room_name;
private DatabaseReference root ;
private String temp_key;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_room);
btn_send_msg = (Button) findViewById(R.id.btn_send);
input_msg = (EditText) findViewById(R.id.msg_input);
chat_conversation = (TextView) findViewById(R.id.textView);
user_name = getIntent().getExtras().get("user_name").toString();
room_name = getIntent().getExtras().get("room_name").toString();
mainToolbar = (Toolbar) findViewById(R.id.main_chat);
setSupportActionBar(mainToolbar);
getSupportActionBar().setTitle(" Room - "+room_name);
root = FirebaseDatabase.getInstance().getReference().child(room_name);
btn_send_msg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Map<String,Object> map = new HashMap<String, Object>();
temp_key = root.push().getKey();
root.updateChildren(map);
DatabaseReference message_root = root.child(temp_key);
Map<String,Object> map2 = new HashMap<String, Object>();
map2.put("name",user_name);
map2.put("msg",input_msg.getText().toString());
message_root.updateChildren(map2);
input_msg.getText().clear();
}
});
root.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
append_chat_conversation(dataSnapshot);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
append_chat_conversation(dataSnapshot);
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private String chat_msg,chat_user_name;
private void append_chat_conversation(DataSnapshot dataSnapshot) {
Iterator i = dataSnapshot.getChildren().iterator();
while (i.hasNext()){
chat_msg = (String) ((DataSnapshot)i.next()).getValue();
chat_user_name = (String) ((DataSnapshot)i.next()).getValue();
chat_conversation.append(chat_user_name +" : "+chat_msg +" \n");
}
}
}
Here is my Firebase Data structure file
Firebase Data Structure
Any help is appreciated. Thanks in advance.
If you are using FCM, you need to understand this part of the doc before proceeding:
The onMessageReceived is provided for most message types, with the following exceptions which are listed below:
Notification messages delivered when your app is in the background. In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default
Messages with both notification and data payload, both background and foreground. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
So if you need to decide the type of payload you re sending to the android device. You may want to go for Message with both Notifications and data payload so that onMessageRecieved() is invoked at all times.
You can find more details Here
I would like to block call from specific number I tried some solution but I get errors.
I tried this solution but it is not working.
https://stackoverflow.com/a/9904826/1937692
It gives
Can not resolve symbol ITelephony
Project Structure:
http://i.hizliresim.com/P02XOd.png
PhoneCallReceiver:
package com.example.myapplication;
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 PhoneCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
PhoneCallStateListener customPhoneListener = new PhoneCallStateListener(context);
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}}
ITelephony.aidl:
package com.android.internal.telephony;
interface ITelephony {
boolean endCall();
void answerRingingCall();
void silenceRinger();
}
PhoneCallStateListener:
package com.example.myapplication;
import java.lang.reflect.Method;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
import com.android.internal.telephony.ITelephony; //Error Line
public class PhoneCallStateListener extends PhoneStateListener {
private Context context;
public PhoneCallStateListener(Context context){
this.context = context;
}
#Override
public void onCallStateChanged(int state, String incomingNumber) {
SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(context);
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
String block_number = prefs.getString("block_number", null);
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
//Turn ON the mute
audioManager.setStreamMute(AudioManager.STREAM_RING, true);
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Toast.makeText(context, "in"+block_number, Toast.LENGTH_LONG).show();
Class clazz = Class.forName(telephonyManager.getClass().getName());
Method method = clazz.getDeclaredMethod("getITelephony");
method.setAccessible(true);
ITelephony telephonyService = (ITelephony) method.invoke(telephonyManager); //Error Line
//Checking incoming call number
System.out.println("Call "+block_number);
if (incomingNumber.equalsIgnoreCase("+91"+block_number)) {
//telephonyService.silenceRinger();//Security exception problem
telephonyService = (ITelephony) method.invoke(telephonyManager);
telephonyService.silenceRinger();
System.out.println(" in "+block_number);
telephonyService.endCall();
}
} catch (Exception e) {
Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
}
//Turn OFF the mute
audioManager.setStreamMute(AudioManager.STREAM_RING, false);
break;
case PhoneStateListener.LISTEN_CALL_STATE:
}
super.onCallStateChanged(state, incomingNumber);
}}
Error - Red Lines:
http://i.hizliresim.com/YD8zQA.png
The interface ITelephony is package-visible and therefore not accessible from outside of the package com.android.internal.telephony.
That's because it is an internal Android API.
you have added ITelephony.AIDL file in your project? and if you have added then your package name must be com/android/internal/telephony/ITelephony.AIDL: for more information Blocking Incoming call. download AIDL file from here
My app listens to BOOT_COMPLETED to start.
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</receiver>
But if my app crashes, how could I get it to automatically restart?
BOOT_COMPLETED is not a sticky intent.
To get Answer of your question is very simple. In that case you need to use Thread.setDefaultUncaughtExceptionHandler(). It will always enter in uncaughtException() in case your application crashed.For Check Full Tutorial Here
public class YourApplication extends Application {
private static Context mContext;
public static YourApplication instace;
#Override
public void onCreate() {
super.onCreate();
mContext = getApplicationContext();
instace = this;
}
#Override
public Context getApplicationContext() {
return super.getApplicationContext();
}
public static YourApplication getIntance() {
return instace;
}
}
DefaultExceptionHandler.java
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.lang.Thread.UncaughtExceptionHandler;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.util.Log;
/**
* This custom class is used to handle exception.
*
* #author Chintan Rathod (http://www.chintanrathod.com)
*/
public class DefaultExceptionHandler implements UncaughtExceptionHandler {
private UncaughtExceptionHandler defaultUEH;
Activity activity;
public DefaultExceptionHandler(Activity activity) {
this.activity = activity;
}
#Override
public void uncaughtException(Thread thread, Throwable ex) {
try {
Intent intent = new Intent(activity, RelaunchActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(
YourApplication.getInstance().getBaseContext(), 0, intent, intent.getFlags());
//Following code will restart your application after 2 seconds
AlarmManager mgr = (AlarmManager) YourApplication.getInstance().getBaseContext()
.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000,
pendingIntent);
//This will finish your activity manually
activity.finish();
//This will stop your application and take out from it.
System.exit(2);
} catch (IOException e) {
e.printStackTrace();
}
}
}
I have made small app for Android mobile.
In one situation I am not getting any solution.
Actually my app has small functionality for calling to customer.
So after call ended I need that event of which last number will dialed or which app is runs.
AndroidManifest:
<receiver android:name=".PhoneStateBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE">
</action></intent-filter>
</receiver>
Add following permission:
<uses-permission android:name="android.permission.READ_PHONE_STATE">
</uses-permission>
PhoneStateBroadcastReceiver.java (refactored the code a bit)
package com.mobisys.android.salesbooster;
import com.mobisys.android.salesbooster.database.HelperDatabase;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.ContactsContract.PhoneLookup;
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 incoming_number;
private int prev_state;
#Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); //TelephonyManager object
CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE); //Register our listener with TelephonyManager
Bundle bundle = intent.getExtras();
String phoneNr = bundle.getString("incoming_number");
Log.v(TAG, "phoneNr: "+phoneNr);
mContext = context;
}
/* Custom PhoneStateListener */
public class CustomPhoneStateListener extends PhoneStateListener {
private static final String TAG = "CustomPhoneStateListener";
#Override
public void onCallStateChanged(int state, String incomingNumber){
if( incomingNumber != null && incomingNumber.length() > 0 )
incoming_number = incomingNumber;
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
Log.d(TAG, "CALL_STATE_RINGING");
prev_state=state;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d(TAG, "CALL_STATE_OFFHOOK");
prev_state=state;
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.d(TAG, "CALL_STATE_IDLE==>"+incoming_number);
if((prev_state == TelephonyManager.CALL_STATE_OFFHOOK)){
prev_state=state;
//Answered Call which is ended
}
if((prev_state == TelephonyManager.CALL_STATE_RINGING)){
prev_state=state;
//Rejected or Missed call
}
break;
}
}
}
}
Read more here, Source : http://mobisys.in/blog/2011/09/is-your-call-ended-on-android-phone/