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" />
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 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
I create a service to start a activity on button click.In first time activity is start but when I closed activity or leave as it is open than activity is start by service automatically.Pleas help me ,I want start activity only click on button not second time automatically by service class
I use following code-
Service class
public class Myservise extends Service {
#Override
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Let it continue running until it is stopped.
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return START_STICKY;
}
#Override
public void onCreate() {
super.onCreate();
Intent dialogIntent = new Intent(this, Demo.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
}
Main Activity
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
// Method to start the service
public void startService(View view) {
startService(new Intent(getBaseContext(), Myservise.class));
}
}
Second Activity
public class Demo extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
}
}
Pleas help me .
First you should return
return START_NOT_STICKY;
in onStartCommand(..) in service.
and try to stop service on onDestroy();
Also refer This SO POST
This question already has answers here:
How to make a phone call in android and come back to my activity when the call is done?
(21 answers)
Closed 9 years ago.
I try here to make a phone call and it works but the problem is when i finish the call,i don't get my view back.it stay stack in the calls list...and when i click on the button return in my phone it goes back to my activity that i want to show...Thanks :)
#Override
public void onClick(View arg0) {
if (arg0 == btn_ajout) {
Intent i=new Intent(Docteur_gestion.this, AjoutDocActivity.class);
startActivity(i);
}
if(arg0 == btn_appel) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+global_txt_tel_doc));
startActivity(callIntent);
}
}
You need to implement a phoneStateListener as follows:
public class MainActivity extends Activity {
final Context context = this;
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.buttonCall);
// add PhoneStateListener
PhoneCallListener phoneListener = new PhoneCallListener();
TelephonyManager telephonyManager = (TelephonyManager) this
.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);
// add button listener
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:0377778888"));
startActivity(callIntent);
}
});
}
//monitor phone call activities
private class PhoneCallListener extends PhoneStateListener {
private boolean isPhoneCalling = false;
String LOG_TAG = "LOGGING 123";
#Override
public void onCallStateChanged(int state, String incomingNumber) {
if (TelephonyManager.CALL_STATE_RINGING == state) {
// phone ringing
Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
// active
Log.i(LOG_TAG, "OFFHOOK");
isPhoneCalling = true;
}
if (TelephonyManager.CALL_STATE_IDLE == state) {
// run when class initial and phone call ended,
// need detect flag from CALL_STATE_OFFHOOK
Log.i(LOG_TAG, "IDLE");
if (isPhoneCalling) {
Log.i(LOG_TAG, "restart app");
// restart app
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(
getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
isPhoneCalling = false;
}
}
}
}
}
Two permissions required in the manifest
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
and
<uses-permission android:name="android.permission.CALL_PHONE" />
I found this solution on this link
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);
}
}