在manifest.xml文件中提及下面的廣播接收器。
<receiver android:name="com.example.incomingcall.IncomingCallReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
IncomingCallReceiver.java:
public class IncomingCallReceiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Thread thread = new Thread(){
private int sleepTime = 400;
@Override
public void run() {
super.run();
try {
int wait_Time = 0;
while (wait_Time < sleepTime) {
sleep(100);
wait_Time += 100;
}
}catch (Exception e) {
Toast.makeText(context,
"Error Occured Because:" + e.getMessage(),
Toast.LENGTH_SHORT).show();
}
context.startActivity(new Intent(context,CustomActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
};
thread.run();
}
}
}
}
請在這裏發佈相關的代碼,而不僅僅是一個鏈接。 –
整個源庫? –
我發佈的鏈接有點不相關,因爲任何接聽電話(我嘗試過)只實現應用層的方法都會導致本地電話應用程序獲得焦點。因此,我對源代碼的調查@ https://android.googlesource.com –