1,电话 InCall 监听
public class InCallReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Service.TELEPHONY_SERVICE);
switch (tm.getCallState())
{
case TelephonyManager.CALL_STATE_RINGING:
String incomingNumber = intent
.getStringExtra("incoming_number");
if ("12345678".equals(incomingNumber))
{
try
{
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Service.TELEPHONY_SERVICE);
Class<TelephonyManager> telephonyManagerClass = TelephonyManager.class;
Method telephonyMethod = telephonyManagerClass
.getDeclaredMethod("getITelephony",
(Class[]) null);
telephonyMethod.setAccessible(true);
Object obj = telephonyMethod
.invoke(telephonyManager, (Object[]) null);
Method endCallMethod = obj.getClass().getMethod("endCall", null);
endCallMethod.setAccessible(true);
endCallMethod.invoke(obj, null);
}
catch (Exception e)
{
Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
break;
}
}
}
<receiver android:name="mobile.android.jx.call.aidl.InCallReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
2,电话 OutCall 监听
public class InOutCallReceiver extends BroadcastReceiver
{
@Override
public void onReceive(final Context context, final Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL))
{
String outcommingNumber = intent
.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(context, outcommingNumber, Toast.LENGTH_LONG).show();
}
else
{
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Service.TELEPHONY_SERVICE);
String incomingNumber = intent.getStringExtra("incoming_number");
switch (tm.getCallState())
{
case TelephonyManager.CALL_STATE_RINGING: // ��������
Toast.makeText(context,
"CALL_STATE_RINGING��" + incomingNumber,
Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK: // ժ��
Toast.makeText(context,
"CALL_STATE_OFFHOOK��" + incomingNumber,
Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_IDLE: // �һ�
Toast.makeText(context,
"CALL_STATE_IDLE��" + incomingNumber,
Toast.LENGTH_SHORT).show();
break;
}
}
}
}
<receiver android:name="InOutCallReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
3,获得通讯录列表
a)获得所有电话号码
Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI,
null, null, null, CallLog.Calls.DEFAULT_SORT_ORDER);
SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, cursor, new String[]
{ "number" }, new int[]
{ android.R.id.text1 });
setListAdapter(simpleCursorAdapter);
b)获得所有联系人信息
Cursor cursor = getContentResolver().query(
Uri.withAppendedPath(ContactsContract.AUTHORITY_URI,
"data/phones"), null, null, null, null);
SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, cursor, new String[]
{ "display_name", "data1" }, new int[]
{ android.R.id.text1, android.R.id.text2 });
ContentValues values = new ContentValues();
Uri rawContactUri = getContentResolver().insert(
RawContacts.CONTENT_URI, values);
long rawContactsId = ContentUris.parseId(rawContactUri);
values.clear();
values.put(StructuredName.RAW_CONTACT_ID, rawContactsId);
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
values.put(StructuredName.DISPLAY_NAME, "Li Ning");
getContentResolver().insert(Data.CONTENT_URI, values);
values.clear();
values.put(Phone.RAW_CONTACT_ID, rawContactsId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, "999999");
getContentResolver().insert(Data.CONTENT_URI, values);
cursor = getContentResolver().query(Data.CONTENT_URI, null,
Phone.NUMBER + "=?", new String[]
{ "999999" }, null);
cursor.moveToFirst();
String id = cursor.getString(cursor
.getColumnIndex(Phone.RAW_CONTACT_ID));
values.clear();
values.put(StructuredName.DISPLAY_NAME, "Liu Ming");
getContentResolver().update(Data.CONTENT_URI, values,
Phone.RAW_CONTACT_ID + "=?", new String[]
{ id });
getContentResolver().delete(Data.CONTENT_URI, "display_name=?",
new String[]
{ "Li Ning" });
/*
* Cursor cursor = getContentResolver().query(
* Uri.withAppendedPath(ContactsContract.AUTHORITY_URI, "contacts"),
* null, null, null, null); SimpleCursorAdapter simpleCursorAdapter =
* new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1,
* cursor, new String[] { "display_name"}, new int[] {
* android.R.id.text1});
*/
setListAdapter(simpleCursorAdapter);