Custom Caller screen in android - android

I want custom calling screen I have achieved that but problem is that my screen is not going back when call cuts.i(..) want to terminate screen when call cuts. My code is:
public class receiver_Call extends BroadcastReceiver {
public void onReceive(final Context context, Intent intent) {
Thread pageTimer = new Thread(){
public void run(){
try{
sleep(1000);
} catch (InterruptedException e){
e.printStackTrace();
} finally {
//Toast.makeText(context," text", 5).show();
Intent i = new Intent();
i.setClass(context, My_call_receiver.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
};
pageTimer.start();
}
}

Have you tried a listener for when the call ends:
EndCallListener callListener = new EndCallListener();
TelephonyManager mTM = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
And in your Listener:
private class EndCallListener extends PhoneStateListener {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
if(TelephonyManager.CALL_STATE_RINGING == state) {}
if(TelephonyManager.CALL_STATE_OFFHOOK == state) {}
if(TelephonyManager.CALL_STATE_IDLE == state) {}
}
}
EndCallListener is your class which extends PhoneStateListener (which is defined above)

Related

Android Broadcast in one Fragment

I'm building my own VoIP app. Originally use Activity and BroadcastReceiver and it works fine, now I want to convert them to Fragment for my drawer.
Because BroadcastReceiver cannot use by Fragment, so I find some methods on stackoverflow but still not work...
All the code below is in the same file
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
V = inflater.inflate(R.layout.dial, container, false);
filter = new IntentFilter();
filter.addAction("android.SipDemo.INCOMING_CALL");
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(callReceiver, filter);
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
initializeManager();
// Inflate the layout for this fragment
return V;
}
private final BroadcastReceiver callReceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
SipAudioCall incomingCall = null;
try {
SipAudioCall.Listener listener = new SipAudioCall.Listener() {
#Override
public void onRinging(SipAudioCall call, SipProfile caller) {
try {
call.answerCall(30);
} catch (Exception e) {
e.printStackTrace();
}
}
public void onCallEnded(SipAudioCall call) {
endMessage();
}
};
incomingCall = manager.takeAudioCall(intent, listener);
updateStatus("call incoming");
call = incomingCall;
call.answerCall(30);
call.startAudio();
call.setSpeakerMode(isSpeakerClicked);
} catch (Exception e) {
if (incomingCall != null) {
incomingCall.close();
}
}
}
};
In your Fragment :
Call registerBroadcast() in your onCreateView
private void registerBroadcast() {
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(addCallReceiver,
new IntentFilter(getString(R.string.broadcast_add_call)));
}
BroadcastReceiver addCallReceiver= new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// perform actions
}
};
From where you want to trigger receiver :
LocalBroadcastManager.getInstance(mContext).sendBroadcast(new Intent(getString(R.string.broadcast_add_call)));

My Broadcastreceiver seem to detect that a call ended, still doesn't do that it should do. Does anybody see the flaws in my code?

I would like to make two calls in a row in an android app. Upon clicking button the app calls the first number. I created the broadcastreceiver below, that detects when the first call ends. It should write out that "First call ended" and then call the second number. It does not seem working. Can anybody spot the mistake in my code?
public class MainActivity extends AppCompatActivity {
public void calling(String phone) {
Intent callIntent = new Intent(Intent.ACTION_CALL)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
callIntent.setData(Uri.parse("tel:" + phone));
callIntent.putExtra("com.android.phone.extra.slot", 1);
startActivity(callIntent);
Intent intent = new Intent(this, CallReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243 , intent, 0);
startActivity(callIntent);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) this.findViewById(R.id.CallButton);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
calling("+36309577686");
}
});
}
public class CallReciever extends BroadcastReceiver {
private Context mContext;
private CustomPhoneStateListener mPhoneListener;
private String incoming_nr;
private int prev_state;
#Override
public void onReceive(Context context, Intent intent) {
mContext = context;
if (mPhoneListener == null) {
mPhoneListener = new CustomPhoneStateListener();
// TelephonyManager object
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
// Register our listener with TelephonyManager
telephony.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
/* Custom PhoneStateListener */
class CustomPhoneStateListener extends PhoneStateListener {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
if (!TextUtils.isEmpty(incomingNumber)) {
incoming_nr = incomingNumber;
}
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
prev_state = state;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
prev_state = state;
break;
case TelephonyManager.CALL_STATE_IDLE:
if ((prev_state == TelephonyManager.CALL_STATE_OFFHOOK)) {
// A call has now ended
//it writes out the call end, but does not call. why?
Toast.makeText(mContext, "Call End", Toast.LENGTH_SHORT).show();
calling("+36303853440");
prev_state = state;
}
else if ((prev_state == TelephonyManager.CALL_STATE_RINGING)) {
// Rejected or Missed call
Toast.makeText(mContext, "Rejected Call", Toast.LENGTH_SHORT).show();
prev_state = state;
}
break;
}
}
}
}
}
You are not registering the CallReciever anywhere. Try this
Button b = (Button) this.findViewById(R.id.CallButton);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
calling("+36309577686");
registerReceiver(new CallReceiver());
}
});
Then unregister the receiver on broadcast is received

