要監聽android打**和接**,只需下面2步驟
第一步,寫乙個receiver繼承自broadcastreceiver
import android.content.broadcastreceiver;
import android.content.context;
import android.content.intent;
import android.telephony.telephonymanager;
import android.util.log;
public class phonestatebroadcastreceiver extends broadcastreceiver
private static final string tag = "phonestatebroadcastreceiver";
private static boolean mincomingflag = false;
private static string mincomingnumber = null;
@override
public void onreceive(context context, intent intent)
// 如果是撥打**
if (intent.getaction().equals(intent.action_new_outgoing_call))
mincomingflag = false;
string phonenumber = intent.getstringextra(intent.extra_phone_number);
log.i(tag, "call out:" + phonenumber);
} else
// 如果是來電
telephonymanager tmanager =
(telephonymanager) context.getsystemservice(service.telephony_service);
switch (tmanager.getcallstate())
case telephonymanager.call_state_ringing:
mincomingnumber = intent.getstringextra("incoming_number");
log.i(tag, "ringing :" + mincomingnumber);
break;
case telephonymanager.call_state_offhook:
if (mincomingflag)
log.i(tag, "incoming accept :" + mincomingnumber);
break;
case telephonymanager.call_state_idle:
if (mincomingflag)
log.i(tag, "incoming idle");
break;
第二步:在androidmanifest.xml,配置寫好的receiver,並攔截相應的broadcastaction,
另外注意加上相應的許可權
Android監聽來電和去電
要監聽android打 和接 只需下面2步驟 第一步,寫乙個receiver繼承自broadcastreceiver import android.content.broadcastreceiver import android.content.context import android.cont...
android 輕鬆監聽來電和去電
要監聽android打 和接 只需下面2步驟 1.第一步,寫乙個receiver繼承自broadcastreceiver public class phonestatreceiver extends broadcastreceiverelse break case telephonymanager....
android 輕鬆監聽來電和去電
要監聽android打 和接 只需下面2步驟 1.第一步,寫乙個receiver繼承自broadcastreceiver public class phonestatreceiver extends broadcastreceiverelse break case telephonymanager....