I need help with app(or service?) basically I was just fooling around, and decided to see if I can make my app send an email if it detects my phone is ringing. (I know it seems stupid but its more of a proof of concept for me. I made custom class in my app (no default main activity) I don't think its getting executed.. I set a toast message to pop up when the app is running, but I never saw one show up. So, I figured I need to do something to make my class execute as if its the default one, and if so, how would I go about doing that? In the android manifest, the only things I added to that were the permissions of read_phone_state, and internet(in case it was needed to send an email)
My question is what would I need to change (no need to debug it.) to make it work? Do I need to add extra info in my manifest file? Do I need to change settings where it will execute "callservice" class when the app first starts? Please, and thanks.
public class MainActivity extends Activity {
PhoneStateListener listener;
TelephonyManager tm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(), "Service has started", Toast.LENGTH_LONG).show();
listener = new MyphoneStateListener();
tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
public class MyphoneStateListener extends PhoneStateListener
{
public void onCallStateChanged(int state, String incomingNumber)
{
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
break;
case TelephonyManager.CALL_STATE_RINGING:
sendemail();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
default:
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
private void sendemail()
{
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"random#gmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT , "phone is ringing");
i.putExtra(Intent.EXTRA_TEXT , "email successfully sent");
startActivity(Intent.createChooser(i, "send mail..."));
Toast.makeText(getApplicationContext(), "Email has been sent", Toast.LENGTH_LONG).show();
try
{
startActivity(Intent.createChooser(i, "Send Email..."));
}
catch(android.content.ActivityNotFoundException ex)
{
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
as in doc onCallStateChanged take two parameters first int state and second is String incomingNumber but in your current code you are just passing single parameter to it means your are not overriding onCallStateChanged method of PhoneStateListener change your code as to work :
class MyPhoneStateListener extends PhoneStateListener{
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
break;
case TelephonyManager.CALL_STATE_RINGING:
sendemail();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
default:
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
Related
My app is has a simple service, i need to pause that service when user makes or receive a call and service has to resume after call has ended.
Here is my Main Activity looks like:-
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(getApplicationContext(), MyService.class);
startService(intent);
TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
TelephonyMgr.listen(new TeleListener(),
PhoneStateListener.LISTEN_CALL_STATE);
}
class TeleListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Toast.makeText(getApplicationContext(), "CALL_STATE_IDLE",
Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(getApplicationContext(), "CALL_STATE_OFFHOOK",
Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
Toast.makeText(getApplicationContext(), incomingNumber,
Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "CALL_STATE_RINGING",
Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}}
Here is my Service class file:-
public class MyService extends Service {
private static final String TAG = "MyService";
private boolean isRunning = false;
#Override
public void onCreate() {
Log.i(TAG, "Service onCreate");
isRunning = true;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "Service onStartCommand");
new Thread(new Runnable() {
#Override
public void run() {
for (int i = 0; i < Integer.MAX_VALUE; i++) {
try {
Thread.sleep(1000);
} catch (Exception e) {
}
if (isRunning) {
Log.i(TAG, "Service running");
}
}
stopSelf();
}
}).start();
return Service.START_STICKY;
}
#Override
public IBinder onBind(Intent arg0) {
Log.i(TAG, "Service onBind");
return null;
}
#Override
public void onDestroy() {
isRunning = false;
Log.i(TAG, "Service onDestroy");
}}
From the TeleListener class in MainActivity i'm able to fetch the phone state. My problem is when the TelephonyManager is equal to CALL_STATE_OFFHOOK i need to pause the MyService and when the TelephonyManager is equal to CALL_STATE_IDLE i need to resume the MyService
you need to use onCallStateChanged method.
put this lines in your onCreate() method,it will initialize object of TelephonyManager and will setup listener for you.
TelephonyManager tManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
ListenToPhoneState listener = new ListenToPhoneState()
tManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
class definition of innerclass ListenToPhoneState will be looking like this,
private class ListenToPhoneState extends PhoneStateListener {
boolean callEnded=false;
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
UTILS.Log_e("State changed: " , state+"Idle");
if(callEnded)
{
//you will be here at **STEP 4**
//you should stop service again over here
}
else
{
//you will be here at **STEP 1**
//stop your service over here,
//i.e. stopService (new Intent(`your_context`,`CallService.class`));
//NOTE: `your_context` with appropriate context and `CallService.class` with appropriate class name of your service which you want to stop.
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
UTILS.Log_e("State changed: " , state+"Offhook");
//you will be here at **STEP 3**
// you will be here when you cut call
callEnded=true;
break;
case TelephonyManager.CALL_STATE_RINGING:
UTILS.Log_e("State changed: " , state+"Ringing");
//you will be here at **STEP 2**
break;
default:
break;
}
}
}
Explaination: While the call,your listener will go through following states,
Step 1: TelephonyManager.CALL_STATE_IDLE
initially your call state will be idle that is why the variable callEnded will have the value false.
Step 2: TelephonyManager.CALL_STATE_RINGING
now you are waiting for the receiver person to receive your call
Step 3: TelephonyManager.CALL_STATE_OFFHOOK
you cut the call
Step 4: TelephonyManager.CALL_STATE_IDLE
idle again
NOTE: If you don't want to know when the call ends and what should be done after ending the call then just remove callEnded variable and stop the service whenever you enter in the block of TelephonyManager.CALL_STATE_IDLE
I hope it will be helpful !!
You cannot pause or resume a service, you can start your service again when call ends. Inside PhoneStateListener's TelephonyManager.CALL_STATE_OFFHOOK constant, start your service again! Using different states of PhoneStateListener as mentioned by #Tijo, you can decide when to start or stop your service.
I am newbie in android development. So I need help .
i would like to display an activity when phone state is ringing, and kill this activity when state is idle or offhook.
So I built a class extends Broadcastreceiver. I call myphonestatelistener class extends PhoneStateListener . Also i create an activity named ringingactivity.
Here is my code (MyPhoneStateListener):
public void onCallStateChanged(int state,String incomingNumber){
switch(state)
{
case TelephonyManager.CALL_STATE_IDLE:
Log.d("DEBUG", "IDLE");
Intent killIntentidle = new Intent();
killIntentidle.setAction("RingingOver") ;
ctx.sendBroadcast(killIntentidle);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d("DEBUG", "OFFHOOK");
Intent killIntent = new Intent();
killIntent.setAction("RingingOver") ;
ctx.sendBroadcast(killIntent);
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d("DEBUG", "RINGING");
Intent dialogIntent = new Intent(ctx , RingingActivity.class) ;
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(dialogIntent);
break;
}
}
In my tests, i call emulator from telnet and it succesfully display activity when it is ringing. When i accept or decline call, activity is finished . But when i call emulator again, activity could not be recreated again. I cant see it on the screen. What am i doing wrong?
Here is my activity code :
public class RingingActivity extends Activity {
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.ringing_activity);
IntentFilter filter = new IntentFilter();
filter.addAction("RingingOver");
registerReceiver(receiver, filter);
//registerReceiver(receiver,filter) ;
}
public void finish() {
super.finish();
};
}
Thanks for your help!
I suspect the issue is in your MyPhoneStateListener code. The other code look O.K to me.
Some how your PhoneStateListener is not alive next time. Are you able to see "RINGING" logs for second time?
I am not sure as why you need to call Garbage Collector. Please try to fix the root cause as why, activity is not created; sometimes. First analyze the issue by putting logs in your MyPhoneStateListener and RingingActivity. In MyPhoneStateListener, do print the state of RingingActivity activity, before taking any action (Start/kill activity).
Thank you very much for your reply Munish Katoch. I have a solution but not sure if i find the right solution . when I use Garbage Collecter , activity could be seen in screen in each calling.
Here is my code :
public class MyPhoneStateListener extends PhoneStateListener {
Context ctx ;
private boolean iscalling ;
MyPhoneStateListener(Context ctx)
{
this.ctx = ctx ;
}
public void onCallStateChanged(int state,String incomingNumber){
switch(state)
{
case TelephonyManager.CALL_STATE_IDLE:
Log.d("DEBUG", "IDLE");
if(iscalling == true)
{
Intent killIntentidle = new Intent();
killIntentidle.setAction("RingingOver") ;
ctx.sendBroadcast(killIntentidle);
System.gc() ;
iscalling = false ;
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d("DEBUG", "OFFHOOK");
if(iscalling == true)
{
Intent killIntent = new Intent();
killIntent.setAction("RingingOver") ;
ctx.sendBroadcast(killIntent);
System.gc() ;
iscalling = false ;
}
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d("DEBUG", "RINGING");
iscalling = true ;
Intent dialogIntent = new Intent(ctx , RingingActivity.class) ;
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(dialogIntent);
System.gc() ;
break;
}
}
}
I asked for help from you guys on a program, and it seems it will work. if I can get threading/asynctask to work. I tried to work on threading already and I didn't get it to work. The reason why I am trying to do threading is because my program crashes when I load it, and basically what its suppose to do is to send an email if someone is calling, because I always misplace my phone or keep it on silent, and I am not aware of it.
Service-
public class Callservice extends Service {
PhoneStateListener listener;
TelephonyManager tm;
#Override
public void onCreate()
{
Toast.makeText(getApplicationContext(), "Service has started", Toast.LENGTH_LONG).show();
listener = new MyphoneStateListener();
tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
public class MyphoneStateListener extends PhoneStateListener
{
public void onCallStateChanged(int state, String incomingNumber)
{
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
break;
case TelephonyManager.CALL_STATE_RINGING:
sendemail();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
default:
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
private void sendemail()
{
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"random#gmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT , "phone is ringing");
i.putExtra(Intent.EXTRA_TEXT , "email successfully sent");
startActivity(Intent.createChooser(i, "send mail..."));
Toast.makeText(getApplicationContext(), "Email has been sent", Toast.LENGTH_LONG).show();
try
{
startActivity(Intent.createChooser(i, "Send Email..."));
}
catch(android.content.ActivityNotFoundException ex)
{
}
}
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
Activity-
public class MainActivity extends Activity {
Callservice callservice = new Callservice();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
callservice.onCreate();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Change MainActivity activity as for starting Service from Activity:
public class MainActivity extends Activity {
//Callservice callservice = new Callservice();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(this,Callservice.class));
}
// your code here...
and make sure you have added your Service in AndroidManifest.xml as:
<service android:name=".Callservice" />
In the app that I'm making it requires a change in activity on the end of a phone call. The code I'm using isn't working like the way I want it:
protected class EndCallListener extends PhoneStateListener {
#Override
public void onCallStateChanged(int state, String incomingNumber){
switch(state){
case TelephonyManager.CALL_STATE_OFFHOOK:
Intent intent = new Intent(MainActivity.this, Password.class);
startActivity(intent);
break;
default:
}
super.onCallStateChanged(state, incomingNumber);
}
}
Any ideas?
I need my app to find out when a phone call is taking place.
It should work on when another phone is being called and also when a call is answered. I just need my app to get notified of exactly when the connection starts and when it stops (not the dialing, ringing, etc.).
Is this somehow possible?
You can use broadcast class to achieve this as below:
public class PhoneStateBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(new CustomPhoneStateListener(context),
PhoneStateListener.LISTEN_CALL_STATE);
}
}
class CustomPhoneStateListener extends PhoneStateListener {
// private static final String TAG = "PhoneStateChanged";
Context context; // Context to make Toast if required
public CustomPhoneStateListener(Context context) {
super();
this.context = context;
}
#Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
//WHEN PHONE WILL BE IDLE
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// when Off hook i.e in call
break;
case TelephonyManager.CALL_STATE_RINGING:
// when Ringing
break;
default:
break;
}
}
}
Hope this will help you.