Send data from Broadcast receiver to MainActivity

I need to display status of connection on the screen. It seemed a simple task...
This is my receiver code below:
public class PowerConnectionReceiver extends BroadcastReceiver {
public PowerConnectionReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
String state;
if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
state = "Charging";
Toast.makeText(context, state, Toast.LENGTH_SHORT).show();
} else {
intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED);
state = "Not charging";
Toast.makeText(context, state, Toast.LENGTH_SHORT).show();
}
}
}
How can i display my STATE var on screen textView. Can anybody help with it? Thanks!
You need to register a Callback. Create an Interface in PowerConnectionReceiver like:
public Interface NetworkStatusCallback {
public void onStateChange(String state);
}
Now implement this Callback in your MainActivity like:
public class MainActivity extends AppCompatActivity implements PowerConnectionReceiver.NetworkStatusCallback {...}
Create a reference of this Callback in PowerConnectionReceiver and declare it like:
private NetworkStatusCallback mCallback;
mCallBack = (NetworkStatusCallback)context;
Now in onReceive(Context context, Intent intent) do this:
#Override
public void onReceive(Context context, Intent intent) {
String state;
if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
state = "Charging";
mCallback.onStateChange(state);
Toast.makeText(context, state, Toast.LENGTH_SHORT).show();
} else {
intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED);
state = "Not charging";
mCallback.onStateChange(state);
Toast.makeText(context, state, Toast.LENGTH_SHORT).show();
}
}
You will receive state in MainActivity

Make calls from custom outgoing screen

I want to replace the outgoing screen and make call through that custom screen. I succeed to bring custom screen but I could not make calls. If I use ACTION.CALL then it call the Default outgoing screen.
public class OutgoingCallBroadcastReciver extends BroadcastReceiver {
Context c;
public OutgoingCallBroadcastReciver() {
}
#Override
public void onReceive(final Context context, final Intent intent) {
final String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
c=context;
setResultData(null);
setResultData(number);
callActionHandler.postDelayed(runRingingActivity, 1000);
}
Handler callActionHandler = new Handler();
Runnable runRingingActivity = new Runnable(){
#Override
public void run()
{
Intent intentPhoneCall = new Intent(c, OutgoingScreen.class);
intentPhoneCall.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intentPhoneCall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
c.startActivity(intentPhoneCall);
}
};
}
For outgoing calls I make custom work around and created custom permission of outgoing reciever in manifest.
I had called the activity after a delay using handler.
Hope i will work for you.
Please check the below code.
#Override
public void onReceive(Context context, Intent intent)
{
mcontext = context;
setResultData(null);
dialphonenumber = getResultData();
if dialphonenumber == null)
{
dialphonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
}
setResultData(dialphonenumber);
callActionHandler.postDelayed(runRingingActivity, 1000);
}
Handler callActionHandler = new Handler();
Runnable runRingingActivity = new Runnable()
{
#Override
public void run()
{
Intent intentPhoneCall = new Intent(mcontext, OutgoingCallActivity.class);
intentPhoneCall.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intentPhoneCall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mcontext.startActivity(intentPhoneCall);
}
};
Hope it will work for you.
Let me know if you have any issue.

How to getApplcationContext using BroadCastReceiver

This questions is similiar to How to use getApplicationContext in BroadcastReceiver class?.
But i didn't know how is the previous activity of his doing. So i didn't know how to resolve mine.
This is my activity :
public class backgroundApplication extends Activity {
private PendingIntent pendingIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.background_application);
/* Retrieve a PendingIntent that will perform a broadcast */
Intent alarmIntent = new Intent(backgroundApplication.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(backgroundApplication.this, 0, alarmIntent, 0);
findViewById(R.id.startAlarm).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
start();
}
});
findViewById(R.id.stopAlarm).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cancel();
}
});
findViewById(R.id.stopAlarmAt10).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startAt10();
}
});
}
for the complete code, i've got it from here : http://javatechig.com/android/repeat-alarm-example-in-android
And this is my AlarmReceiver.class extends BroadCastReceiver implements IndoorAtlasListener :
public class AlarmReceiver extends BroadcastReceiver implements IndoorAtlasListener
{
#Override
public void onReceive(Context context, Intent intent) {
initIndoorAtlas();
}
private void initIndoorAtlas() {
try {
mIndoorAtlas = IndoorAtlasFactory.createIndoorAtlas(
getApplicationContext, // this is the redline
this, // IndoorAtlasListener
mApiKey,
mApiSecret);
} catch (IndoorAtlasException ex) {
Log.e("IndoorAtlas", "init failed", ex);
}
}
Anyone can help me ?
in you onReceive method
initIndoorAtlas(context);
and in method
private void initIndoorAtlas(Context context) {
try {
mIndoorAtlas = IndoorAtlasFactory.createIndoorAtlas(
context, // this is the redline
this, // IndoorAtlasListener
mApiKey,
mApiSecret);
} catch (IndoorAtlasException ex) {
Log.e("IndoorAtlas", "init failed", ex);
}
}
If method is form implementing interface then use this code..
private Context context:
and onReceive Method
this.context = context;
and in you method use context as it is global variable we can access it.

Categories

Resources