在服务器上保存传入和传出呼叫详细信息(Android)

时间:2015-10-16 07:26:34

标签: android android-studio broadcastreceiver incoming-call

我想自动在服务器上保存传入和传出呼叫。我想要编写代码,但有时它可以工作,有时我的应用程序崩溃..任何人都可以帮助我解决这个问题。下面是我的CallReceiver.java代码:

public class MyCallReceiver extends BroadcastReceiver {

Context context;
String currentDateTimeString;
String state;
String number;

@Override
public void onReceive(Context context, Intent intent) {


    //this.context = context;
    state=intent.getStringExtra(TelephonyManager.EXTRA_STATE);

    if(state==null){
        //outgoing call
        number=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        Log.i("tag", "Outgoing number : " +number);
        currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
        Log.i("tag", "Date :" + currentDateTimeString);
        doSaveCallRecord(1);


    }
    else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
    {
        //incoming call
        number=intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Log.i("tag","Incoming number : "+number);
        currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
        Log.i("tag", "Date :" + currentDateTimeString);
        doSaveCallRecord(0);
    }
}

public static String getContactName(Context context,String phoneNumber){
    ContentResolver cr = context.getContentResolver();
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
    if (cursor == null) {
        return null;
    }
    String contactName = null;
    if(cursor.moveToFirst()) {
        contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));

        Toast.makeText(context,"Name :"+contactName,Toast.LENGTH_LONG).show();
    }

    if(cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
    return contactName;
}

0 个答案:

没有答案