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[]{"[email protected]"});
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With