Posto un po' di codice allora...
codice:
public class ServizioTelefonate extends Service {
private int notifiche;
private Notification notification;
private Notification servicenotification;
private PendingIntent pIntent;
private PendingIntent pIntent2;
private int notificationNumber;
private NotificationManager notificaservizio;
private NotificationManager notificationManager2;
private BroadcastReceiver br;
private BroadcastReceiver br2;
private static final String ADMINPREF="adminpref";
private static final String CONTATTIDAINSERIRE = "contattidainserire";
private static final String LASTUPDATE="lastupdate";
private static final String NUMEROCONTATTI = "numerocontatti";
boolean running=true;
long inizio=0;
long fine=0;
long durata=0;
String telefono="no number";
String azienda= "-";
int numeronotifica=100;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
ServizioOnAir();
br=new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
try {
if ((intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL"))&&(running)) {
if (!(intent.getExtras().getString("android.intent.extra.PHONE_NUMBER")==null)){
telefono = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
}
}
else {telefono = "numero nascosto";}
inizio=Calendar.getInstance().getTimeInMillis();
}
} catch (Exception e) {
}
}
};
this.registerReceiver(br,new IntentFilter("android.intent.action.NEW_OUTGOING_CALL"));
try{
TelephonyManager telephonyManager=(TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener listener;
listener = new PhoneStateListener() {
int statoprecedente=TelephonyManager.CALL_STATE_IDLE;
@Override
public void onCallStateChanged(final int state, String incomingNumber) {
if (running){
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
fine=Calendar.getInstance().getTimeInMillis();
if ((statoprecedente==TelephonyManager.CALL_STATE_OFFHOOK)&&(running)){
durata=fine-inizio;
notificatelefonata("telefonata con: "+telefono,numeronotifica,telefono,inizio,fine,azienda);
numeronotifica++;
}
statoprecedente=TelephonyManager.CALL_STATE_IDLE;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if (statoprecedente==TelephonyManager.CALL_STATE_RINGING){
}else{
}
statoprecedente=TelephonyManager.CALL_STATE_OFFHOOK;
inizio=Calendar.getInstance().getTimeInMillis();
break;
case TelephonyManager.CALL_STATE_RINGING:
if (!(incomingNumber==null)){
telefono = incomingNumber;
}
else {telefono = "numero nascosto";}
statoprecedente=TelephonyManager.CALL_STATE_RINGING;
break;
}
}
}
};
telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}catch(Exception ie){}
}
public void notificatelefonata(String notificatelefonata,int tag,String num, long inizio, long fine,String azienda) {
//notifico la telefonata con un intente che poi mi lancia una activity...
}
public void ServizioOnAir() {
// metto una notifica sul servizio attivo
}
@Override
public void onDestroy() {
running=false;
this.unregisterReceiver(br);
Log.i(LOG_TAG, "unregistrato br");
notificaservizio = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificaservizio.cancel(10);
this.stopSelf();
super.onDestroy();
}
}