任何人都可以告诉我如何在Android中运行服务时弹出一个窗口,意味着,我正在开发一个应用程序,当Android手机中检测到来电时会弹出一个窗口。我搜索了很多,但没有找到任何确切的解释。所以请告诉我我该怎么做。
我正在使用此代码。当我运行这个应用程序时,它会强行退出。
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.os.Bundle;
public class CallHelper extends Activity
{
/**
* Listener to detect incoming calls.
*/
public class CallStateListener extends PhoneStateListener
{
@Override
public void onCallStateChanged(int state, String incomingNumber)
{
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
// called when someone is ringing to this phone
Intent i = new Intent(getApplicationContext(), Out.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //must be provided
getApplicationContext().startActivity(i);
break;
}
}
}
/**
* Broadcast receiver to detect the outgoing calls.
*/
private Context ctx;
private TelephonyManager tm;
private CallStateListener callStateListener;
public CallHelper(Context ctx)
{
this.ctx = ctx;
callStateListener = new CallStateListener();
}
/**
* Start calls detection.
*/
public void start()
{
tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
/**
* Stop calls detection.
*/
public void stop()
{
tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
}
//Intent intent = new Intent();
//startService(intent);
}
提前致谢!!!!!
答案 0 :(得分:0)
you can use the Broadcast receiver in your service in manifest.xml
---------------------------------------------
<receiver android:name="com.pericent.saferide.recciver.InComingCall" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
--------------------------------------------------
and when call is coming you call your activity in on receive method of BroadcastReciver
and you change your code listener like this
--------------------------------------------------
switch(state) {
case CALL_STATE_RINGING:
checkCallAsUnreceived(incomingNumber);
break;
case CALL_STATE_OFFHOOK: //call was received
checkCallAsReceived();
break;
case CALL_STATE_IDLE: //no active calls
sendMsgIfCallWasntReceived();
break;
}
-------------------------------------------------------------
and for more detail you can see the below url:-
https://github.com/MarcinOS/AutoResponder