call 电话

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);



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值